howw to pass user value using same variable between function to function

/*
Given the following C++ program,

#include<iostream>
using namespace std;
int main()
{
int n1,n2,sum;
cout<<"Pls enter 2 number: ";
cin>>n1>>n2;
sum=n1+n2;
cout<<"The sum of "<<n1<<" and "<<n2 <<" is "<<sum<<"\n";
cout<<"program ended\n";
return 0;
}
Convert the program into 4 different functions:
input : reads two numbers
total : sums up the two numbers
dis_total : displays the total
dis_end : displays the ending message

Any global variable not allowed to declare but can use pass by reference to solve the problem.
*/
//what i code

#include <iostream>
using namespace std;
void input(int num1, int num2);
void total(int num1, int num2 ,int* sum);
void display_total(int num1, int num2, int* sum);
void display_end();
int main()
{
int num1, num2, sum; /*end out with error because uninitialized variables num1, num2, and sum. But when i initialized it end up with the number which i initialized #didn't update what user input*/
input(num1, num2);
total(num1, num2, &sum);
display_total(num1, num2, &sum);
display_end();


return 0;
}

void input(int num1, int num2)
{
cout << "Enter 2 number: ";
cin >> num1 >> num2;
}

void total(int num1, int num2, int* sum)
{
*sum = num1 + num2;
}

void display_total(int num1,int num2, int* sum)
{
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
}

void display_end()
{
cout << "program ended\n";
}

//help me find out what and where are wrong
Refernce is like this:

1
2
3
4
5
6
7
8
void input(int& num1, int& num2); // Note: &
...

void input(int& num1, int& num2) // Note: &
{
cout << "Enter 2 number: ";
cin >> num1 >> num2;
}
thks bro!! my code finally works XDD
Topic archived. No new replies allowed.