I’m having an issue with scanf() getting stuck, and the only way to continue the program is to type exit.
My input is something like this:
L 1 1 3 4 C 2 3 4
where the numbers are the parameters for L and C.
int main ()
{
// Get use input and put in array
int count, x;
int array[4];
char type;
scanf("%c", &type);
for (x = 0; x < 2; x++)
{
if (type == 'L')
{
for (count = 0; count < 4; count++)
{
scanf("%d", &array[count]);
}
} else if (type == 'C')
{
for (count = 0; count < 3; count++)
{
scanf("%d", &array[count]);
}
}
if (scanf("%*[ \n\t]%c", &type) == 0)
{
printf("Error");
break;
}
}
The scanf statement in question:
scanf("%*[ \n\t]%c", &type)
works fine if I’m not at the end of the line, but breaks otherwise. However, I won’t know how many L and C objects I will have so can’t rely on the value from the for loop.
I think your problem is here:
You’re trying to check if you’re out of characters to read, so you want to check if
scanfreturnsEOF(which is-1, but just use theEOFconstant). If you break when you seeEOFreturned you’ll drop out of the loop as soon as you end input to the process (Ctrl+D on Linux, I believe Ctrl+Z Enter on Windows)