fgets() not waiting for input - c

I wrote the following code:
int N;
scanf("%d", &N);
int i;
for (i = 0; i < N; i++) {
char line[LINE_MAX];
if (fgets(line, LINE_MAX, stdin) != NULL) {
// do stuff with line here
printf("%c - %c\n", line[0], line[1]);
}
}
I have an input file which has the number of lines it has, and then that number of lines followed which I want to process. So I read in the number of lines into N. After that, I use fgets to get the line to be able to process it.
However, fgets does not seem to wait for a stdin the first time. I always get output of -, and then it waits for input. Meaning, the first iteration of the loop, it is not waiting for standard input at fgets and just prints out two empty characters separated by - as my printf does.
Why is that? How can I get fgets to wait for input each time? I feel like it is a threading issue.

You can place a call to fflush() just after the scanf() like this:
int N;
scanf("%d", &N);
fflush (stdin);
int i;
... (rest of code)...
So the newline character ges erased from the stdin buffer and the next fgets() will stop to ask for input.

As geekosaur said, you are not handling the newline left behind by scanf. You can modify your scanf format string to take it into account:
scanf("%d *[^\n]", &N);
*[^\n] says to ignore everything after your integer input that isn't a newline, but don't do anything with the newline (skip it).
Test program output:
emulawsk#cs:~/testing$ ./test2
3
13
1 - 3
26
2 - 6
59
5 - 9

It has nothing to do with threading. scanf() reads exactly what you ask it to; it's leaving everything else unread, notably the newline following the data. (You also aren't dealing with the possibility that the user didn't type what you intended.)
If you want to do line oriented input, use fgets(). Don't use scanf() and hope the system can magically intuit that you want to ignore what you didn't read.

Related

