Why can't I assign a character from getchar() into character array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • askingallday
    New Member
    • Jul 2015
    • 1

    Why can't I assign a character from getchar() into character array?

    So I am trying to get a sentence using getchar() and print it out.
    I assigned the character I get from getchar() into a char variable and it worked fine.
    But when I assigned into character array directly, it looks like the loop never ends and wait for me to input more.

    <CODE>

    #include <stdio.h>

    int main()
    {
    char a[81];
    int i = 0;

    printf("Enter a sentence\n");

    while( a[i] != '\n')
    {
    a[i] = getchar();
    i++;
    }

    a[i - 1] = '\0';

    printf("%s\n", a);

    }

    </CODE>

    So this code does not work because I used a[i] directly.
    Following code works fine.

    <CODE>


    #include <stdio.h>

    int main()
    {
    char a[81];
    int i = 0;
    char character;

    printf("Enter a sentence\n");

    while( character != '\n')
    {
    character = getchar();
    a[i] = character;
    i++;
    }

    a[i - 1] = '\0';

    printf("%s\n", a);

    }

    </CODE>

    What's going on???Why can't I just assign it into array ? Why should I go through a variable ?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    while( a[i] != '\n')
     {
     a[i] = getchar();
     i++;
    }
    doesn't do what you think.
    Assume i is 0. Then getchar assigns to a[0]. Then i is incremented so the while loop checks a[1] != '\n'

    Unfortunately, a[1] hasn't been initialized yet.

    Maybe:

    Code:
    for( i =0; ( a[i] = getchar()) != '\n'; ++i);
    BTW: always check for EOF on the return from getchar in case you exhaust the input buffer.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Another issue with both of these loops is that the loop condition is tested before the loop body is executed, so the first time through the loop you are testing uninitialized variables.
      Code:
      while( a[i] != '\n')
          {
          a[i] = getchar();
          i++;
          }
       ...
      while( character != '\n')
          {
          character = getchar();
          a[i] = character;
          i++;
          }
      One way to properly achieve your aim is to use do-while, where the test occurs after the body executes. (Notice the first loop tests moved i++ into the loop test to account for the problem identified above by @weaknessforcat s.)
      Code:
      do
          {
          a[i] = getchar();
          } while( a[i++] != '\n');
       ...
      do
          {
          character = getchar();
          a[i++] = character;
          } while( character != '\n');
      However, be aware that do-while is used a lot less often than for or while.

      Another issue that neither loop protects the program from a user who types in more than 80 characters.

      Comment

      Working...