Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. how to use getchar() to store n names in an array ?

how to use getchar() to store n names in an array ?

Scheduled Pinned Locked Moved ATL / WTL / STL
data-structuresjsontutorialquestion
4 Posts 3 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    i am trying to take multiple names by using getchar() but it only taking the first value and just skipping the rest. here is the code.

    #include int main()
    {
    char name[10][30], ch;
    int i;

    for(i=0; i<10; i++)
    {
        printf("Enter name: \\n");
    
        int j=0;
        while(ch != '\\n')
        {
            ch = getchar();
            name\[i\]\[j\] = ch;
            j++;
        }
        name\[i\]\[j\] = '\\0';
    
    }
    
    return 0;
    

    }

    L R 2 Replies Last reply
    0
    • T Tarun Jha

      i am trying to take multiple names by using getchar() but it only taking the first value and just skipping the rest. here is the code.

      #include int main()
      {
      char name[10][30], ch;
      int i;

      for(i=0; i<10; i++)
      {
          printf("Enter name: \\n");
      
          int j=0;
          while(ch != '\\n')
          {
              ch = getchar();
              name\[i\]\[j\] = ch;
              j++;
          }
          name\[i\]\[j\] = '\\0';
      
      }
      
      return 0;
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Because at the end of the first time round the while loop ch will contain the newline character, so every other loop will terminate immediately. You must remember to reset variables after you have finished with them. Please get yourself a good study guide on C and learn the language the proper way.

      1 Reply Last reply
      0
      • T Tarun Jha

        i am trying to take multiple names by using getchar() but it only taking the first value and just skipping the rest. here is the code.

        #include int main()
        {
        char name[10][30], ch;
        int i;

        for(i=0; i<10; i++)
        {
            printf("Enter name: \\n");
        
            int j=0;
            while(ch != '\\n')
            {
                ch = getchar();
                name\[i\]\[j\] = ch;
                j++;
            }
            name\[i\]\[j\] = '\\0';
        
        }
        
        return 0;
        

        }

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        In addition to what Richard wrote, you have limited the names to 30 characters so make sure more than that are not entered.

        T 1 Reply Last reply
        0
        • R Rick York

          In addition to what Richard wrote, you have limited the names to 30 characters so make sure more than that are not entered.

          T Offline
          T Offline
          Tarun Jha
          wrote on last edited by
          #4

          ok

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups