Functions: Code works, but how?

Hello:

My final program for Math Tutor works...and I’m not 100% why. I’m just wondering if you can tell me how the parameters and arguments "numCorrect1," numCorrect2, and "numCorrect3" are receiving the number of correct answers in each set? It looks like their values are coming from a different variable "numCorrect," but I don’t know how because it seems like there’s not enough information to do this.

e.g.
nowhere in the program do I declare:
numCorrect1 = numCorrect or
numCorrect2 = numCorrect or
numCorrect3 = numCorrect


Also, I don’t know how "numCorrect" can be working correctly when I never even set its beginning value anywhere in the program. Usually, I set counts to "0" to start with. As you can see, "numCorrect1," numCorrect2, and "numCorrect3" were set to "0" in "int main()".

Thanks for any feedback...

loloA

[snippet removed]
Last edited on by admin

Also, I don’t know how "numCorrect" can be working correctly when I never even set its beginning value anywhere in the program. Usually, I set counts to "0" to start with. As you can see, "numCorrect1," numCorrect2, and "numCorrect3" were set to "0" in "int main()".


You "declared" numCorrect as a reference parameter of
1
2
3
void doOneSet(char mathOp, int probsPerSet, int& numCorrect);
void doOneProblem(char mathOp, int maxNum, int& numCorrect);
void checkAnswer(int userAnswr, int correctAnswr, int& numCorrect);


When you call this function
 
doOneSet('+', probsPerSet, numCorrect1);


It calls this function:

 
void checkAnswer(int userAnswr, int correctAnswr, int& numCorrect)



By doing that,
1
2
3
4
5
6
7
8
9
10
void checkAnswer(int userAnswr, int correctAnswr, int& numCorrect)
{
    if(userAnswr == correctAnswr){
            cout << "correct"; 
            numCorrect = numCorrect + 1; 
    }else if(userAnswr != correctAnswr){
            cout << "incorrect";
    } 
    cout << endl;
}


You changed the value of numCorrect which in turn, change the value of its alias: numCorrect1


Correct me if Im wrong.
Your code works just fine.
And how is that possible that you wrote your code without understanding how it works?
Last edited on
Topic archived. No new replies allowed.