-2

The code is supposed to accept a line of user input containing different characters and then print out one line containing only the letters. For example, Cat8h08er64832&*^ine would be Catherine. However, the code works and outputs "Catherine" however the program doesn't exit... see picture here I'm not sure if the loop is just looping infinitely or...

int main(void){

  int i=0, j=0;
  char userString[1000];
  char alphabet[1000];

  printf("Please enter a string: ");

  while(scanf("%c", &userString[i])){
    if((userString[i]>='A' && userString[i]<='Z')||(userString[i]>='a'&&userString[i]<='z')){
        alphabet[j]=userString[i];
        printf("%c", alphabet[j]);
        j++;
     }
     i++;
   }
   printf("\n");
   return 0;
}
Quinn Tai
  • 55
  • 1
  • 7
  • 1
    There is no exit condition for the loop. What should be the exit condition? – Harsh Nov 17 '15 at 00:46
  • `scanf` returns `EOF` when it reaches the end of the input (when you type `Ctl-d`). You're not checking for that. The `%c` operator can never return 0 conversions, because any character matches it. – Barmar Nov 17 '15 at 00:50
  • I thought that while(scanf("%c", &userString[i])) would loop until the end of the user input (enter key)? Does it not? – Quinn Tai Nov 17 '15 at 00:51
  • @Barmar ohh I see that makes a lot of sense. – Quinn Tai Nov 17 '15 at 00:52
  • If you want to read a single line of input, use `fgets` to read into an array. Then loop over the characters of the array. – Barmar Nov 17 '15 at 00:52
  • if I loop until the enter key is pressed, the theoretically the following should work right? while(scanf("%c", &userString[i]) != '\n') however I encounter the same issue – Quinn Tai Nov 17 '15 at 00:53
  • Do not just post external links, or images in general. Instead paste text **in** you question. – too honest for this site Nov 17 '15 at 00:54
  • @QuinnTai No. That will still allow the loop to continue when scanf returns EOF, since EOF (usually) == -1. – Paul Hicks Nov 17 '15 at 00:54

1 Answers1

1

Your problem is that you're checking that scanf is finished by checking for the return value 0. scanf returns EOF (usually, -1) when there is no more input. So if you get some input (return 1) then no more input (return -1), your loop won't ever exit.

Change the scanf condition to check for <> EOF.

This answer also explains it quite well.

Community
  • 1
  • 1
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • while(scanf("%c", &userString[i]) != EOF) doesn't work as well. Is this because %c accepts the enter key (\n) as a valid input, and doesn't recognize that I want it to terminate once the enter key is pressed? – Quinn Tai Nov 17 '15 at 01:05
  • Sounds likely. scanf is tricky to get working with human input. You may need to check for multiple termination values and error conditions. – Paul Hicks Nov 17 '15 at 01:11
  • I ended up using fgets and looping through there. Thanks for all your help anyways. – Quinn Tai Nov 17 '15 at 01:15
  • Good stuff. fgets protects you from some of the many pitfalls in scanf. – Paul Hicks Nov 17 '15 at 01:17