Void functions. How do I use a variable from one function in another?

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
Oct 31, 2015 at 1:20am
closed account (E0p9LyTq)
Quick and dirty:

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
void checkAnswer(int& answer, bool& isCorrect);

void calcCorrectAnswer(char problemType, int num1, int num2, int& answer, bool& isCorrect) //I'm trying to send this to the checkAnswer function
{
   switch(problemType)
   {
   case '+':
      std::cout << num1<<" + "<<num2 << " = ";
      answer = num1 + num2;
      break;

   case '-':
      std::cout << num1 << " - "<<num2<< " = ";
      answer = num1 - num2;
      break;

   case '*':
      std::cout << num1<< " * " << num2 << " = ";
      answer = num1 * num2;
      break;

   default :
      std::cout << "ERROR";
   }

   checkAnswer(answer, isCorrect);
}

void checkAnswer(int& answer, bool& isCorrect)
{
   int userAnswer;
   std::cin >> userAnswer;

   if(userAnswer == answer) //I want this from the calcCorrectAnswer function
   {
      isCorrect = true;
      std::cout << "correct"<<std::endl;
   }
   else
   {
      isCorrect=false;
      std::cout << "incorrect" << std::endl;
   }
}
Oct 31, 2015 at 1:21am
If you are just sending one variable, it is much better to return it rather than playing around with references and the like.

For your program, you also need to call the function; where is it getting answer from on line 22?

In any case, here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
int square (int num) {
    return num*num;
}

bool check(int num, int numSquared) {
    return square(num) == squared;
}

// ...

if (check(4, 16)) {
    std::cout << "You got it right!\n";
}
Oct 31, 2015 at 1:34am
Quick and dirty:


Nice! Thanks FurryGuy!
Topic archived. No new replies allowed.