Reputation: 117
So I'm having a problem with the following code and I keep getting a runtime error when I compile it. Essentially, this is a program to enter an undetermined amount of students and each student's undetermined amount of test scores, then calculate each student's GPA and display it back to the user. The sample input I used was as follows: Johnny A B
#include <stdio.h>
int main(void)
{
char StudentName[50];
char UserInput;
int GradeSum;
int TotalGrades;
float GPA;
int Test;
int Try;
Test = 0;
while ( Test != 1 )
{
printf ("Enter the student’s name...\n");
scanf ("%49s", StudentName);
Try = strcmp(StudentName, "");
if (Try != 0)
{
GradeSum = 0;
TotalGrades = 0;
GPA = 0;
while ( Test != 1 )
{
printf ("Enter the student’s letter grade...\n");
scanf (" %c", &UserInput);
if ((UserInput == 'A') || (UserInput == 'B') || (UserInput == 'C') || (UserInput == 'D') || (UserInput == 'F'))
{
if (UserInput == 'A')
{
GradeSum += 4;
TotalGrades += 1;
}
else if (UserInput == 'B')
{
GradeSum += 3;
TotalGrades += 1;
}
else if (UserInput == 'C')
{
GradeSum += 2;
TotalGrades += 1;
}
else if (UserInput == 'D')
{
GradeSum += 1;
TotalGrades += 1;
}
else if (UserInput == 'F')
{
TotalGrades += 1;
}
}
else
{
printf ("That is not a valid letter grade...\n");
}
}
GPA = ((float)GradeSum) / TotalGrades;
printf ("%s: %f\n", StudentName, GPA);
}
else
{
break;
}
}
return 0;
}
Edit: I made the adjustments suggested to me and I'm still getting a Runtime Error and the output looks like this:
Enter the student’s name...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
And it just keeps going on and on like that...
Upvotes: 0
Views: 950
Reputation: 117
There were a few syntax errors, but my biggest problem was the online compiler I was using. Plugged the code into a different one that allowed me to enter data real-time and it worked just fine. Live and learn.
Upvotes: 0
Reputation: 165
The edits made, fixed the problem for me, no run-time errors at all....
Enter the studentÆs name...
Jane
Enter the student's letter grade...
A
Enter the student's letter grade...
B
Enter the student's letter grade...
A
Enter the student's letter grade...
A
Enter the student's letter grade...
A
Enter the student's letter grade...
1
That is not a valid letter grade...
Jane: 3.800000
Enter the student's name...
Code I ran....
#include <stdio.h>
int main(void)
{
char StudentName[50];
char UserInput;
int GradeSum;
int TotalGrades;
float GPA;
int Test;
int Try;
Test = 0;
while ( Test != 1 )
{
printf ("Enter the student’s name...\n");
scanf ("%49s", StudentName);
Try = strcmp(StudentName, "");
if (Try != 0)
{
GradeSum = 0;
TotalGrades = 0;
GPA = 0;
while ( Test != 1 )
{
printf ("Enter the student’s letter grade...\n");
scanf (" %c", &UserInput);
if ((UserInput == 'A') || (UserInput == 'B') || (UserInput == 'C') || (UserInput == 'D') || (UserInput == 'F'))
{
if (UserInput == 'A')
{
GradeSum += 4;
TotalGrades += 1;
}
else if (UserInput == 'B')
{
GradeSum += 3;
TotalGrades += 1;
}
else if (UserInput == 'C')
{
GradeSum += 2;
TotalGrades += 1;
}
else if (UserInput == 'D')
{
GradeSum += 1;
TotalGrades += 1;
}
else if (UserInput == 'F')
{
TotalGrades += 1;
}
}
else
{
printf ("\nThat is not a valid letter grade...\n");
break;
}
}
GPA = ((float)GradeSum) / TotalGrades;
printf ("\n\n%s: %f\n", StudentName, GPA);
}
else
{
break;
}
}
return 0;
}
Upvotes: 0
Reputation: 323
You never actually set the value of Test, so it will run in an infinite loop.
Upvotes: 0
Reputation: 134286
Firstly, instead of
scanf ("%s", &StudentName);
adding
scanf ("%49s", StudentName);
will suffice.
Then, the content of an array cannot be compared using the ==
operator. You need to make use of strcmp()
for that.
After that, change
scanf ("%c", &UserInput);
to
scanf (" %c", &UserInput);
to avoid the previously stored newline.
Finally, to enforce the floating-point division, you can use a cast, like
GPA = ((float)GradeSum) / TotalGrades;
Upvotes: 1