Thread: Simple quiz program problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    13

    Simple quiz program problem

    I'm extremely new to c++.

    I've been reading the tutorials and decided to write my own little program

    I want the program to ask a couple of questions, and based on your answers, output your score as a percentage.

    I was able to write part of it, but I don't know how to complete it.

    I can't figure out how to get the program to see how many you got right, then calculate your score.

    Such as, if I got two right, I want it to say "your score is 66%" Or if I just got one right, "your score is 33%"

    How would I go about doing this?

    Heres the code I have so far.

    Code:
    #include<iostream>
    using namespace std;
    
    int main ()
    {
        int add;
        int sub;
        int div;
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
    
    if (add == 4 && sub == 0 && div == 5)
    {
       cout << "Your score is 100%! \n ";
       }
       else
       {
           cout << "You got one wrong :( \n";
       }
    system("Pause");
    return 0;
    }

  2. #2
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    first off if you're going to do it this way, you will have to satisfy the rest of the possible answers in your if statements. You have one for your 100% answer. Think of the other scenarios that you have. and give that a try

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Create a counter in lieu of the numerous if statements. IE., int counter=0; Each time an answer is correct, do ++counter; At the end of your Q & A, compare the counter count to the number of questions asked.

    So, if you asked 3 and your counter say you've answered 1 correct, well, do the math. There's an operand that can compute the percentage.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by todouble22 View Post
    first off if you're going to do it this way, you will have to satisfy the rest of the possible answers in your if statements. You have one for your 100% answer. Think of the other scenarios that you have. and give that a try
    Ah, thanks, something like this you mean?

    Code:
    #include<iostream>
    using namespace std;
    
    int main ()
    {
        int add;
        int sub;
        int div;
        int counter=0
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
    
    if (add == 4 && sub == 0 && div == 5)
    {
      cout << "Your score is 100%!\n" ;
      }
    else
    {
         if (add == 4 && sub == 0)
          {
           cout << "Your score is 66%! \n";
           }
         else
          {
              if (add == 4)
              {
                cout << "Your score is 33%! \n";
                }
              else
              {
                   if (add == 4 && div == 5)
                   {
                     cout << "Your score is 66%! \n";
                     }
                   else
                   {
                        if (div == 5)
                        {
                          cout << "Your score is 33%! \n";
                          }
                        else
                        {
                            if (sub == 0)
              {
                cout << "Your score is 33%! \n";
                }
              else
              {
                   if (sub == 0 && div == 5)
                   {
                     cout << "Your score is 66%! \n";
                     }
                     else
                     {
                             cout << "Your score is 0%!\n";
                             }
                        }
                   }
              }
         }
    system("Pause");
    return 0;
    }
    }
    }
    Quote Originally Posted by Oldman47 View Post
    Create a counter in lieu of the numerous if statements. IE., int counter=0; Each time an answer is correct, do ++counter; At the end of your Q & A, compare the counter count to the number of questions asked.

    So, if you asked 3 and your counter say you've answered 1 correct, well, do the math. There's an operand that can compute the percentage.
    Sorry for my nubness, I don't understand what you mean, do you mean something like this?

    Code:
    #include<iostream>
    using namespace std;
    
    int main ()
    {
        int add;
        int sub;
        int div;
        int counter = 0;
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
        
    if (add == 4)
    {
        ++counter;
      }
    else
    {
         if (sub == 0)
          {
           ++counter;
           }
         else
          {
              if (add == 4)
              {
                ++counter;
                }
              else
              {
                   if (div == 5)
                   {
                    ++counter;
                     }
                     else
                     {
                             cout << "Your score is 0%!\n";
                             }
                        }
                   }
              }
    
    system("Pause");
    return 0;
    }
    Only I don't know how to use the counter to output the percentage though.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Well, I was thinking more along the lines of what's below. Before doing a board search, try to figure out percentages on your own.

    Code:
    int num_correct=0; //initialize a counter for correct answers
    int answer;
    
     void Check_Qs(int a)  //function to check user response against answers
       {
        int add_return = 4;  //correct addition total
        int sub_return = 0;  //correct subtraction total
        int div_return = 5;  //correct division answer
    
    
       if(add_return==a || sub_return==a || div_return == a){  //condense all if/else statements
         ++num_correct;           //if corrrect, score one for the user
         cout<<"correct! \n";
        }
         else {
          cout<<"wrong! \n" ;  //if wrong,  no score added - inform user he/she is wrong
        }
       }
    
    
    
    int main()
     {
        cout << "Whats 2 + 2? ";
        cin >>answer;
        Check_Qs(answer);  //call function.  compare user answer against add_return value
        
            cout << "Whats 2 - 2? ";
            cin >> answer;
            Check_Qs(answer); //call function. compare user answer against sub_return value
        
        cout << "Whats 20 / 4? ";
        cin >> answer;
        Check_Qs(answer);  // call function. compare user answer against div_return value
        
        cin.ignore();  //ignore input so program doesn't close immediately
        cin.get();
          return 0;
         }
    Last edited by Oldman47; 03-04-2010 at 05:52 PM. Reason: comment

  6. #6
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    oldman, the user paulb39 is a noob. you're giving him examples of functions and calling to them within your main method.. do you seriously think that will get what you're doing without even knowing what they are?

  7. #7
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    I'm not attempting to discredit your assistance to the user but in the beginning it can be a bit daunting and at least as i've noticed its best to fully explain

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by Oldman47 View Post
    Well, I was thinking more along the lines of what's below. Before doing a board search, try to figure out percentages on your own.

    -snip-
    Thanks, tho any chance can you comment that code, I can't understand any of it ;(
    I'm really new to c++, I'd like to learn what your thought process was when you wrote that.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Commented. Basically, I was trying to do away with most of your if/else statements and the function was created for that reason, along with keeping the code compact.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by Oldman47 View Post
    Commented. Basically, I was trying to do away with most of your if/else statements and the function was created for that reason, along with keeping the code compact.
    Thanks, tho that is like my original code, doesn't give a percentage based on how many you got right.

  11. #11
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    Quote Originally Posted by paulb39 View Post
    Thanks, tho that is like my original code, doesn't give a percentage based on how many you got right.
    do you want a method to calculate percentage or just display in this example? as it stands now:
    Code:
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    int num_correct=0; //initialize a counter for correct answers
    
    
    
     void Check_Qs(int a)  //function to check user response against answers
       {
        int add_return = 4;  //correct addition total
        int sub_return = 0;  //correct subtraction total
        int div_return = 5;  //correct division answer
     
    
    
       if(add_return==a || sub_return==a || div_return == a){  //condense all if/else statements
         ++num_correct;           //if corrrect, score one for the user, this will increment the number for each correct answer
         cout<<"correct! \n";
        }
         else {
          cout<<"wrong! \n" ;  //if wrong,  no score added - inform user he/she is wrong
        }
     }
     
       
    
    
    
     int main(){
    	 int answer;
    
    
    
        cout << "Whats 2 + 2? ";
        cin >>answer;
        Check_Qs(answer);  //call function.  compare user answer against add_return value
        
            cout << "Whats 2 - 2? ";
            cin >> answer;
            Check_Qs(answer); //call function. compare user answer against sub_return value
        
        cout << "Whats 20 / 4? ";
        cin >> answer;
        Check_Qs(answer);  // call function. compare user answer against div_return value
    
    	//this portion below will use the number derived from the counter and write out 
    	//the percentage based on answers correct
    	if (num_correct == 3){
    	cout <<"you got " <<num_correct << " out of 3 correct which is 100%"<<endl;
    	}
    	else if (num_correct == 2){
    	cout <<"you got " <<num_correct << " out of 3 correct which is 66%"<<endl;
    	}
    	else if (num_correct == 1){
    	cout <<"you got " <<num_correct << " out of 3 correct which is 33%"<<endl;
    	}
    return 0;
    }

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    You can eliminate those if/else statements with a little math, bub.

    Code:
         int percentage=num_correct*100/3;
        cout<<"You've scored "<<percentage<<" on this quiz";
    Also, the function I wrote isn't foolproof. If for instance, you give the answer 0 for what is 2+2 the function would still return correct. It wasn't well thought out, just something I typed in on the fly. I also just typed in your percentage code, so you'd have too verify accuracy.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by todouble22 View Post
    do you want a method to calculate percentage or just display in this example? as it stands now:
    Code:
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    int num_correct=0; //initialize a counter for correct answers
    
    
    
     void Check_Qs(int a)  //function to check user response against answers
       {
        int add_return = 4;  //correct addition total
        int sub_return = 0;  //correct subtraction total
        int div_return = 5;  //correct division answer
     
    
    
       if(add_return==a || sub_return==a || div_return == a){  //condense all if/else statements
         ++num_correct;           //if corrrect, score one for the user, this will increment the number for each correct answer
         cout<<"correct! \n";
        }
         else {
          cout<<"wrong! \n" ;  //if wrong,  no score added - inform user he/she is wrong
        }
     }
     
       
    
    
    
     int main(){
    	 int answer;
    
    
    
        cout << "Whats 2 + 2? ";
        cin >>answer;
        Check_Qs(answer);  //call function.  compare user answer against add_return value
        
            cout << "Whats 2 - 2? ";
            cin >> answer;
            Check_Qs(answer); //call function. compare user answer against sub_return value
        
        cout << "Whats 20 / 4? ";
        cin >> answer;
        Check_Qs(answer);  // call function. compare user answer against div_return value
    
    	//this portion below will use the number derived from the counter and write out 
    	//the percentage based on answers correct
    	if (num_correct == 3){
    	cout <<"you got " <<num_correct << " out of 3 correct which is 100%"<<endl;
    	}
    	else if (num_correct == 2){
    	cout <<"you got " <<num_correct << " out of 3 correct which is 66%"<<endl;
    	}
    	else if (num_correct == 1){
    	cout <<"you got " <<num_correct << " out of 3 correct which is 33%"<<endl;
    	}
    return 0;
    }
    Calculate it, not just display

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    I took a different approach, made it add to "incorrect" and "correct" then in end divide "correct" by "total"

    It compiles fine, but for some reason ,it always says "you scored 0 percent"

    Any ideas?

    Code:
    #include<iostream>
    using namespace std;
    
    int main ()
    {
        int add;
        int sub;
        int div;
        int correct =0;
        int incorrect =0;
        int total =3;
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
        std::cout<< "You scored, " << correct / total << " percent\n";
    
    if (add == 4)
    {
      ++correct;
      }
    else
    {
         if (add != 4)
          {
           ++incorrect;
           }
         else
          {
              if (sub == 0)
              {
                ++correct;
                }
              else
              {
                   if (sub != 0)
                   {
                     ++incorrect;
                     }
                   else
                   {
                        if (div == 5)
                        {
                          ++correct;
                          }
                        else
                        {
                            if (div != 5)
              {
                ++correct;
                }
                                       }
    
                        }
                   }
              }
         }
    system("Pause");
    return 0;
    }

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by paulb39
    It compiles fine, but for some reason ,it always says "you scored 0 percent"
    That is due to integer division. You can cast one of the two operands to float or double as you divide.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with a simple program
    By alfredd in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2009, 03:48 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM