Need help on collecting the number of right answers in math quiz program

Hi, I am working on an RPG math game that collects the number of corrected answers then convert them in to Experience Points for the leveling system. I have just complete with the math problem generator; however, when running the program I cannot get it the correct answer counter to keep track of the number of answers that are correct. It will only display a constant value of "1" and would not change even if the first answer is incorrect.

Here is the function that's suppose to count the number of correct answers
Note: The function runs in for loop in int main();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int answerCorrectorPlayer1(int userInput[][2], int answerAdd[]){
    int num_of_correct_answers = 0, valueHolder = 0;
    
    //Cycles to all of the answers for correction
    for (int answerChecker = 0; answerChecker <= 5; answerChecker++){
            
             userInput[answerChecker][1];
             
             //answerAdd is the variable that holds the correct answer
             //Corrects and counts the amount of answers correct
             if (userInput[answerChecker][1] == answerAdd[answerChecker]){ 
             
                 num_of_correct_answers = valueHolder + 1;
             }
             // If the an answer is wrong no point is added
             else if (userInput[answerChecker][1] != answerAdd[answerChecker]){
                 num_of_correct_answers = valueHolder + 0;
             }
            
    }
    
return num_of_correct_answers;}



And here is the entirety of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* 
 * File:   main.cpp
 * Author: Kevin Vo
 *
 * Created on May 31, 2012, 4:09 PM
 */

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <ctime>

using namespace std;


int getRounds();//iterates the game by number of rounds  
int answerCorrectorPlayer1(int[][2], int[]);
int ExpSystemPlayer1(int);
int MaxEXPplayer1(int);
int player1_Lvl_status(int);


int main(int argc, char** argv) {
    
    int NUMofRounds; 
    string arrName[2];
    const int cycles_problems = 6, cycles_name = 2;
    int userInput[cycles_problems][cycles_name]; 
    //Player 1's Variables
    int Player1_Current_EXP, AMOUNT_Correct_Answers_Player1, Player1MAX_exp, 
            player1_Level_Status;
    
    
    //variables for the number generator
    int randTopNum, randBotNum, answerAdd[cycles_problems], answerMult;
    
    //Define variables to functions
     NUMofRounds = getRounds();
     
     //Requests for the Usernames
     for (int user = 0; user <= 1; user++){
     cout<<"Please enter username for player # "<<(user + 1)<<": ";
     cin>>arrName[user];
     }
     
     for (int player = 0; player <= 1; player++){  
         for (int round = 1; round <= NUMofRounds; round++){
             
             //Displays the player's turn and what round 
                if(player == 0){
                        cout<<"Player: "<<arrName[player]<<endl;
                }
                else if (player == 1){
                        cout<<"Player: "<<arrName[player]<<endl;
                }
                
                        for (int Prob = 0; Prob <= 5; Prob++){
                            
                                //Random Number Generator
                                unsigned seed = time(0);
    
                                srand(seed);
    
                                randTopNum = rand()%10;
                                randBotNum = rand()%10;
   
                                answerAdd[Prob] = randTopNum + randBotNum;//Answer for addition problems
                                answerMult = randTopNum * randBotNum;//Answer for multi. problems
                                
                                //Displays the math problem   
                                cout<<randTopNum<<" + "<<randBotNum<<" = ";
                
                                //Requests answers of both players  
                                cin>>userInput[Prob][player];
                                
                                //Corrects the inputted answers
                                AMOUNT_Correct_Answers_Player1 = answerCorrectorPlayer1(userInput, answerAdd);
                                cout<<"# of Player1's correct answers: "<<AMOUNT_Correct_Answers_Player1<<endl;
                                //Collects ansewrCorrector's result  and convert them into EXP
                                Player1_Current_EXP = ExpSystemPlayer1(AMOUNT_Correct_Answers_Player1);
                                cout<<"Current EXP: "<<Player1_Current_EXP<<endl;;
                                //Maximum Experience Points for Player 1
                                Player1MAX_exp = MaxEXPplayer1(Player1_Current_EXP);
                                cout<<"Player 1 Max EXP: "<<Player1MAX_exp<<endl;
                                //Player1's Level Calculator
                                player1_Level_Status = player1_Lvl_status(Player1MAX_exp);
                                cout<<"Player 1's Level: "<<player1_Level_Status<<endl;
                 
                }
         }
     }
    
     cout<<userInput[0][0]<<endl;
     cout<<userInput[1][0]<<endl;
     cout<<userInput[2][0]<<endl;
     cout<<userInput[3][0]<<endl;
     cout<<"answerCorrect: "<<answerCorrectorPlayer1(userInput, answerAdd)<<endl;
    return 0;
}



int getRounds(){
    int numRounds = 1;

return numRounds;}

int answerCorrectorPlayer1(int userInput[][2], int answerAdd[]){
    int num_of_correct_answers = 0, valueHolder = 0;
    
    //Cycles to all of the answers for correction
    for (int answerChecker = 0; answerChecker <= 5; answerChecker++){
            
             userInput[answerChecker][1];
             
             //Corrects and counts the amount of answers correct
             if (userInput[answerChecker][1] == answerAdd[answerChecker]){
             
                 num_of_correct_answers = valueHolder + 1;
             }
             // If the an answer is wrong no point is added
             else if (userInput[answerChecker][1] != answerAdd[answerChecker]){
                 num_of_correct_answers = valueHolder + 0;
             }
            
    }
    
return num_of_correct_answers;}


int ExpSystemPlayer1(int AMOUNT_Correct_Answers_Player1){
    int Player1EXPstatus = 0;
    
    Player1EXPstatus = AMOUNT_Correct_Answers_Player1 * 250;//1500 exp is the max
    
return Player1EXPstatus;}

int MaxEXPplayer1(int Player1_Current_EXP){
    int max_EXP_of_player_1;
    
    if (Player1_Current_EXP > 500){
        max_EXP_of_player_1 = 500;
    }
    else if (Player1_Current_EXP == 500){
        max_EXP_of_player_1 = 1000;
    }
    else if(Player1_Current_EXP == 1000){
        max_EXP_of_player_1 = 1500;
    }
return max_EXP_of_player_1;}

int player1_Lvl_status(int Player1MAX_exp){
    int player1_Level;
    
    if (Player1MAX_exp == 500){
        player1_Level = 1;
    }
    else if (Player1MAX_exp == 1000){
        player1_Level = 2;
    }
    else if (Player1MAX_exp == 1500){
        player1_Level = 3;
    }

return player1_Level;}


Last edited on
The problem is in your for loop.

Notice that after each answer the player answers, the correct answers are counted.

I'm assuming you want the player to answer all five questions then display the number correct.

So, have the problems in the for loop, and have the display after the for loop.

1
2
3
4
5
6
7
for(problems)
{
   generate problem
   have player answer it
}
calculate number of answers correct
display desired information (including numbers of information


Also, taking a look at your other for loops, it looks like you have the first two for loops reversed.

I'm assuming you want each player to go in each round, and then the next round is executed for the total number of rounds. Right now you have it so that each player goes one at a time for all of his rounds... try this instead:
1
2
3
4
5
6
7
8
9
10
11
for( rounds )
{
   display round #
   for( players )
   {
    display player's name
      for( problems )
      {generate problem; have player answer problem;}
    display player's score
   }
}
I figured out the issue it had to do with the "answerAdd[]" not storing the correct value from the random number generator, thanks for the help.
Topic archived. No new replies allowed.