# Thread: Simple quiz program problem

1. Originally Posted by laserlight
That is due to integer division. You can cast one of the two operands to float or double as you divide.

What do you mean?

cout<< "You scored, " << correct / total << " percent\n";

to the end, but now it always outputs "you scored 45 percent"

Code:
```#include<iostream>
#include<string>
using namespace std;

int main ()
{
int sub;
int div;
int correct;
int incorrect =0;
int total =3;

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

{
++correct;
}
else
{
{
++incorrect;
}
else
{
if (sub == 0)
{
++correct;
}
else
{
if (sub != 0)
{
++incorrect;
}
else
{
if (div == 5)
{
++correct;
}
else
{
if (div != 5)
{
++correct;
}
}

}
}
}
}
{
cout<< "You scored, " << correct / total << " percent\n";
}
system("Pause");
return 0;
}```

2. Originally Posted by paulb39
What do you mean?
Something like this:
Code:
`cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";`
Before we continue, I suggest that you indent your code properly, e.g.,
Code:
```#include<iostream>
#include<string>

using namespace std;

int main()
{
int sub;
int div;
int correct;
int incorrect = 0;
int total = 3;

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

{
++correct;
}
else
{
{
++incorrect;
}
else
{
if (sub == 0)
{
++correct;
}
else
{
if (sub != 0)
{
++incorrect;
}
else
{
if (div == 5)
{
++correct;
}
else
{
if (div != 5)
{
++correct;
}
}
}
}
}
}

{
cout<< "You scored, " << correct / total << " percent\n";
}
system("Pause");
return 0;
}```
Note that those nested if-else statements would more typically be written as:
Code:
```if (add == 4)
{
++correct;
}
{
++incorrect;
}
else if (sub == 0)
{
++correct;
}
else if (sub != 0)
{
++incorrect;
}
else if (div == 5)
{
++correct;
}
else if (div != 5)
{
++correct;
}```
Either way, with indentation, it is clear that the quiz taker can only get at most 1 question reported as correct. Even if he/she got the other questions correct, they would never be checked for correctness. Likewise, the quiz taker can get at most 1 question reported as incorrect.

3. Also, one more issue.
Code:
```int correct;
...
++correct;```
You should initialize the variable before using it.

4. Originally Posted by _Mike
Also, one more issue.
Code:
```int correct;
...
++correct;```
You should initialize the variable before using it.
This initializes it right?

Code:
```int correct;
int incorrect;
int total;
correct = 0;
incorrect = 0;
total = 3;```

Originally Posted by laserlight
Either way, with indentation, it is clear that the quiz taker can only get at most 1 question reported as correct. Even if he/she got the other questions correct, they would never be checked for correctness. Likewise, the quiz taker can get at most 1 question reported as incorrect.
I don't understand why though.

The way I have it now, it always outputs "your score is 33 percent"

Code:
```#include<iostream>
#include<string>

using namespace std;

int main()
{
int sub;
int div;
int correct;
int incorrect;
int total;
correct = 0;
incorrect = 0;
total = 3;

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

{
++correct;
}
{
++incorrect;
}
else if (sub == 0)
{
++correct;
}
else if (sub != 0)
{
++incorrect;
}
else if (div == 5)
{
++correct;
}
else if (div != 5)
{
++correct;
}
{
cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
}
system("Pause");
return 0;
}```

5. Originally Posted by paulb39
This initializes it right?
No, you should initialise it to 0:
Code:
`int correct = 0;`
Originally Posted by paulb39
I don't understand why though.
Trace the logic. For example, suppose that add == 4. Then ++correct. But the else branch will not be followed, hence the next thing that happens is that the result is printed. But suppose that add != 4. You know that add == 4 will evaluate to false, hence the else branch will be followed. Now, add != 4 evaluates to true, hence ++incorrect. But now the else branch here will not be followed, hence the next thing that happens is that the result is printed. Since either add == 4 or add != 4, there is no way that sub == 0 will ever get checked.

Rather, you should have three separate if-else statements, each handling a question independently of the others.

6. Originally Posted by laserlight
Rather, you should have three separate if-else statements, each handling a question independently of the others.
Woot, thank you, working now

Code:
```#include<iostream>
#include<string>

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;

{
++correct;
}
else
{
++incorrect;
}
if (div == 5)
{
++correct;
}
else
{
++incorrect;
}
if (sub == 0)
{
++correct;
}
else
{
++incorrect;
}
{
cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
}
system("Pause");
return 0;
}```
One question, for

Code:
`cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";`
Could you explain what it means? I don't understand what the "static_cast<int>" part is for, and why does correct need two parentheses in front of if instead of one?

7. Originally Posted by paulb39
I don't understand what the "static_cast<int>" part is for
From what I see, you want to output an int rather than a double, so this a type cast to coerce the double to int. It truncates the double in the process (i.e., the portion after the decimal point is "chopped off"), but other than that it probably does what you want.

Originally Posted by paulb39
and why does correct need two parentheses in front of if instead of one?
Because I chose to use parentheses to group (correct * 100.0). This is actually unnecessary.

8. Originally Posted by laserlight
From what I see, you want to output an int rather than a double, so this a type cast to coerce the double to int. It truncates the double in the process (i.e., the portion after the decimal point is "chopped off"), but other than that it probably does what you want.

Because I chose to use parentheses to group (correct * 100.0). This is actually unnecessary.
Thanks.

Wrote another program, does same sort of thing but I tried to make it a little long/ more complicated.

Got it to work in my first try, which is pretty awesome, but it doesn't calculate the percent correct, it always says "you scored 25 percent"

I'm guessing its because I have int and char variables.

Any ideas?

Code:
```#include<iostream>
#include<string>

using namespace std;

int main()
{
int mole;
char gas_law[7];
int num;
char place_p[11];
char place_v[1];
char place_n[1];
char place_r[1];
char place_t[1];
int correct = 0;
int incorrect = 0;
int total = 8;

cout << "Write a number 1 - 10 ";
cin >> num;
cout << "What is the 16th letter in the alphabet? ";
cin >> place_p;
cout << "What is the 22nd letter in the alphabet? ";
cin >> place_v;
cout << "What is the 14th letter in the alphabet? ";
cin >> place_n;
cout << "What is the 18th letter in the alphabet? ";
cin >> place_r;
cout << "What is the 20th letter in the alphabet? ";
cin >> place_t;
cout << "What is the ideal gas law? (use all lower case) Hint: ^^ " ;
cin >> gas_law;
cout << "How many moles are in 80 grams of Calcium? Hint: calcium is Ca ";
cin >> mole;

if (gas_law == "pv=nrt")
{
++correct;
}
else
{
++incorrect;
}
if (place_p == "p")
{
++correct;
}
else
{
++incorrect;
}
if (place_v == "v")
{
++correct;
}
else
{
++incorrect;
}
if (place_n == "n")
{
++correct;
}
else
{
++incorrect;
}
if (place_r == "r")
{
++correct;
}
else
{
++incorrect;
}
if (place_t == "t")
{
++correct;
}
else
{
++incorrect;
}
if (mole == 2)
{
++correct;
}
else
{
++incorrect;
}
if (num == 3)
{
++correct;
cout << "You guessed the number correctly! Aren't you a genius...\n";
}
else if (num < 3)
{
++incorrect;
cout << "You should of gussed higher for the number!\n";
}
else if (num > 3)
{
cout << "You should of guessed lower for the number!\n";
}
{
cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
}
system("Pause");
return 0;
}```