Why do I have to use gets() function twice in a row? [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Why is the gets function so dangerous that it should not be used?
(13 answers)
Closed 2 years ago.
I have an unusual problem regarding the code below.
void Menu()
{
bool end = 0;
int n;
while (!end)
{
scanf("%d", &n);
switch(n)
{
case 1:
my_strlen_show();
break;
//other irrelevant cases 2-6
case 7:
end = 1;
break;
default:
printf("ERROR");
break;
}
}
}
int my_strlen(const char *str)
{
int count = 0;
for (; *str != '\0' ; str++)
{
count++;
}
return count;
}
void my_strlen_show()
{
char tab[1000];
printf("\n\nEnter a sentence: ");
gets(tab);
gets(tab);
printf("\nWritten sentence has %d characters.\n\n", my_strlen(tab));
return;
}
I have no idea why I have to write gets(tab) twice to get the program to work properly. When I use it once, my_strlren_show() function executes instantly and shows that the sentence has 0 characters. I am aware that I can use other methods such as a scanf() function inside a for loop, but I am curious why this method works in a peculiar way.
Can anyone explain why that is the case? I would be very thankful.
Do not use gets(). Its dangerous unsafety has earned it the dubious distinction of belonging to a very small set of functions that have been withdrawn from the C language standard.
However, you would probably experience the same issue if you changed to fgets:
fgets(tab, sizeof(tab), stdin);
The issue is that gets() and fgets() read through the end of the current line (or until the buffer is filled in the case of fgets()). The preceding scanf() consumed only the bytes through the end of a decimal integer, leaving the rest of that line on the input stream, waiting to be read. That includes at least a newline marking the end of the line. That has to be consumed before the wanted input can be read with fgets() or gets(). One way to accomplish that would be:
if ((scanf("%*[^\n]") == EOF) || (getchar() == EOF)) {
// handle end-of-file or I/O error ...
}
The scanf reads and discards any characters preceding the next newline, and, supposing that the end of the file is not reached and no I/O error occurs, the getchar() consumes the newline itself.
Your first scanf only reads a single integer from stdin. When you press enter after inputting the integer, a newline (\n) is sent to the stdin, which just stays there - waiting to be picked up by the next function reading from stdin.
The next gets then reads this newline and instantly returns. So you needed another gets to actually read the input.
With that said, you should never even use gets in the first place - it's a deprecated function. On top of that, consider using fgets for reading input. scanf is really an input parsing function, not a reading function. It only reads what it can parse, and leaves everything else in the stdin.
If you still decide to go the scanf route, you should consume the newline on the first input using "%d\n" - not only that, you must also check the return value for scanf, it returns the number of values it was able to parse.
Now the next fgets call won't have to consume the left over newline. It'll wait for another line of user input (note that the newline will be included in the buffer fgets reads into)-
char tab[1000];
printf("\n\nEnter a sentence: ");
if (fgets(tab, 1000, stdin) == NULL)
{
// handle error during reading
return;
}
// tab now has the user input, delimited with a `\n`
// if you want to get rid of this newline - use `strcspn`
tab[strcspn(name, "\n")] = 0
Docs on strcspn
I would however, recommend you to go the full fgets route and do the integer parsing with sscanf.
int n;
char buff[4096];
if (fgets(buff, 4096, stdin) == NULL)
{
// handle error during reading
return;
}
if (sscanf(tab, "%d", &n) != 1)
{
// parsing failed - sscanf should've parsed exactly 1 value
// handle error
return;
}
// use n here
Here's a full guide on how to move away from scanf - which will mention this specific problem.

fgets( ) does not wait for user input? [duplicate]

I wrote the following code:
int N;
scanf("%d", &N);
int i;
for (i = 0; i < N; i++) {
char line[LINE_MAX];
if (fgets(line, LINE_MAX, stdin) != NULL) {
// do stuff with line here
printf("%c - %c\n", line[0], line[1]);
}
}
I have an input file which has the number of lines it has, and then that number of lines followed which I want to process. So I read in the number of lines into N. After that, I use fgets to get the line to be able to process it.
However, fgets does not seem to wait for a stdin the first time. I always get output of -, and then it waits for input. Meaning, the first iteration of the loop, it is not waiting for standard input at fgets and just prints out two empty characters separated by - as my printf does.
Why is that? How can I get fgets to wait for input each time? I feel like it is a threading issue.
You can place a call to fflush() just after the scanf() like this:
int N;
scanf("%d", &N);
fflush (stdin);
int i;
... (rest of code)...
So the newline character ges erased from the stdin buffer and the next fgets() will stop to ask for input.
As geekosaur said, you are not handling the newline left behind by scanf. You can modify your scanf format string to take it into account:
scanf("%d *[^\n]", &N);
*[^\n] says to ignore everything after your integer input that isn't a newline, but don't do anything with the newline (skip it).
Test program output:
emulawsk#cs:~/testing$ ./test2
3
13
1 - 3
26
2 - 6
59
5 - 9
It has nothing to do with threading. scanf() reads exactly what you ask it to; it's leaving everything else unread, notably the newline following the data. (You also aren't dealing with the possibility that the user didn't type what you intended.)
If you want to do line oriented input, use fgets(). Don't use scanf() and hope the system can magically intuit that you want to ignore what you didn't read.

Scanf is being ignored, how to prevent?

I got this code:
#include <stdio.h>
int main(void){
char ordet;
int again = 1;
while(again == 1){
printf("Write a name: \n");
while (ordet!='\n'){
scanf("%c",&ordet);
if('A'<= ordet && ordet <='W')
{
printf("%c",ordet+3);
}
else if('W'<=ordet){
printf("%c", ordet-22);
}
}
printf("\nDo you want to write a new name? 1/0");
scanf("%d", &again);
}
return 0;
}
It runs fine the first time but as soon as you press 1 to write a new name you never get the chance to write it, it just keeps asking "Do you want to write a new name?" and ignores "Write a name:". I have tested with fflush(stdin), != EFO and space infront of %c but nothing works. How could I fix this since I really have no idea, started out with C a few days ago.
while (ordet!='\n'){
ordet is used without initialization here. Not quite UB (if your implementation uses unsigned char or signed char has no trap-representations), but bad enough.
scanf("%c",&ordet);
The above line is the only line potentially setting ordet. Please take note that the loop starting with it is only entered when ordet is not '\n'.
You probably want to reset it before entering the inner loop, or use a do-while-loop.
Actually, changing both loops to do-while-loops would be good.
scanf("%d", &again);
The above line reads a number from beginning of stdin, but leaves the newline.
This must be dealt with by consuming all whitespace on repeat.
Luckily, that's easily done as the inner loop ignores whitespace.
Change "%c" to " %c".
Also, all your scanf-invocations can fail. Check them!
There are 2 problems with your code:
ordet is not initialized before its first usage. It is undefined behavior.
You do not reset ordet anywhere outside the inner loop. That is, once ordet becomes equal to \n, the body of the inner loop is never executed again.
To fix it, you can write something like ordet = 0 before inner loop. And don't forget to skip new line character that is left in the buffer after reading again variable.
All input you write in the console from the keyboard is placed in a buffer, scanf reads from this buffer however if there are more characters in the buffer than what your format specifier specifies those characters will remain in the buffer.
e.g.
scanf( "%d", &n );
when you enter a numerical value 100 and press ENTER afterwards, the ENTER character is placed in the buffer as well "100\n" when you do scanf on the buffer (stdin) the 100 is extracted from the buffer but the \n remains.
Next time you write scanf("%d", &n ); the \n is still in there and will not be read by %d since it is not a %d number.
IMO the best way to handle this is instead to read from the keyboard using fgets, then after the content has been read use sscanf to extract the information you want:
char buffer[128];
fgets( buffer, sizeof(buffer), stdin );
sscanf( buffer, "%d", &n );
you also then can have additional error checks:
if (fgets( buffer, sizeof(buffer), stdin ) != NULL )
{
if (sscanf( buffer, "%d", &n ) ==1)
{
...
}
}
now to your code:
if you want to read character by character from the keyboard use fgetc() not scanf()
Use the C-runtime function isalpha()/toupper() to recognize a character.
In C, a character is an int, that is why most functions like fgetc return an int and not a char.
In general I would recommend using fgets() instead of reading one character at a time.

Changing the scanf() delimiter

My objective is to change the delimiter of scanf to "\n".
I tried using scanf("%[^\n]s",sen); and works fine for single inputs.
But when i put the same line inside a for loop for multiple sentences it gives me garbage values.
Does anyone know why?
Here's my code:
char sen[20];
for (i=0;i<2;i++)
{
scanf("%[^\n]s",sen);
printf("%s\n",sen);
}
Consider this (C99) code:
#include <stdio.h>
int main(void)
{
char buffer[256];
while (scanf("%255[^\n]", buffer) == 1)
printf("Found <<%s>>\n", buffer);
int c;
if ((c = getchar()) != EOF)
printf("Failed on character %d (%c)\n", c, c);
return(0);
}
When I run it and type in a string 'absolutely anything with spaces TABTABtabs galore!', it gives me:
Found <<absolutely anything with spaces tabs galore!>>
Failed on character 10 (
)
ASCII (UTF-8) 1010 is newline, of course.
Does this help you understand your problem?
It works in this case (for a single line) but if I want to take multiple lines of input into an array of arrays then it fails. And I don't get how scanf returns a value in your code?
There are reasons why many (most?) experienced C programmers avoid scanf() and fscanf() like the plague; they're too hard to get to work correctly. I'd recommend this alternative, using sscanf(), which does not get the same execration that scanf() and fscanf() do.
#include <stdio.h>
int main(void)
{
char line[256];
char sen[256];
while (fgets(line, sizeof(line), stdin) != 0)
{
if (sscanf(line, "%255[^\n]", sen) != 1)
break;
printf("Found <<%s>>\n", sen);
}
int c;
if ((c = getchar()) != EOF)
printf("Failed on character %d (%c)\n", c, c);
return(0);
}
This reads the line of input (using fgets() which ensures no buffer overflow (pretend that the gets() function, if you've heard of it, melts your computer to a pool of metal and silicon), then uses sscanf() to process that line. This deals with newlines, which are the downfall of the original code.
char sen[20];
for (i=0;i<2;i++)
{
scanf("%[^\n]s",sen);
printf("%s\n",sen);
}
Problems:
You do not check whether scanf() succeeded.
You leave the newline in the buffer on the first iteration; the second iteration generates a return value of 0 because the first character to read is newline, which is the character excluded by the scan set.
The gibberish you see is likely the first line of input, repeated. Indeed, if it were not for the bounded loop, it would not wait for you to type anything more; it would spit out the first line over and over again.
Return value from scanf()
The definition of scanf() (from ISO/IEC 9899:1999) is:
ยง7.19.6.4 The scanf function
Synopsis
#include <stdio.h>
int scanf(const char * restrict format, ...);
Description
2 The scanf function is equivalent to fscanf with the argument stdin interposed
before the arguments to scanf.
Returns
3 The scanf function returns the value of the macro EOF if an input failure occurs before
any conversion. Otherwise, the scanf function returns the number of input items
assigned, which can be fewer than provided for, or even zero, in the event of an early
matching failure.
Note that when the loop in my first program exits, it is because scanf() returned 0, not EOF.
%[^\n] leaves the newline in the buffer. %[^\n]%*c eats the newline character.
In any case, %[^\n] can read any number of characters and cause buffer overflow or worse.
I use the format string %*[^\n]%*c to gobble the remainder of a line of input from a file. For example, one can read a number and discard the remainder of the line by %d%*[^\n]%*c. This is useful if there is a comment or label following the number, or other data that is not needed.
char sen[20];
for (i=0;i<2;i++)
{
scanf("%[^\n]s",sen);
printf("%s\n",sen);
getchar();
}
Hope this helps ... actually "\n" remains in stream input buffer... Ee need to flush it out before scanf is invoked again
I know I am late, but I ran into same problem after testing C after a long time.
The problem here is the new line is considered as input for next iteration.
So, here is my solution, use getchar() to discard the newline the input stream:
char s[10][25];
int i;
for(i = 0; i < 10; i++){
printf("Enter string: ");
scanf("%s", s[i]);
getchar();
}
Hope it helps :)
While using scanf("%[^\n]", sen) in a loop, the problem that occurs is that the \n stays within the input buffer and is not flushed. As a result next time, when the same input syntax is used, it reads the \n and considers it as a null input. A simple but effective solution to address this problem is to use:
char sen[20];
for (i=0;i<2;i++)
{
scanf("%[^\n]%*c",sen);
printf("%s\n",sen);
}
%*c gets rid of the \n character in the input buffer.

scanf ignoring, infinite loop

int flag = 0;
int price = 0;
while (flag==0)
{
printf("\nEnter Product price: ");
scanf("%d",&price);
if (price==0)
printf("input not valid\n");
else
flag=1;
}
When I enter a valid number, the loop ends as expected. But if I enter something that isn't a number, like hello, then the code goes into an infinite loop. It just keeps printing Enter Product price: and input not valid. But it doesn't wait for me to enter a new number. Why is that?
When you enter something that isn't a number, scanf will fail and will leave those characters on the input. So if you enter hello, scanf will see the h, reject it as not valid for a decimal number, and leave it on the input. The next time through the loop, scanf will see the h again, so it just keeps looping forever.
One solution to this problem is to read an entire line of input with fgets and then parse the line with sscanf. That way, if the sscanf fails, nothing is left on the input. The user will have to enter a new line for fgets to read.
Something along these lines:
char buffer[STRING_SIZE];
...
while(...) {
...
fgets(buffer, STRING_SIZE, stdin);
if ( sscanf(buffer, "%d", &price) == 1 )
break; // sscanf succeeded, end the loop
...
}
If you just do a getchar as suggested in another answer, then you might miss the \n character in case the user types something after the number (e.g. a whitespace, possibly followed by other characters).
You should always test the return value of sscanf. It returns the number of conversions assigned, so if the return value isn't the same as the number of conversions requested, it means that the parsing has failed. In this example, there is 1 conversion requested, so sscanf returns 1 when it's successful.
The %d format is for decimals. When scanf fails (something other a decimal is entered) the character that caused it to fail will remain as the input.
Example.
int va;
scanf("%d",&va);
printf("Val %d 1 \n", val);
scanf("%d",&va);
printf("Val %d 2 \n", val);
return 0;
So no conversion occurs.
The scanf function returns the value of the macro EOF if an input failure occurs before
any conversion. Otherwise, the scanf function returns the number of input items
assigned, which can be fewer than provided for, or even zero, in the event of an early
matching failure
7.19.6. The scanf function - JTC1/SC22/WG14 - C
So you should note that scanf returns its own form of notice for success
int scanf(char *format)
so you could have also did the following
do {
printf("Enter Product \n");
}
while (scanf("%d", &sale.m_price) == 1);
if(scanf("%d", &sale.m_price) == 0)
PrintWrongInput();
Also keep in the back of your head to try to stay away from scanf. scanf or scan formatted should not be used for interactive user input. See the C FAQ 12.20
After the first number, a '\n' will be in the input buffer (the return you pressed to input the number), so in the second iteration the scanf call will fail (becouse \n isn't a number), scanf will not remove that \n from the buffer, so in the next iteration it will fail again and so on.
You can fix that by reading the '\n' with a getchar() call after scanf.
The "answers" that say it will because there is a '\n' in the buffer are mistaken -- scanf("%d", ...) skips white space, including newlines.
It goes into an infinite loop if x contains 0 and scanf encounters a non-number (not just whitespace) or EOF because x will stay 0 and there's no way for it to become otherwise. This should be clear from just looking at your code and thinking about what it will do in that case.
It goes into an infinite loop because scanf() will not consumed the input token if match fails. scanf() will try to match the same input again and again. you need to flush the stdin.
if (!scanf("%d", &sale.m_price))
fflush(stdin);
Edit: Back when I first wrote this answer, I was so stupid and ignorant about how scanf() worked.
First of all let me clear something, scanf() is not a broken function, if I don't know how scanf() works and I don't know how to use it, then I probably haven't read the manual for scans() and that cannot be scanf()'s fault.
Second in order to understand what is wrong with your code you need to know how scanf() works.
When you use scanf("%d", &price) in your code, the scanf() tries to read in an integer from the input, but if you enter a non numeric value, scanf() knows it isn't the right data type, so it puts the read input back into the buffer, on the next loop cycle however the invalid input is still in the buffer which will cause scanf() to fail again because the buffer hasn't been emptied, and this cycle goes on forever.
In order to tackle this problem you can use the return value of scanf(), which will be the number of successful inputs read, however you need to discard the invalid inputs by flushing the buffer in order to avoid an infinite loop, the input buffer is flushed when the enter key is pressed, you can do this using the getchar() function to make a pause to get an input, which will require you to press the enter key thus discarding the invalid input, note that, this will not make you press the enter key twice whether or not you entered the correct data type, because the newline character will still be in the buffer. After scanf() has successfully finished reading the integer from input, it will put \n back into the buffer, so getchar() will read it, but since you don't need it, it's safe to discard it:
#include <stdio.h>
int main(void)
{
int flag = 0;
int price = 0;
int status = 0;
while (flag == 0 && status != 1)
{
printf("\nEnter Product price: ");
status = scanf("%d", &price);
getchar();
if (price == 0)
printf("input not valid\n");
else
flag = 1;
}
return 0;
}

Resources