Finding Letter Grade and Marital Status

This is a discussion on Finding Letter Grade and Marital Status within the C++ Programming forums, part of the General Programming Boards category; I'm working on this program which finds the letter grade and marital status of a student..the problem is outputting the ...

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

    Finding Letter Grade and Marital Status

    I'm working on this program which finds the letter grade and marital status of a student..the problem is outputting the Letter Grade and Marital Status.. they come out wrong. what am I doing wrong here?

    [code]
    #define CB cin.ignore(cin.rdbuf()->in_avail())
    #include <iomanip.h>
    #include <stdlib.h>
    main()

    { int TestScore, Grade; char Name[15], MarStat;

    while(TestScore != -1)
    {
    cout<<"\n Enter a Student's Name: "; cin.getline(Name,15);CB;
    cout<<" Enter Student's Test Score: "; cin>>TestScore;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"; break;}

    cout<<" Enter Student's Marital Status: "; cin>>MarStat;CB;

    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;
    }
    "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
    Mar 2002
    Posts
    1,595
    I've not seen the syntax of the argument being used for cin.ignore, nor do I like to use defines, so I have to say that I am suspicious of that context. However, I can't say it won't work either.

    I do know that you are using the switch statement incorrectly.

    I would add iostream.h to your list of includes and you need a
    return type for main(); should be int.

    I would use a different flagging system for the loop to be more efficient. You don't want to print out a student score of -1.

    Code:
    #include <iostream.h>
    
    int main()
    {
      int TestScore, Grade; 
      char Name[15], marStat[15];
      char MarStat; 
      char continue = 'y';
    
      while(continue == 'y')
      {
        cout<<"\n Enter a Student's Name: "; 
        cin.getline(Name,15);
      
        cout<<" Enter Student's Test Score: "; 
        cin>>TestScore;
        cin.ignore(); 
    
    
        if(TestScore < 100 && TestScore >= 90"; 
          Grade=(char)'A' ; break; 
        else if (TestScore < 90 && TestScore >= 80"; 
          Grade=(char)'B' ; break; 
        //etc. 
    
        cout  << " Enter Student's Marital Status: "; 
        cin  >> MarStat;
        cin.ignore(); 
    
       switch(MarStat) 
      { 
         case 'M': case 'm': 
           strcpy(marStat, " Married ");
           break; 
         //etc
       } 
    
      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; 
      
      cout << "To stop at this time enter anything but y:" << end;
      cout << "To continue with another student enter y:" << endl;
      cin >> continue;
     } 
     
      char dummy;
      cin >> dummy;
    
      return 0; 
    }
    Last edited by elad; 03-27-2002 at 09:58 AM.

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    I'm sorry, I didn't make it clear that I must use two switch statements- 1 for finding the letter grade and 1 for finding the marital status.
    Elad..thanks for your help, i appreciate the whole re-writing.
    Am I setting proper case conditions for the test scores? How do I get it to print MARRIED when a user enters m, for example?
    I tried using strcpy, but the compiler says " passing `char' to argument 1 of `strcpy(char *, const char *)' lacks a cast "

    Code:
    #define CB cin.ignore(cin.rdbuf()->in_avail())
    #include<iomanip.h>
    #include<stdlib.h>
    #include<string.h>
     main()
    
            {   int TestScore, Grade; char Name[15], MarStat;
    
                   while(TestScore != -1)
                {
                cout<<"\n Enter a Student's Name: "; cin.getline(Name,15);CB;
                cout<<" Enter Student's Test Score: "; cin>>TestScore;
    
                  switch(TestScore)
                   {case 1:"<=100 && >=90";
                         TestScore=(char)'A'         ; break;
                    case 2:"<90  && >=80";
                         TestScore=(char)'B'         ; break;
                    case 3:"<80  && >=70";
                         TestScore=(char)'C'         ; break;
                    case 4:"<70  && >=60";
                         TestScore=(char)'D'         ; break;
                    case 5:"<60  && >=0";
                         TestScore=(char)'F'         ; break;
                    default:
                         TestScore=(char)"Invalid Grade"; break;}
    
                   cout<<" Enter Student's Marital Status: "; cin>>MarStat;
    
                   switch(MarStat)
                    { case 'M': case 'm':
                         strcpy (MarStat,"Married"); break;
                      case 'S': case 's':
                         break;
                      case 'D': case 'd':
                         break;
                      case 'W': case 'w':
                         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;
    }
    I'm not lookin' for easy answers, I'm just lost beyond comprehension
    "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

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    MarStat is declared to be a single char, not an array of char in the first line after main(). you can't read a string, which is an array of char into a single char. That's what is causing the error. That's why I put the variable marStat (which is declared as a char array) in my rewrite. MarStat can never be a string based on how you declare it.

    switch statements are based on integer results. Since char types are actually integers to the computer they can be as case results rather than type int.

    Here's the only way I can think of to use a switch statement to do the grading:

    switch(TestScore)
    case 100:
    case 99:
    case 98:
    case 97:
    case 96:
    case 95:
    case 94:
    case 93:
    case 92:
    case 91:
    case 90:
    {
    grade = 'A';
    break;
    }
    case 89:
    .
    .
    .
    case 80:
    {
    grade = 'B';
    break;
    }
    //etc

    but that seems utterly ridiculus.

  5. #5
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    thanks ...I used it the way you're talking about-
    case 100:
    case 99:
    case 98:
    etc..

    and when I print the results..I get the right number grade that was input, but when I want to display the letter grade, it prints out integers like 65...I have absolutely noooo idea why it does this..do u?

    Code:
    #define CB cin.ignore(cin.rdbuf()->in_avail())
    #include<iomanip.h>
    #include<stdlib.h>
    #include<string.h>
     main()
    
            {   int TestScore, Grade; char Name[15], MarStat;
    
                   while(TestScore != -1)
                {
                cout<<"\n Enter a Student's Name: "; cin.getline(Name,15);CB;
                cout<<" Enter Student's Test Score: "; cin>>TestScore;
    
                  switch(TestScore)
                   {case 100: case 99: case 98: case 97: case 96: case 95:
                    case  94: case 93: case 92: case 91: case 90:
                         Grade='A'         ; break;
                    case  89: case 88: case 87: case 86: case 85: case 84:
                    case  83: case 82: case 81: case 80:
                         Grade='B'         ; break;
                    case  79: case 78: case 77: case 76: case 75: case 74:
                    case  73: case 72: case 71: case 70:
                         Grade='C'         ; break;
                    case  69: case 68: case 67: case 66: case 65: case 64:
                    case  63: case 62: case 61: case 60:
                         Grade='D'         ; break;
                    default:
                         Grade='F'         ; break;}
    
                   cout<<" Enter Student's Marital Status: "; cin>>MarStat;
    
                  /* switch(MarStat)
                    { case 'M': case 'm':
                         strcpy (MarStat,"Married"); break;
                      case 'S': case 's':
                         break;
                      case 'D': case 'd':
                         break;
                      case 'W': case 'w':
                         break;}*/ I left this part out until I can figure out how to get it to print the corresponding word.
    
    
    
                  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;
    }
    OUTPUT:
    **ALSO, if you look at the output..the second time the loop starts, it skips "Enter a Student's Name" and goes to the second prompt...what am I missing? or what am I mistakenly adding in?


    Enter a Student's Name: chris
    Enter Student's Test Score: 93
    Enter Student's Marital Status: M

    The Name of the Student: chris
    The Marital Status of the Student: M
    The Test Score of the student: 93
    The Letter Grade of the student: 65

    Enter a Student's Name: Enter Student's Test Score:
    "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

  6. #6
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    can anyone help at all? some advice would be greatly appreciated
    "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

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    24
    the reason that your skipping the name the second time through has to do with the getline function some thing to do with getting all 15 characters, your getting a number for the Grade output because you have grade set to int type when it should be char, im not to keen on #define so i dont use it much here your code and some of mine (and it works) good luck

    Code:
    #include <iostream>
    #include <iomanip.h>
    
    main()
    {
    	int TestScore, Stat; 
    	char Name[10], Last[10],Grade, MarStat;
    	TestScore=100;
    	while(TestScore != -1)      
    	{
    		/*INPUT FROM USER*/
    		cout<<"\n Enter Student's Name: "; 
    		cin>>Name;
    		cin>>Last;
            cout<<" Enter Student's Test Score: "; 
    		cin>>TestScore;
    		cout<<" Enter Student's Marital Status: ";
    		cin>>MarStat;
    		cout<<endl;
    		
    		/*CONVERT TO LETTER GRADE*/
    		if(TestScore<=100&&TestScore>=90)
    			Grade='A';
    		if(TestScore<=89&&TestScore>=80)
                Grade='B';
    		if(TestScore<=79&&TestScore>=70)
                Grade='C';
    		if(TestScore<=69&&TestScore>=60)
                Grade='D';
            if(TestScore<=59)
                Grade='F';
            
    		/*FIND MARSTAT*/
    		if(MarStat=='S'||MarStat=='s')
    			Stat=1;
    		if(MarStat=='M'||MarStat=='m')
    			Stat=2;
    		if(MarStat=='D'||MarStat=='d')
    			Stat=3;
    		if(MarStat=='W'||MarStat=='w')
    			Stat=4;
    		if(MarStat!='S'&&MarStat!='s'&&MarStat!='M'&&MarStat!='m'&&
    			MarStat!='D'&&MarStat!='d'&&MarStat!='W'&&MarStat!='w')
    			Stat=5;
    
    		/*DISPLAY OUTPUT TO SCREEN*/
            cout<<"\n The Name of the Student: "<<Name<<" "<<Last;
            cout<<"\n The Marital Status of the Student: ";
    		if(Stat==1)
    			cout<<"Single";
    		if(Stat==2)
    			cout<<"Married";
    		if(Stat==3)
    			cout<<"Divorced";
    		if(Stat==4)
    			cout<<"Widow";
    		if(Stat==5)
    			cout<<"Invalid";
            cout<<"\n The Test Score of the student: "<<TestScore;
    		cout<<"\n The Letter Grade of the student: "<<Grade;
    		cout<<endl;
    	}
    	system("PAUSE");
        return 0;
    }

  8. #8
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    what are the "break;" for?
    Paro

Popular pages Recent additions subscribe to a feed

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