How to make functions like this, asking user to input on different function and calculate on another function

Don't judge me, I am just a beginner and want to know why it is not working? If you can explain me the solution, in easy way. How to add the two numbers and get result, by asking the first number on the first function and asking the second number on different function and calculate the result there.
I just can't find tutorial on this, please if you can fix this code, and explain why it doesn't work.

#include <iostream>

using namespace std;
int question();
int sum(int total);

int main(){
int num;
int total;
question();
sum(total);
return 0;
}

int question(){
int num;
cout << "Enter a whole number" << endl;
cin >> num;
return 0;
}

int sum(int total){

int num;
int sec_num;
cout << "Enter another whole number" << endl;
cin >> sec_num;

total = num + sec_num;
return total;
}


In the function question, you create a local variable, num. Then, you put a value into num. Then, the function ends and that variable, num, is destroyed. Gone.

In the function sum, you create a local variable, num. This variable has NOTHING to do with the variable that was created in question. Nothing. That the two variables happened to have the same name has NO special meaning. They are different variables, that happen to have the same name.

You can hold onto the value by returning it.

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

using namespace std;
int question();
int sum(int total);

int main(){
  int num;
  int total;
  num = question();
  total = sum(num);
  cout << "Total is: " << total;
  return 0;
}

int question(){
  int userInput;
  cout << "Enter a whole number" << endl;
  cin >> userInput;
  return userInput;
}

int sum(int value)
{

  int userInput;
  cout << "Enter another whole number" << endl;
  cin >> userInput;

  int summedValue = userInput + value;
  return summedValue;
}

Last edited on
First, you are calling the sum() function with an uninitalized local variable called "total" by value. This variable currently holds garbage in main(), so when you pass it to sum(), all you are doing is passing garbage to it.

Secondly, you are not doing anything with the total variable in sum, you are just returning it back to main, where it gets discarded because the return value is not used anywhere. You are also doing something similar in question(), where you are inputting into a local variable num and simply returning 0 without doing anything with num.

Here is a simple fix, try to step through it to see how it works:

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
#include <iostream>
int question();
int sum(int);
int main()
{
    int input = question();
    std::cout << sum(input);
}

int question()
{
    int num = 0;
    std::cout << "Enter number: " << std::endl;
    std::cin >> num;
    return num;
}

int sum(int num1)
{
    int num2 = 0;
    std::cout << "Enter another number: " << std::endl;
    std::cin >> num2;
    return num1 + num2;
}


Although, your approach is unusual. It would be much easier to do this with a single function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int sum();

int main()
{
    std::cout << "Sum: " << sum() << std::endl;
}

int sum()
{
    int num1 = 0, num2 = 0;
    std::cout << "Enter two numbers (seperated by whitespace)" << std::endl;
    std::cin >> num1 >> num2;
    return num1 + num2;
}
Thank you I understand the concept very well, I will practice more on it.
what about this one though? How can you fix this? If you just use the number from first function only to make for loop, not calculation.

#include <iostream>

using namespace std;
int question();
int sum(int total);

int main(){
int num;
int total;
int value;
total = sum(value);
cout << "Total is: " << total;
return 0;
}

int question(){
int userInput;
cout << "Enter a whole number" << endl;
cin >> userInput;
return userInput;
}

int sum(int value)
{
int summedValue;
int userInput;
int second_input;
for (int x=0; x<=userInput; x++){
cout << "Enter another whole number" << endl;
cin >> second_input;

summedValue += second_input;
}


return summedValue;
}
That code never calls the function question
TheToaster
The way you do it looks very easy but I have one question. Program consider num1 as the first number right? if that so, how can the program knows that num1 is the first number, since you didn't use nothing from the first function?




#include <iostream>
int question();
int sum(int);
int main()
{
int input = question();
std::cout << sum(input);
}

int question()
{
int num = 0;
std::cout << "Enter number: " << std::endl;
std::cin >> num;
return num;
}

int sum(int num1)
{
int num2 = 0;
std::cout << "Enter another number: " << std::endl;
std::cin >> num2;
return num1 + num2;
}
Repeater
so how can you make it call the function. Please I really need it. If you can fix that code?
Last edited on
Well you already know how to call functions.

Here, look at this line:
std::cout << sum(input);

There's a call to the function sum.
Repeater
Thank you very much!!! now I get it. I understand both of them perfectly. Thanks again.
Topic archived. No new replies allowed.