Implementing score counter in game.

Right now i have "int score" all over the place in the main() function and the subroutine functions, from trying to make the point system work if the user got the math problem right. All it does is print 1 every time currently and I cant figure out how to actually add up the points.



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

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>

using namespace std;

int menu();
int addition();
int subtraction();




int main()
{



int score;
int choice;

do 
{
choice = menu();


     switch  (choice){
            case 0: cout << score;                     break;
            case 1: addition();                        break;
            case 2: subtraction();                     break;
            default : cout << "Error in input\n\n";    break;
    }
    }
while (choice != 0);
}


int menu() //main menu that gets called by the switch
{
    int MenuChoice;
        cout << "\n";
        cout << "Enter 1 for addition\n";
        cout << "Enter 2 for Subtraction\n";
        cout << "Enter 0 to quit\n";
        cout << "-----------------\n";
        cout << "Choice: ";
    cin >> MenuChoice; // user input
    return MenuChoice; //return the choice to choice to select a case
}



int addition() //function that is called by the users selection
{
    int score=0;
    int answer;
    int random1, random2;



srand(time(NULL));

random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99


    cout << "\n" << random1 << " + " << random2 << " = "; //code to format the random math problem
    cin >> answer; // user input of answer
{




if (answer == (random1+random2)){
        cout << "Congratulations\n\n";
        ++score;
        cout << "\n" << score;}   //if user is correct than the score is incremented
    else{
        cout << "Incorrect\n\n";} //if not than the score is not incremented

return score;

}
}



int subtraction() // follows same model as previous function, but instead subtraction.
{


    int answer;
    int random1, random2;
    int score=0;



srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99



    cout << "\n" << random1 << " - " << random2 << " = ";
    cin >> answer;


    if (answer == (random1-random2)){
        cout << "Congratulations\n\n";
        ++score;
        cout << "\n" << score;}
          else{
            cout << "Incorrect\n\n";}

return score;

}



Last edited on
Your code is kinda hard to read, but this is what you'd do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    #include <iostream>
    
    using namespace std;

    int main()
    {
        int Reward = 5; Score = 2;
        
        cout << "Score: " << Score; // Score will be 2.
        // Some code that asks questions etc.
        if(/*Answer is correct*/){
            Score += Reward; // Score = Score + Reward.
        }
        cout << "Score: " << Score; // Score will be 7.
    }
well yes, but how do i apply that code into my main menu loop and functions. I kind of have what you are saying in my addition and subtraction functions already, where i increment the score ++score and print it if you are correct. But it wont save the value at all, just prints 1.
I think this a problem of scope.
http://www.cplusplus.com/doc/tutorial/variables/

The functions addition() and subtraction() have their own local variable "score" that gets destroyed when the functions finish. Everytime main() calls either one of them, their local "score" is set to zero, incremented to 1 if applicable, that value is returned to main(), and the local variable "score" is lost (out of scope) . If main() happens to call either function again, the local variables are used, incremented to 1 if applicable, value returned to main(), then forgotten once again.

There are a few ways to correct this: one is to use a global variable, or you could pass main()'s "score" to the functions.
This should cover everything I think:

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
    #include <iostream>

    using namespace std;

    bool Addition(int &Reward);
    bool Subtraction(int &Reward);

    int main()
    {
        bool Result, Finished = false;
        int Choice, Reward, Score = 0;
        
        // srand(time(0));
        // ..goes here and only here if using random numbers!
        // Remember the #include <ctime>

        while(!Finished) // while finished is not true
        {
            cout << "\nScore: " << Score;
            cout << "\n\nChoose a math problem:\n1) Addition\n2) Subtraction\n3) Quit\n> ";
            cin >> Choice;

            switch(Choice)
            {
                case 1: Result = Addition(Reward);     break;
                case 2: Result = Subtraction(Reward);  break;
                case 3: Finished = true;               break;
                default: continue;                     break;
            }

            if(Result == true){
                Score += Reward;
            }
        }
    }

    bool Addition(int &Reward)
    {
        int Answer, Num1 = 3, Num2 = 5;
        Reward = 10; // set reward for addition problems

        cout << "\nWhat is " << Num1 << " + " << Num2 << "?:\n> ";
        cin >> Answer;

        if(Answer == (Num1 + Num2)){
            cout << "\nCorrect!\n";
            return true;
        } else {
            cout << "\nTry again.\n";
            return false;
        }
    }

    bool Subtraction(int &Reward)
    {
        int Answer, Num1 = 10, Num2 = 3;
        Reward = 15; // set reward for subtraction problems

        cout << "\nWhat is " << Num1 << " - " << Num2 << "?:\n> ";
        cin >> Answer;

        if(Answer == (Num1 - Num2)){
            cout << "\nCorrect!\n";
            return true;
        } else {
            cout << "\nTry again.\n";
            return false;
        }
    }

If you don't want to give a specific reward for each kind of problem, remove every instance of Reward from the code and replace:

1
2
3
            if(Result == true){
                Score += Reward;
            }

.. with:

1
2
3
            if(Result == true){
                Score += 1;
            }

Just ask if anything is unclear.
Last edited on
Topic archived. No new replies allowed.