Thread: I need guidance.

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    I need guidance.

    This is the assignment.

    Purpose:
    The purpose of this assignment is for you to demonstrate your ability to design and code a program that employs a multi-decision construct such as else...if, or switch to resolve a problem that has a multiplicity of conditions to be evaluated. Use the else...if for question 1; switch for question 2.
    Specifications:
    Professor C. N. Fusing administered an exam in which there were only two questions. The questions were multiple-choice with five options each. To test the students reasoning, none of the answers were incorrect but were intended to determine a student’s depth of understanding of the material. Each answer had a different point value with 10 as the maximum and 5 the minimum. They were weighed as follows:
    Question 1 – A = 5, B = 7, C = 10, D = 5, E = 8;
    Question 2 – A = 10, B = 5, C = 8, D = 7, E = 5.
    Should a student fail to answer a question, the professor will enter an F to indicate such. An F answer gets a score of zero (0). To avoid the problem of upper and lower case, use the toupper function to convert the input character. If the answer entered for any question is not between A and F, display an error message, and then end the program.
    The professor needs a program to help him calculate the total score and letter grade for a student. The letter grade is to be assigned as follows:
    18 – 20 = A; 16 – 17 = B; 14 – 15 = C; 10 – 13 = D; 0 – 9 = F
    Design, code, compile and execute a program to supply the professor with the results he needs. Display the student’s first and last name, the answers to the questions, total score, and letter grade for the student’s pair of answers.


    I've worked on this for several days now and have redone several codes but I still can not get it. Can some assist me in helping me correct my mistakes?

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
       
    
        string firstname, lastname;
        char ans1, ans2;
        int score;
        char A,B,C,D,E,F;
        
        
     
     
    
        cout << "Enter student's first and last name:" <<endl;
        cin >> firstname >> lastname;
        
        cout << "Enter answer to question 1:";
        cin >> ans1;
    
    if (ans1== A){
              A =5;
              }
              else if (ans1== B){
                   B = 7;
                   }
                   else if (ans1 == C){
                        C = 10;
                        }
                         else if (ans1 == D){
                              D = 5;
                              }
                               else if (ans1 == E){
                                    E = 8;
                                    }
                                    else if (ans1 ==F){
                                     F==0;
                                     } 
                                     else
                                           cout << "\nInvalid!!" <<endl;
                                           cout << "Enter answer for question 1:";
                                           cin >> ans1;
    				                         
    
                                   
      
          cout << "Enter answer to question 2:";
        cin >> ans2;
        
    switch (ans2){
      case 1:
        A = 10;
        break;
      case 2:
        B = 5;
        break;
      case 3:
        C = 8;
        break;
      case 4:
        D = 7;
        break;
      case 5:
        E = 0;
        break;
      case 6:
        F = 0;
        break;
      
       default:
         cout << "Invalid!!"<<endl;
         }
         cout << "Enter answer for question 2:";
         cin >> ans2;
          
    
    
    
         
       
    cout << "Student's name:" <<endl;
    cin >> firstname >> lastname;
    cout << "Answers:";
    cin >> ans1 >> ans2;
    cout << "Score:";
    score=ans1+ans2;
    
    cout <<score<<endl;
    
    cout << "Grade";
       cin >> score;
       if (score >= 18-20 )
          cout << "Your grade is an A" << endl;
       else if (score >= 16-17 )
          cout << "Your grade is a B" << endl;
       else if (score >= 14-15 )
          cout << "Your grade is a C" << endl;
       else if (score >= 10-13)
          cout << "Your grade is a D" << endl;
       else 
          cout << "Your grade is an F";
    
     system ("pause");
     return EXIT_SUCCESS;
    
    }

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    char A, B, ... is a misunderstanding. Instead of doing that, do it like this:
    Code:
    if (ans1== 'A'){ // Notice the '', that denotes a character.  This checks whether answer contains a value equal to the ASCII value of the letter A, which is want you are trying to do.
    ...
    This is just wrong:
    Code:
    switch (ans2){
      case 1:
        A = 10;
        break;
      case 2:
        B = 5;
        break;
      case 3:
        C = 8;
        break;
      case 4:
        D = 7;
        break;
      case 5:
        E = 0;
        break;
      case 6:
        F = 0;
        break;
      
       default:
         cout << "Invalid!!"<<endl;
         }
    Instead of all those if and stuff, just get the inputted grade into a variable with a meaningful name like "grade", and do a switch with it:
    Code:
    switch(grade)
    {
         case 'A':
                 // crap to do when grade is A
                 break;
        ...
    }

    Get it, does that help? Don't just copy what I said based on my directions. Ask about anything you don't get.
    Last edited by User Name:; 10-21-2010 at 10:32 PM.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    1) You define A, B, C etc as characters and then you have statements like:
    Code:
    A = 5;
    Which won't cause any compile or runtime errors but it is very bad programming practice. You aren't even typecasting the '5' to type character. I won't explain why, but try making a separate program and running this code, it shouldn't take you long to realise why it is bad practice:
    Code:
    int i=0;
    for(i=0; i < 256; i++)
    {
          printf("%d. %c ; ", i, i);
    }
    2) In your if statements you have said if( ans1 == 'A' ) etc, and then say A = 5 etc, but then don't do anything with the variable 'A'. Worse still you reassign a new value to A in switch statement which comes later (this is bad because you completely lose any information you had about 'A' in the previous part).

    3) After you ask for the student's score and you do a whole load of absolutely useless logic (see point above), all you do is add up the scores in this line:
    Code:
    score=ans1+ans2;
    And then compare score with the different grade brackets to find out what their overall grade is, which is really silly because by that isn't what the assignment asks you to do (you are suppose use the weightings given to determine which grade the student should be awarded overall).

    4) In the following line of code:
    Code:
    score=ans1+ans2;
    You seem to completely neglect the fact that ans1 and ans2 are in fact characters. Need I say more?

    5) For some reason you ask for the student's name and results twice which seems a little unnecessary.

    Those are the errors I can spot off the cuff. There may well be more I haven't seen.
    Last edited by Swarvy; 10-21-2010 at 10:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need small guidance in my networking journey
    By Netman in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-12-2010, 11:14 AM
  2. Need further guidance
    By jlimz in forum C Programming
    Replies: 3
    Last Post: 07-26-2010, 06:34 PM
  3. Guidance, please.
    By mattagrimonti in forum C Programming
    Replies: 2
    Last Post: 11-26-2008, 08:50 AM
  4. need guidance to connect to serial port
    By gnychis in forum Linux Programming
    Replies: 1
    Last Post: 06-02-2005, 10:10 AM
  5. advice and possibly guidance
    By nazri81 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2002, 10:19 PM