Trouble with switch statement

This is a discussion on Trouble with switch statement within the C++ Programming forums, part of the General Programming Boards category; ...okay, i'm trying to make this program which accepts name, testscore, and marital status..then outputs them along with the letter ...

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Trouble with switch statement

    ...okay, i'm trying to make this program which accepts name, testscore, and marital status..then outputs them along with the letter grade based on test score. I don't think I'm using the cases right in the switch statement for test score, can anyone help?

    Code:
    #define CB cin.ignore(cin.rdbuf()->in_avail())
    #include <iomanip.h>
    #include <stdlib.h>
     main()
    
            {   int TestScore, Grade; char Name[20], MarStat;
    
                cout<<"\n Enter a Student's Name: "; cin.getline(Name,20);CB;
                cout<<" Enter Student's Test TestScore: "; cin>>TestScore; CB;
                cout<<" Enter Student's Marital Status: "; cin>>MarStat; CB;
    
                while(TestScore != -1)
                {
                  switch(TestScore)
                   {case "TestScore<=100": case"TestScore>=90":
                         Grade='A'        ;
                    case "TestScore<90":  case "TestScore>=80":
                         Grade='B'        ;
                    case "TestScore<80":  case "TestScore>=70":
                         Grade='C'        ;
                    case "TestScore<70":  case "TestScore>=60":
                         Grade='D'        ;
                    case "TestScore<60":  case "TestScore>=0)":
                         Grade='F'        ;
                    default:
                         Grade="Invalid Grade";}
    
                   switch(MarStat)
                    { case 'M': case 'm':
                       MarStat=" Married ";
                      case 'S': case 's':
                       MarStat=" Single ";
                      case 'D': case 'd':
                       MarStat=" Divorced ";
                      case 'W': case 'w':
                       MarStat=" Widowed ";}
                 }
                  cout<<"\n The Name of the Student: "<<Name;
                  cout<<"\n The Marital Status of the Student: "<<MarStat;
                  cout<<"\n The Test Score of the student: "<<TestScore;
                  cout<<"\n The Letter Grade of the student: "<<Grade;
           system("PAUSE");
          return 0;
    }
    Thanks in advance..
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    c++ primer plus

    Code:
    switch(integer expression)
    {
            case label1 : statement
            case label2 : statement
            .....
            default : statement
    }
    integer-expression must be an expression that reduces to an integer expression

    each label must an integer constant expression. Most often labels are simple int or char constants or enumerations ie 1 or 'q'

    this is a paraphrase from c++ primer plus

    I would use if then for testscores
    Last edited by rippascal; 03-24-2002 at 12:00 PM.

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    do you need breaks after those case statment?

    Code:
    {case "TestScore<=100": case"TestScore>=90":
                         Grade='A'    
    break;

  4. #4
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    you guys were a great help- I have no compiler errors now. But a few bugs which I don't know how to get rid of when i run the program

    Code:
    #define CB cin.ignore(cin.rdbuf()->in_avail())
    #include <iomanip.h>
    #include <stdlib.h>
     main()
    
            {   int TestScore, Grade; char Name[20], MarStat;
    
                   while(TestScore != -1)
                {
                cout<<"\n Enter a Student's Name: "; cin.getline(Name,20); CB;
                cout<<" Enter Student's Test TestScore: "; cin>>TestScore; CB;
                cout<<" Enter Student's Marital Status: "; cin>>MarStat  ; CB;
    
    
                  switch(TestScore)
                   {case 1:"<=100 && >=90";
                         Grade=(char)'A'         ; break;
                    case 2:"<90  && >=80";
                         Grade=(char)'B'         ; break;
                    case 3:"<80  && >=70";
                         Grade=(char)'C'         ; break;
                    case 4:"<70  && >=60";
                         Grade=(char)'D'         ; break;
                    case 5:"<60  && >=0";
                         Grade=(char)'F'         ; break;
                    default:
                         Grade=(char)"Invalid Grade";}
    
                   switch(MarStat)
                    { case 'M': case 'm':
                       MarStat=(char)" Married "; break;
                      case 'S': case 's':
                       MarStat=(char)" Single ";  break;
                      case 'D': case 'd':
                       MarStat=(char)" Divorced ";break;
                      case 'W': case 'w':
                       MarStat=(char)" Widowed "; break;}
    
                  cout<<"\n The Name of the Student: "<<Name;
                  cout<<"\n The Marital Status of the Student: "<<MarStat;
                  cout<<"\n The Test Score of the student: "<<TestScore;
                  cout<<"\n The Letter Grade of the student: "<<Grade;
                }
           system("PAUSE");
          return 0;
    }
    ..when I run the program..it outputs name and test score correctly, but the marital status and letter grade comes out weird. Can anyone help? It's buggin' the crap outta me..
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  2. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  3. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  4. Switch Statement
    By DarkDot in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2007, 10:11 PM
  5. Equivalent of less than in a switch statement?
    By Diamonds in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 07:14 PM

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