Eric Cuevas
Eric Cuevas

Reputation: 63

Trouble getting percentages in C++

C++ beginner here. I'm trying to replicate this output and I'm almost there but I'm having trouble with the last part (the quiz percentage).

 Example    output:
 Welcome to our math quiz program! Please enter the answers to the
 following questions:
 5! = 5
 (2^(2^(2^2))) = 65536
 3 * (4 + 8) / ((4 * 2) / (5 – 1)) = 18
 3 + 2 + 1 + 2 + 3 = 11
 Number of correct answers: 3
 Number of incorrect answers: 1
 Quiz percentage: 75%

Here is my code so far:

int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;

int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;

int guessOne;
int guessTwo;
int guessThree;
int guessFour;

cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;

switch (guessOne) {
    case 120:
        numCorrect++;
        break;
    default:
        numIncorrect++;
}

switch (guessTwo) {
case 65536:
    numCorrect++;
    break;
default:
    numIncorrect++;
}

switch (guessThree) {
case 18:
    numCorrect++;
    break;
default:
    numIncorrect++;
}

switch (guessFour) {
case 11:
    numCorrect++;
    break;
default:
    numIncorrect++;
}

cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect / possibleCorrect) * 100 << "%" << endl;
cout << endl;

return 0;
}

It seems like when I answer all the questions correctly I get 100% as I should, however, when I score anything but that (only 1, 2, or 3 correct), I end up getting a 0% as a result. I thought I was doing the percentage calculation correctly but the output is suggesting otherwise. Can anybody point me in the right direction? Thanks.

EDIT: Added breaks to switches (however, not root of problem).

Upvotes: 0

Views: 1887

Answers (2)

MegaMind
MegaMind

Reputation: 671

Break statements are missing from your switch(s).

Plus you need to type cast the result to float as all the variables are integers. i suggest you use:

float perc;

perc = (numCorrect/possibleCorrect)*100;

Upvotes: 1

George Houpis
George Houpis

Reputation: 1729

You need a break if you switch, and multiply by 100.0 first to implicitly promote the first expression to a double and then divide:

int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;

int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;

int guessOne;
int guessTwo;
int guessThree;
int guessFour;

cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;

switch (guessOne) {
case 120:
    numCorrect++;
    break;
default:
    numIncorrect++;
}

switch (guessTwo) {
case 65536:
    numCorrect++;
    break;
default:
    numIncorrect++;
}

switch (guessThree) {
case 18:
    numCorrect++;
    break;
default:
    numIncorrect++;
}

switch (guessFour) {
case 11:
    numCorrect++;
    break;
default:
    numIncorrect++;
}

cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;

return 0;
}

For this purpose though, a simple if/else would be easier to read:

int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;

int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;

int guessOne;
int guessTwo;
int guessThree;
int guessFour;

cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
if( guessOne == 120 ) ++numCorrect; else ++numIncorrect;
if( guessTwo == 65536 ) ++numCorrect; else ++numIncorrect;
if( guessThree == 18 ) ++numCorrect; else ++numIncorrect;
if( guessFour == 11 ) ++numCorrect; else ++numIncorrect;

cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;

return 0;
}

Upvotes: 0

Related Questions