0

This is a part of a bigger program, but what is driving me crazy is the fact that the program doesn't wait for something to be read from stdin in the string s (it just puts null in string s) , but if I put the read of the string s first and afterwards I read the character c the program works just fine. The thing is I need to read the data in this specific order. How can i fix it?

char s[100],c;
printf("enter character:\n");
c=getchar();
printf("enter string text:\n");
fgets(s,101,stdin);   
uint8_t s_len = strlen(s) - 1;        
s[s_len] = '\0'; 
printf("i have read %s\n",s);
ironflower
  • 101
  • 1
  • 4
  • 11
  • 1
    I had the same problem, here I've solved http://stackoverflow.com/questions/15340343/strings-gets-and-do-while – Mitro Mar 31 '13 at 12:26
  • Why do you think you can read 101 bytes into a 100 char array? You should use `sizeof s` instead of a hard-coded 101. Another bug: getchar() returns int, not char. And s_len should be a size_t, not a uint8_t. – Jens Mar 31 '13 at 12:30
  • @Jens It's fine to store the result of `getchar` in a `char` if you don't want to check for EOF. – teppic Mar 31 '13 at 12:36
  • @teppic No, it's not fine. It's a bug. You would misinterpret EOF as a (char)-1 since you can't distinguish them anymore. – Jens Mar 31 '13 at 17:49
  • @Jens - I said if you _don't_ check for EOF. The C99 standard itself has an example of `getchar` being assigned to a `char`. – teppic Mar 31 '13 at 17:58
  • @teppic Are you referring to Ex. 7 in 5.1.2.3? This is about order of evaluation, not about use of getchar(). Apart from that, I consider that example a bug in C99 promoting sloppy use of getchar. Any code that does not check getchar() for EOF to determine EOF or read failure is buggy. If you *don't* check, you soldier on with a "character" that was never there when you hit EOF or read error. But you can call me a purist :-) – Jens Mar 31 '13 at 20:27
  • @Jens - I agree that it's likely a bug, but it's not if you don't care about EOF. One reason to do it is if you're reading from a memory stream which you know is larger than the amount you're reading. I'm not saying it's good style, just not universally incorrect. – teppic Mar 31 '13 at 20:31
  • @teppic Fair enough. You got me thinking. Can you point to a source that demonstrates how to use getchar() with a memory stream? – Jens Mar 31 '13 at 21:17
  • @Jens: Sure, I just wrote this quick example: http://ideone.com/4Q2L0I – teppic Mar 31 '13 at 21:21

2 Answers2

2

When you press the enter key for the first character, the actual newline is still left in the input buffer. This newline is then read by the fgets call.

You can solve this by a couple if different methods. The first is to add a dummy getchar call, to get the newline. This has the drawback that if you're on Windows that newline is actually two characters.

Another solution is to use fgets for the first character as well, and then use e.g. sscanf to extract the character.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Use

c = getchar();
int eatMe;
do 
{
   eatMe = getchar();
} while (eatMe != '\n' && eatMe != EOF);
Shmil The Cat
  • 4,548
  • 2
  • 28
  • 37
  • @teppic yep, but from ironFlower's code he/she doesn't need _really_ the value of c :) hence this plausible (if he/she needs the value of c he/she can call to multiple getchar() or use the more elegant fgets) – Shmil The Cat Mar 31 '13 at 12:28
  • It won't work because c is a `char` but must be `int` to hold `EOF`. I strongly recommend reading the comp.lang.c FAQ. – Jens Mar 31 '13 at 12:29
  • Edited my post based on the valuable feedback from Jens and ironflower needs :) – Shmil The Cat Mar 31 '13 at 13:34