Reputation: 23
I am making a program for some friends and myself that calculates grades with the weighted percentage and what grades need to me made on the final to pass the class with a specified grade.
The for loop in question is this,
for(;;)
{
scanf("%f", &s->grade);
if(s->grade == 'x')
{
break;
}else{
grade += s->grade;
}
}
how can I make this exit through the use of a letter rather than a number?
If you need any more code feel free to ask.
Upvotes: 0
Views: 81
Reputation: 16406
char buf[100];
while (fgets(buf, sizeof buf, stdin) && *buf != 'x')
{
sscanf(buf, "%f", &s->grade); // should check return value of sscanf to see if the input was correct
grade += s->grade;
}
Upvotes: 0
Reputation: 231
So I believe that you're asking for a percentage received from somewhere and want to convert that to a letter grade.
If so, I would receive the percentage either from another function passed into the code you have or read from a file.
Then, I would create a switch statement which takes the value received. Truncate it to an int. Then, cast that char you want for the given value. Take that char and bring it into the function you have.
Scanf("%f", x);
Function(x){ Switch(x){ Case 90: (Char*)x = 'a'; Return x; ... }
Upvotes: 0
Reputation: 8583
Either check the return value of scanf
--EOF
means nothing was parsed/scanned.
Or, use a two step process. First, read a string and check if it equals "x" or whatever, it not, use sscanf
to convert the string into a float.
Upvotes: 3
Reputation: 8338
You are already asking for a floating point number, and you are checking this against the char 'x', so just replace the 'x' with the value you want or do it in an or.
Upvotes: 0