Sign in to follow this  
Aiwendil

Why is my program skipping fgets()?

Recommended Posts

Aiwendil    158
Hi again,

I'm trying to get the user to enter a string in C by using fgets(), but my program keeps skipping over the input secion that uses fgets(). Can someone help me figure out why it's skipping?


#include <stdio.h>

int main(void)
{
char semester;
char input = ' ';
int year;
char inputArray[50];
char* className;
int lowGrades[4];
int numStudents = 0;
int i, j;
int student[50][3];
char *studentInitials[50];

printf("\t\tSEMESTER GRADE REPORT\n");
printf("\t\t-------- ----- ------\n\n");
printf("You will need the class identification, the initials of\n");
printf("and the three test scores for each student, and the low\n");
printf("cutoff score fo a grade of 'A', 'B', 'C', and 'D'.\n\n");
printf("When you have entered the scores for the last student,\n");
printf("press Return at the initials prompt.\n\n");
printf("Press Return when you are ready to proceed:\n\n");

printf("Fall or Spring semester? (F or S): ");
scanf("\n%c", &semester);
printf("Year: ");
scanf("%d", &year);
printf("\n");

printf("Which class: ");
className = fgets(inputArray, 50, stdin);

printf("\n");

for (i = 0; i < 4; i++)
{
if (i == 0)
printf("Low score for an '%c' (out of 300) : ", i + 65);
else
printf("Low score for a '%c' (out of 300) : ", i + 65);
scanf("%d", &lowGrades[i]);
}

for (i = 0; i < 4; i++)
{
printf("Student's initials: \n");
studentInitials[i] = fgets(inputArray, 50, stdin);
printf("Scores:\n");
printf(" Test #1: ");
scanf("%d", &student[numStudents][0]);
printf(" Test #2: ");
scanf("%d", &student[numStudents][1]);
printf(" Test #3: ");
scanf("%d", &student[numStudents][2]);
printf("\n");
numStudents++;
}

return 0;
}



Also, is there a way to tell if only the enter key has been pressed? I want the user to be able to add students until they press only enter for student initials (I know the loop won't really work for that right now, and I'll change it later when I know how to get only enter as input).

Thanks

Share this post


Link to post
Share on other sites
Uziel2101    242
Hey there, so yeah fgets doesn't read input from the user. It reads from a file that was opened. See this reference

http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

you want to be using the stream from the user, look at this reference

http://www.cplusplus.com/reference/iostream/istream/getline/

This is what you will be wanting to use. Bookmark that website, so useful ^^

Share this post


Link to post
Share on other sites
Zahlman    1682
The user has to press enter, and thus put a newline onto the standard input, in order for your program to see anything. Your scanf() calls don't consume this newline at the end, so it is waiting there to be consumed when fgets() is called. The newline is seen immediately as the end of the "line" you wanted to read, and that's that.

Share this post


Link to post
Share on other sites
zerothrillz    152
My guess is that scanf is leaving a newline on the input stream that's causing fgets to just read the newline and continue.

--edit--
shucks, beaten.

anyway, tacking a getc in there should do the trick.

Share this post


Link to post
Share on other sites
zerothrillz    152
No. When you hit 'enter' to flush your keyboard input to stdin, it also flushes your newline character from hitting the enter key. But scanf isn't reading that newline character. Adding fgetc(stdin) before you use fgets(...) to read the newline should fix it.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Uziel2101
Hey there, so yeah fgets doesn't read input from the user.


It certainly does if you specify stdin as the file.

Quote:
you want to be using the stream from the user


The OP is trying to write the code in C, not C++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this