Thread: Case statement problem

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Case statement problem

    I'm completely new to using case statements in C++ so bear with me. I'm trying to do a very simple case statement that will display the user to the user what grade they have got.

    Code:
     #include <iostream> 
    using namespace std; 
    
    int main() 
    { 
    
    int grade; 
    
    cout << "Please enter your grade"; 
    cin >> grade; 
    
    switch (grade) 
    { 
    case grade >= 70: 
    cout << endl << "You got a grade A"; 
    break; 
    case grade >= 60: 
    cout << endl << "You got a grade B1"; 
    break; 
    case grade >= 50: 
    cout << endl << "You got a grade B2"; 
    break; 
    case grade >= 40: 
    cout << endl << "You got a grade C"; 
    break; 
    case grade >= 30: 
    cout << endl << "You got a grade D"; 
    break; 
    case grade == 30: 
    cout << endl << "You Failed"; 
    } 
    
    return 0;
    
    }
    I know it is the "grade >=" ect parts that are wrong but I dont know how to right it so that it works. Anyone help?

    Thanks.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Switch case statements can only contain integers or chars as case statements.

    ie:
    Code:
    case 1:
    // code
    break;
    
    or
    
    case 'A':
    case 'a':
    // code
    break;
    
    aslo, supply a default case to catch an error
    
    default:
       cout << "\nERROR: Invalid input!" << endl;
       break;
    Double Helix STL

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    It may be easier to do this:

    Code:
    cout << "Enter your grade: ";
    cin >> grade;
    
    if ( grade >= 50 )
       cout << "\nYou got a D" << endl;
    Double Helix STL

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yeah, it's the job for if statements, not switches.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    To use a switch you need to have a single constant expression for each case. So you can't do case grade <= 10. you must do case 1: , case 2: ...

    But you can change your expression grade/10 to get the tenth's place digit of grade.

    Eg:
    Code:
    switch(grade/10)
    { 
    case 9:
    case 8:
    cout << endl << "You got a grade A"; 
    break; 
    case 6: 
    cout << endl << "You got a grade B1"; 
    break; 
    case 5: 
    cout << endl << "You got a grade B2"; 
    break; 
    case 4: 
    cout << endl << "You got a grade C"; 
    break;  
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Thnaks got it working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  4. case statement wrong?
    By Swaine777 in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2003, 06:46 PM
  5. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM