We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Are you using scanf before that? Scanf will stop on whitespace after reading what ever is in it's format specifier, so likely after you've read the double there is still the newline after that on stdin. This is what your first call to fgets it reading. You can make a second call to fgets to get the string you're looking for.
Another option instead of mixing scanf and fgets is to use fgets for all your reading and use atoi and atof for converting to integer and double on the first two reads.
if (fgets(t, 1000, stdin) != NULL) // read in 4
i += atoi(t); // add 4 to i
if (fgets(t, 1000, stdin) != NULL) // read in 4.0
d += atof(t); // add 4.0 to d
fgets(t, 1000, stdin); // read in "is the best..."
Day 1: Data Types
You are viewing a single comment's thread. Return to all comments →
Are you using scanf before that? Scanf will stop on whitespace after reading what ever is in it's format specifier, so likely after you've read the double there is still the newline after that on stdin. This is what your first call to fgets it reading. You can make a second call to fgets to get the string you're looking for.
Another option instead of mixing scanf and fgets is to use fgets for all your reading and use atoi and atof for converting to integer and double on the first two reads.