My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Thanks all for your help. I am learning C on my own, still on the loop control chapters and I'm using Code Block with mingw as compiler.
I have added this line to deal with space and newline:
Here's the full exercise:Code:else if ( grade == '\n' || grade == ' ' ) continue;
Rewrite the program of Fig. 4.7 by replacing the switch statement with a nested if/else statement, be careful to deal with the default case properly. The rewrite this new version by replacing the nested if/else statement with a series of if statements; here too, be careful to deal with the default case properly (this is more difficult than in the nested if/else statement version). This exercise demonstrates that switch is a convenience and that any switch statement can be written with only single-selection statements.
fig. 4.7:
Code:/* Fig. 4.7: fig04_07.c Counting letter grades */ #include <stdio.h> int main() { int grade; int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0; printf( "Enter the letter grades.\n" ); printf( "Enter the EOF character to end input.\n" ); while ( ( grade = getchar() ) != EOF ) { switch ( grade ) { /* switch nested in while */ case 'A': case 'a': /* grade was uppercase A */ ++aCount; /* or lowercase a */ break; case 'B': case 'b': /* grade was uppercase B */ ++bCount; /* or lowercase b */ break; case 'C': case 'c': /* grade was uppercase C */ ++cCount; /* or lowercase c */ break; case 'D': case 'd': /* grade was uppercase D */ ++dCount; /* or lowercase d */ break; case 'F': case 'f': /* grade was uppercase F */ ++fCount; /* or lowercase f */ break; case '\n': case' ': /* ignore these in input */ break; default: /* catch all other characters */ printf( "Incorrect letter grade entered." ); printf( " Enter a new grade.\n" ); break; } } printf( "\nTotals for each letter grade are:\n" ); 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; } /************************************************************************** * (C) Copyright 2000 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/
Last edited by dev123; 09-05-2010 at 12:28 PM.
You only want a single character, so a better way to deal with the unwanted characters/newline issue is to eat them all up like this after getting the first character in grade:
So with input like this:Code:int dummy; // outside of loop ... while ((dummy = getchar()) != '\n') ;
You end up with output like this:Code:Enter the EOF character to end input. a b c dedsdfwrewrwrewrwedsfw a c ^D
Code:Totals for each letter grade are: A: 2 B: 1 C: 2 D: 0 F: 0
Create a boolean flag that is set to true, e.g., use an int variable initialised to 1. Set this boolean flag to false for each case (e.g., assign 0 to the int variable). Use this flag to check for the default case.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'd do something like this:
You would flush unwanted characters as suggested by rags_to_riches.Code:while ( ( grade = getchar() ) != EOF ) { if( grade == 'a' || grade == 'A') { aCount++; continue; } /* b,c, etc ... */ if( grade == '\n' || grade == ' ') { continue; } /* now default */
My code below. My biggest inspiration for solving this was due to Rags_to_Riches and laserlight suggestions. Also I found out, the initial problem at hand was not-so-trivial.
My biggest problem was, that I thought the EOF for my home system was ( ctrl^C + Enter ).
It wasn't. I just found out by accident, that it's ( ctrl^z + Enter). I've spent many hours thinking that the code was wrong, when I was only using the wrong escape sequence.
Anyway, I learned a lot from this, and spent the last day pushed back to the beginning pages
of my C book - dealing with keyboard input.
I'm saving this code for future reference.
Code:#include <stdio.h> int main() { int grade; int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0; char junk[80]; printf( "Enter the letter grades.\n" ); printf( "Enter the EOF character to end input.\n\n" ); while ( (grade = fgetc(stdin)) != EOF ) // on my home system, EOF character is invoked { // by ( ctrl^z + ENTER ) sequence if ( grade == 'A' || grade == 'a' ) { aCount++; getchar(); // chomps out the extra '\n' per Rags_To_Riches } else if ( grade == 'B' || grade == 'b' ) { bCount++; getchar(); } else if ( grade == 'C' || grade == 'c' ) { cCount++; getchar(); } else if ( grade == 'D' || grade == 'd' ) { dCount++; getchar(); } else if ( grade == 'F' || grade == 'f' ) { fCount++; getchar(); } else { printf( "Incorrect letter grade entered." ); printf( " Enter a new grade.\n" ); gets(junk); // gets rid of many extra chars } } printf( "\nTotals for each letter grade are:\n\n" ); 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 ); printf("\n\n"); return 0; }
Last edited by Char*Pntr; 09-05-2010 at 05:23 PM. Reason: improper display inside the code....format
No, no, no, no, NO!!! NO gets()!!!Code:gets(junk); // gets rid of many extra chars
I found that using gets() was quite useful in case the user entered multiple characters.
And reading the OP's requirement, it did not say that gets() could not be used.
But I have faith that you have a valid reason.. so I'm off now to delete that part
of offending code, with another small routine without using gets().
Thanks for your input.
OK, I am posting my final version without the offending gets() function. The changes to the code are highlighted in green.
When I read the above, I knew there was more there than replying on the "merits of undefined behaviour" to a pervious poster. I knew that laserlight was also giving us a clue on how to handle extra chars from STDIN.
After making the changes, I tried to "break" the code. On one input, I typed in "qwerty" and the
code handled that appropriately.
here's the code:
Code:// 9/5/10 // #include <stdio.h> int main() { int grade, TestChar; // Test Char used as a condition to clear STDIN invalid chars int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0; printf( "Enter the letter grades.\n" ); printf( "Enter the EOF character to end input.\n\n" ); while ( (grade = fgetc(stdin)) != EOF ) // on my home system, EOF character is invoked { // by ( ctrl^z + ENTER ) sequence if ( grade == 'A' || grade == 'a' ) { aCount++; getchar(); // chomps out the extra '\n' per Rags_To_Riches } else if ( grade == 'B' || grade == 'b' ) { bCount++; getchar(); } else if ( grade == 'C' || grade == 'c' ) { cCount++; getchar(); } else if ( grade == 'D' || grade == 'd' ) { dCount++; getchar(); } else if ( grade == 'F' || grade == 'f' ) { fCount++; getchar(); } else { printf( "Incorrect letter grade entered." ); printf( " Enter a new grade.\n" ); do { TestChar = getc(stdin); } while ( TestChar != EOF && TestChar != '\n' ); // using laserlight's previous example :-) } } printf( "\nTotals for each letter grade are:\n\n" ); 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 ); printf("\n\n"); return 0; }