Thread: Grading in c++

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    Grading in c++

    well im trying to make a program that when a person enters a letter such as A,a,B,b,C,c,D,d,F,f it will show up on the console saying your grade. i am stuck and don't know what to do. although i got this far.

    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <cmath>
    // this one is for the system library =)
    #include <cstdlib>
    
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    
    int main ()
    {
    
      int LetterGrade;
      cout << "Mini Program 5.2\n";
      cout << "Enter the letter grade:  ";
      cin >> LetterGrade;
      cout << endl;
      switch(LetterGrade)
    {
         if (LetterGrade = 'A' || 'a')
              case 'A' : cout << "a perfect score.  Excellent." << endl; break;
         else if (LetterGrade = 'B' || 'b')
              case 'B' : cout << "well above average.  Keep up the good work. " << endl;break;
         else if (LetterGrade = 'C' || 'c')
              case 'C' : cout << "average: keep trying you will get there." << endl;break;
         else if (LetterGrade = 'D' || 'd')
              case 'D' : cout << "below average: seems you need some extra help." << endl;break;
         else if (LetterGrade = 'F' || 'f')
              case 'F' : cout << "not passing: spend some more time asking questions and make sure you understand the material ahead of time." << endl;break;
         else
              default  : cout << "LETTER DOES NOT EXIST" << endl; break;
    }
        system("pause");
    return 0;
    }
    ok so i fixed it a bit that way it compiles then when i type to enter A it says the letter does not exist even though i declared it in the case.

    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <cmath>
    // this one is for the system library =)
    #include <cstdlib>
    
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    
    int main ()
    {
    
      int LetterGrade;
      cout << "Mini Program 5.2\n";
      cout << "Enter the letter grade:  ";
      cin >> LetterGrade;
      cout << endl;
      switch(LetterGrade)
    {
              case 'A' : cout << "a perfect score.  Excellent." << endl; break;
              case 'B' : cout << "well above average.  Keep up the good work. " << endl;break;
              case 'C' : cout << "average: keep trying you will get there." << endl;break;
              case 'D' : cout << "below average: seems you need some extra help." << endl;break;
              case 'F' : cout << "not passing: spend some more time asking questions and make sure you understand the material ahead of time." << endl;break;
              default  : cout << "LETTER DOES NOT EXIST" << endl; break;
    }
        system("pause");
    return 0;
    }
    Last edited by findme; 11-17-2005 at 05:55 PM.

  2. #2
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    LetterGrade should be char not int.

    blow me ... ...

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    your boolean logic should evalute conditions as opposed to constants.. (constants will always evaluate to true)


    Code:
    if (LetterGrade = 'A' ||  'a')
    
    //should be 
    
    if (LetterGrade == 'A' || LetterGrade == 'a')
    Last edited by The Brain; 11-18-2005 at 03:35 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    As was said, it should be char not an int.
    Also, in your if you use "=". That set's it equal to something. To check for equality you want "==". And you do not use switch and statments at the same time. You can use if statements in switch statements, but not both to decide what to do how you have it done.

  5. #5
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    just use "toupper" whenever the user enters the grade, and always test for capital letters.. it'll save you 50% of the "OR" statements...

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    i have no idea what toupper is. also i tried using it and it came up with an error.

    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <cmath>
    // this one is for the system library =)
    #include <cstdlib>
    
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    
    int main ()
    {
    
      char LetterGrade;
      cout << "Mini Program 5.2\n";
      cout << "Enter the letter grade:  ";
      cin >> LetterGrade;
      cout << endl;
      switch(LetterGrade)
    {
         if (LetterGrade == 'A' || LetterGrade == 'a')
              case 'A' && 'a' : cout << "a perfect score.  Excellent." << endl; break;
         else if (LetterGrade == 'B' || LetterGrade == 'b') 
              case 'B' : cout << "well above average.  Keep up the good work. " << endl;break;
         else if (LetterGrade == 'C' || LetterGrade == 'c')
             case 'C' : cout << "average: keep trying you will get there." << endl;break;
         else if (LetterGrade == 'D' || LetterGrade == 'd')
              case 'D' : cout << "below average: seems you need some extra help." << endl;break;
         else if (LetterGrade == 'F' || LetterGrade == 'f')
              case 'f' : cout << "not passing: spend some more time asking questions and make sure you understand the material ahead of time." << endl;break;
         else 
              default  : cout << "LETTER DOES NOT EXIST" << endl; break;
    }
        system("pause");
    return 0;
    }

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Here ya go:

    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <cmath>
    // this one is for the system library =)
    #include <cstdlib>
    #include <cctype>
    
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    
    int main ()
    {
    
      char LetterGrade;
      cout << "Mini Program 5.2\n";
      cout << "Enter the letter grade:  ";
      cin >> LetterGrade;
      cout << endl;
    
      LetterGrade = toupper(LetterGrade); //This makes sure it is upper case to make checking easier.
      //You can also use tolower..they are in the <cctype> library...but how you are doing it is fine.
    
         if (LetterGrade == 'A' ) //As you see now with toupper even if you input a lower case a this will work
               cout << "a perfect score.  Excellent." << endl; 
    
         else if (LetterGrade == 'B' || LetterGrade == 'b') 
              cout << "well above average.  Keep up the good work. " << endl;
    
         else if (LetterGrade == 'C' || LetterGrade == 'c')
             cout << "average: keep trying you will get there." << endl;
    
         else if (LetterGrade == 'D' || LetterGrade == 'd')
               cout << "below average: seems you need some extra help." << endl;
    
         else if (LetterGrade == 'F' || LetterGrade == 'f')
               cout << "not passing: spend some more time asking questions and make sure you understand the material ahead of time." << endl;
    
         else 
             cout << "LETTER DOES NOT EXIST" << endl;
    
        system("pause");
    return 0;
    }
    Notice the comments in the code about toupper. It is just nice but your method is perfectly fine. Notice how I removed all the switch statements. You do not need them if you are going to use the if else if statements.

    It now works just fine.

    One comment. You include a lot of libraries you do not use. Be careful with that.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should not combine the switch and if. Pick one. I'd pick the if because you've got the code for it already there. If you pick the switch, you have to have a separate case for 'A' and 'a'.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    ok so when you use switch statements you must have a case for A and a?

    im trying to use switch statements without repeating the statements.

    o wait nvm i got it!!!
    Code:
    //adds the iostream library to the program
    #include <iostream>m
    // a little thing you need for this
    #include <cctype>
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    
    int main ()
    {
    
      char LetterGrade;
      cout << "Mini Program 5.2\n";
      cout << "Enter the letter grade:  ";
      cin >> LetterGrade;
      cout << endl;
      LetterGrade = toupper(LetterGrade);
      switch(LetterGrade)
    {
    
    
              case 'A' : cout << "a perfect score.  Excellent." << endl; break;
    
              case 'B' : cout << "well above average.  Keep up the good work. " << endl;break;
    
              case 'C' : cout << "average: keep trying you will get there." << endl;break;
    
              case 'D' : cout << "below average: seems you need some extra help." << endl;break;
    
              case 'F' : cout << "not passing: spend some more time asking questions and make sure you understand the material ahead of time." << endl;break;
    
              default  : cout << "LETTER DOES NOT EXIST" << endl; break;
    }
        system("pause");
    return 0;
    }
    Last edited by findme; 11-17-2005 at 11:08 PM.

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Yes, bit with switch statements you can have the cases together.
    example.
    Code:
    switch (LetterGrade)
    {
         case 'A':
         case 'a': cout << "A perfect score. Excellent" << endl; break:
    
    .......
    }
    So, if you did not do the toupper or to lower, then if they entered either A or a they both get the same data.

    Switch statements are really just very limited if else statements. If else has more flexibility that switch though. Make sure to learn how to properly use if else statements as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grading
    By archriku in forum C Programming
    Replies: 12
    Last Post: 03-25-2009, 08:46 AM
  2. a grading program
    By compugeek915 in forum C Programming
    Replies: 7
    Last Post: 10-19-2007, 12:38 AM
  3. Grading Program pt.II (classes) help...
    By eun-jin in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2006, 04:50 PM
  4. Grading Program Error
    By eun-jin in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2006, 07:45 PM
  5. Grading AP CS Exams
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 06-18-2004, 03:46 PM