Thread: Switch statement = infinite loop

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    Daved, thanks for the help... unfortunetely I cannot use what you have provided me

    This is homework (if you couldn't guess :P ), and we aren't allowed to use anything that hasn't been covered in class up until this point.

    There's another thing that's bothering me. There is an example in the book (a gradebook) that uses an integer type, and allows for entering characters without going into a fail state... here's the code:

    Code:
    void GradeBook::inputGrades()
    {
       int grade; // grade entered by user
       cout << "Enter the letter grades." << endl
          << "Enter the EOF character to end input." << endl;
       // loop until user types end-of-file key sequence
       while ( ( grade = cin.get() ) != EOF )
       {
          // determine which grade was entered
          switch ( grade ) // switch statement nested in while
           {
              case 'A': // grade was uppercase A
              case 'a': // or lowercase a
                aCount++; // increment aCount
                break; // necessary to exit switch
     
    		  case 'B': // grade was uppercase B
              case 'b': // or lowercase b
                 bCount++; // increment bCount
                 break; // exit switch
    
              case 'C': // grade was uppercase C
              case 'c': // or lowercase c
                 cCount++; // increment cCount
                 break; // exit switch
    
              case 'D': // grade was uppercase D
              case 'd': // or lowercase d
                 dCount++; // increment dCount
                 break; // exit switch
    
              case 'F': // grade was uppercase F
              case 'f': // or lowercase f
                 fCount++; // increment fCount
                 break; // exit switch
    
              case '\n': // ignore newlines,
              case '\t': // tabs,
              case ' ': // and spaces in input
                 break; // exit switch
    
              default: // catch all other characters
                 cout << "Incorrect letter grade entered."
                    << " Enter a new grade." << endl;
                 break; // optional; will exit switch anyway
           } // end switch
        } // end while
    } // end function inputGrades
    ... I just don't get it. I ran this program and input characters that weren't allowed for in the case section (such as g and t) and it successfully defaulted...
    Last edited by Lucid003; 10-09-2005 at 11:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  2. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  3. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  4. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM
  5. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 06:29 PM