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 add;
int sub;
int div;
int correct;
int incorrect = 0;
int total = 3;
cout << "Whats 2 + 2? ";
cin >> add;
cout << "Whats 2 - 2? ";
cin >> sub;
cout << "Whats 20 / 4? ";
cin >> div;
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;
}
}
}
}
}
}
{
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;
}
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;
}
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.