I'm not looking for an exact answer, but hopefully just a nudge in the correct direction. The following code compiles fine, but when I execute it and enter a valid value (i.e. A, B, C), it always prints "Incorrect grade, please enter a valid grade" on the screen. The number of grades tallies up just fine however. Am I using an incorrect operator somewhere in my nested If statement? Is my nested if statement even correct? I know that once I figure out what is wrong, I'll wonder how I overlooked it. Any help that you could offer would be much appreciated. Thank you for your time.
- Mike
#include<stdio.h>
/*function main begins program execution*/
int main()
{
int grade = 0; /*one grade*/
int aCount = 0;/*number of A's*/
int bCount = 0;/*number of B's*/
int cCount = 0;/*number of C's*/
int dCount = 0;/*number of D's*/
int fCount = 0;/*number of F's*/
printf("Enter the letter grades.\n");
printf("Enter the EOF character to end input which is ctrl z.\n\n");
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("Incorrect grade, please enter a valid grade.\n");
} /*end while*/
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);
}/*end main*/
Impossible to understand how Microsoft is thinking this is viable in context of the market, especially Apple. They just want to POSITION themselves at the ultra premium, but it doesn't make sense.
people are using threads? I dont know of a single person that I knew that signed up when it launched that still uses it, is this including "facebook integration" in the count?...
Question
Taco Smith
Hello all,
I'm not looking for an exact answer, but hopefully just a nudge in the correct direction. The following code compiles fine, but when I execute it and enter a valid value (i.e. A, B, C), it always prints "Incorrect grade, please enter a valid grade" on the screen. The number of grades tallies up just fine however. Am I using an incorrect operator somewhere in my nested If statement? Is my nested if statement even correct? I know that once I figure out what is wrong, I'll wonder how I overlooked it. Any help that you could offer would be much appreciated. Thank you for your time.
- Mike
#include<stdio.h> /*function main begins program execution*/ int main() { int grade = 0; /*one grade*/ int aCount = 0;/*number of A's*/ int bCount = 0;/*number of B's*/ int cCount = 0;/*number of C's*/ int dCount = 0;/*number of D's*/ int fCount = 0;/*number of F's*/ printf("Enter the letter grades.\n"); printf("Enter the EOF character to end input which is ctrl z.\n\n"); 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("Incorrect grade, please enter a valid grade.\n"); } /*end while*/ 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); }/*end main*/Link to comment
https://www.neowin.net/forum/topic/679282-newb-w-c-nested-if-statement-question/Share on other sites
5 answers to this question
Recommended Posts