# Simple quiz program problem

This is a discussion on Simple quiz program problem within the C++ Programming forums, part of the General Programming Boards category; I'm extremely new to c++. I've been reading the tutorials and decided to write my own little program I want ...

1. ## 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 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 sub;
int div;

cout << "Whats 2 + 2? ";
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. 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. 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. Originally Posted by todouble22
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 sub;
int div;
int counter=0

cout << "Whats 2 + 2? ";
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
{
{
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;
}
}
}
Originally Posted by Oldman47
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 sub;
int div;
int counter = 0;

cout << "Whats 2 + 2? ";
cout << "Whats 2 - 2? ";
cin >> sub;
cout << "Whats 20 / 4? ";
cin >> div;

{
++counter;
}
else
{
if (sub == 0)
{
++counter;
}
else
{
{
++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. 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

void Check_Qs(int a)  //function to check user response against answers
{
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? ";

cout << "Whats 2 - 2? ";

cout << "Whats 20 / 4? ";

cin.ignore();  //ignore input so program doesn't close immediately
cin.get();
return 0;
}

6. 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. 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. Originally Posted by Oldman47
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. 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. Originally Posted by Oldman47
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. Originally Posted by paulb39
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 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(){

cout << "Whats 2 + 2? ";

cout << "Whats 2 - 2? ";

cout << "Whats 20 / 4? ";

//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. 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. Originally Posted by todouble22
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 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(){

cout << "Whats 2 + 2? ";

cout << "Whats 2 - 2? ";

cout << "Whats 20 / 4? ";

//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. 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 sub;
int div;
int correct =0;
int incorrect =0;
int total =3;

cout << "Whats 2 + 2? ";
cout << "Whats 2 - 2? ";
cin >> sub;
cout << "Whats 20 / 4? ";
cin >> div;
std::cout<< "You scored, " << correct / total << " percent\n";

{
++correct;
}
else
{
{
++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. 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.

Page 1 of 2 12 Last