Reputation: 3
In one of my problem sets I need to rewrite a program that has switches into if/else statements.
The program is to tally the grades entered, so if I enter "a" it will tally that, and when I exit the program it will give the user a final tally of all the grades that were entered.
I have two problems, the first one is it's simply not working, it's not reading the character inputs as legitimate characters that I have set. The second is how to prevent the other if statements from executing after the correct one executes.
I have it set as breaks in the if/else statements. I'm not sure if they are working because the program obviously isn't even accepting the characters.
I was going to have them be goto, but the original program had breaks so I used them instead.
{
int grade;
int aCount=0;
int bCount=0;
int cCount=0;
int dCount=0;
int fCount=0;
puts( "Enter the letter grades" );
puts( "Enter EOF to end input" );
while( ( grade = getchar() ) != EOF )
{
if( grade == 'A' && grade == 'a' )
{
++aCount;
break;
}
else
{
printf( "%s\n", "That's not a valid grade, please enter again" );
break;
}
if( grade == 'B' && grade == 'b' )
{
++bCount;
break;
}
else
{
printf( "%s\n", "That's not a valid grade, please enter again" );
break;
}
if( grade == 'C' && grade == 'c' )
{
++cCount;
break;
}
else
{
printf( "%s\n", "That's not a valid grade, please enter again" );
break;
}
if( grade == 'D' && grade == 'd' )
{
++dCount;
break;
}
else
{
printf( "%s\n", "That's not a valid grade, please enter again" );
break;
}
if( grade == 'F' && grade == 'f' )
{
++fCount;
break;
}
else
{
printf( "%s\n", "That's not a valid grade, please enter again" );
break;
}
}
puts( "\nTotals for each letter grade are:" );
printf( "A: %d\n", aCount );
printf( "B: %d\n", bCount );
printf( "C: %d\n", cCount );
printf( "D: %d\n", dCount );
printf( "F: %d\n", fCount );
return 0;
}
Upvotes: 0
Views: 1090
Reputation: 1557
Your conditions cannot be true at the same time. Change them to OR ( || )
if( grade == 'D' && grade == 'd' )
grade cannot be both D and d
Upvotes: 1
Reputation: 59987
Why not just store the grades in an array?
i.e.
int grades[] = { 0, 0, 0, 0, 0 };
while( ( grade = getchar() ) != EOF )
grade = toupper(grade);
if (grade >= 'A' && grade <= 'E') {
++grades[grade - 'A'];
} else {
printf("That's not a valid grade, please enter again\n");
}
}
Upvotes: 1
Reputation: 561
While it's syntactically correct to use break inside an if statement, it's confusing because it's really breaking out of the enclosing while statement. Something like this is closer to what you're looking for:
puts( "Enter EOF to end input" );
while( ( grade = getchar() ) != EOF ) {
if( grade == 'A' || grade == 'a' )
++aCount;
else if( grade == 'B' || grade == 'b' )
++bCount;
else if( grade == 'C' || grade == 'c' )
++cCount;
else if( grade == 'D' || grade == 'd' )
++dCount;
else if( grade == 'F' || grade == 'f' )
++fCount;
else
printf( "That's not a valid grade, please enter again\n" );
}
puts( "\nTotals for each letter grade are:" );
printf( "A: %d\n", aCount );
printf( "B: %d\n", bCount );
printf( "C: %d\n", cCount );
printf( "D: %d\n", dCount );
printf( "F: %d\n", fCount );
return 0;
}
Upvotes: 4
Reputation: 482
You should change the AND (&&) conditions to be OR (||) conditions so that either 'A' OR 'a' increments the aCount
variable. Use a large if
- else if
- else
chain to prevent the repeated error check. The error print statement can be simplified by using just a string literal.
if (grade == 'A' || grade == 'a') {
++aCount;
} else if (grade == 'B' || grade == 'b') {
++bCount;
} else if (grade == 'C' || grade == 'c') {
++cCount;
} else if (grade == 'D' || grade == 'd') {
++dCount;
} else if (grade == 'F' || grade == 'f') {
++fCount;
} else {
printf("That's not a valid grade, please enter again");
}
Upvotes: 2