Case statement problem

This is a discussion on Case statement problem within the C++ Programming forums, part of the General Programming Boards category; I'm completely new to using case statements in C++ so bear with me. I'm trying to do a very simple ...

  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,475
    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;
    I'm just trying to be a better person - My Name Is Earl

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

    Code:
    cout << "Enter your grade: ";
    cin >> grade;
    
    if ( grade >= 50 )
       cout << "\nYou got a D" << endl;
    I'm just trying to be a better person - My Name Is Earl

  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,009
    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, 01:09 AM
  3. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21