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*/
Microsoft is reportedly seeking help from its biggest cloud rival, Amazon Web Services, to address mounting capacity issues of GitHub. According to a report by Business Insider, this move of the company comes after a series of AI-driven outages on the coding platform, which Microsoft acquired in 2018. Despites its plans to migrate GitHub completely to Azure by 2027, increasing demand from AI coding tools has forced Microsoft to adopt a multi-cloud strategy...............
https://cio.economictimes.indiatimes.com/news/corporate-news/microsoft-taps-aws-for-github-capacity-amid-ai-driven-outages-and-multi-cloud-strategy/131761981
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