Oct 31, 2015 at 12:45am
Hello, I've been working on an assignment and I've almost got it. I just can't figure out how to pass a variable to another function.
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
|
void calcCorrectAnswer(char problemType, int num1, int num2, int& answer//I'm trying to send this to the checkAnswer function)
{
switch(problemType)
{
case '+': cout<<num1<<" + "<<num2<<" = ";
answer=num1+num2;
break;
case '-': cout<<num1<<" - "<<num2<<" = ";
answer=num1-num2;
break;
case '*': cout<<num1<<" * "<<num2<<" = ";
answer=num1*num2;
break;
default : cout<<"ERROR";
}
}
void checkAnswer(int num1, int num2, bool& isCorrect)
{
int userAnswer;
cin>>userAnswer;
if(userAnswer==answer//I want this from the calcCorrectAnswer function)
{
isCorrect=true;
cout<<"correct"<<endl;
}
else
{
isCorrect=false;
cout<<"incorrect"<<endl;
}
}
|
Last edited on Oct 31, 2015 at 1:10am
Oct 31, 2015 at 12:47am
You have to pass it back to main then to the new function or create a global variable.
*edit* or you might call the second function from the 1st
Last edited on Oct 31, 2015 at 12:48am
Oct 31, 2015 at 12:49am
I'm not allowed to use global variables and my main function has to be exactly a certain way. So I cannot add to it.
*edit* or you might call the second function from the 1st |
Ok I'll try this.
This crashed my program.
*edit didn't crash it, made it run worse than before.
Last edited on Oct 31, 2015 at 1:18am