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*/
Sparkle 2.20.0 by Razvan Serea
Sparkle is a free, open-source Windows optimization tool designed to make your PC faster, cleaner, and more private. With Sparkle, you can easily debloat Windows by removing unnecessary apps and services, disable Microsoft tracking to enhance privacy, and apply performance tweaks to boost speed. Its cleaner removes junk and temporary files, while every change is safe and fully reversible. Sparkle also features a modern, user-friendly interface with automatic updates, making system maintenance simple. Explore over 39 tweaks, from disabling telemetry and hibernation to optimizing network and game settings, all aimed at customizing and enhancing your Windows experience.
Sparkle supports Windows 10 and 11.
Sparkle 2.20.0 changelog:
Debloat Tweak has animated border
New homepage loading UI
New Tweak Modal (Markdown Supported)
Refactored GPU Detection
Added Tests with vitest
Added foobar2000 to apps
Added Localsend to apps
Updated Modal Styles
Added styles for disabled inputs
Added Animated Border to debloat-windows tweak
Bumped dependencies
Refactor System info logic for speed
Tweak info modals now support Markdown
Added Clear System info cache to settings
Redesigned Home Page Loading UI
Changed Some Icons around the app
Download: Sparkle 2.20.0 | Portable | ~100.0 MB (Open Source)
Links: Sparkle Website | Github | Screenshot
Get alerted to all of our Software updates on Twitter at @NeowinSoftware
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