I\'m trying to have this program only print to screen with two functions. Overlo
ID: 3547199 • Letter: I
Question
I'm trying to have this program only print to screen with two functions. Overloading is ok. The reference passing has me lost. I thought where I'm at made sense, but the print out is 1 1 9 9. It's suppose to be 4 11 12 24. The body of the code cannot change and must only show the int x=1; and two functions. Any suggestions please?
#include <iostream>
int addTo(int *x,int num);
int addTo(int *x);
int print(int *x);
using namespace std;
int main()
{
int x = 1;
addTo(&x,3);
print(&x);
addTo(&x,7);
print(&x);
addTo(&x);
print(&x);
addTo(&x,x);
print(&x);
return 0;
}
//=============== addTo() ==================
int addTo(int *x, int num){
int total;
total=*x+num;
}
//============== addTo() ===================
int addTo(int *x){
int total;
*x= total+1;
}
//=============== print() ==================
int print(int *x){
cout << *x << " ";
return 0;
}
Explanation / Answer
#include <iostream>
int addTo(int *x,int num);
int addTo(int *x);
int print(int *x);
using namespace std;
int main()
{
int x = 1;
addTo(&x,3);
print(&x);
addTo(&x,7);
print(&x);
addTo(&x);
print(&x);
addTo(&x,x);
print(&x);
return 0;
}
//=============== addTo() ==================
int addTo(int *x, int num)
{
*x = *x+num;
return *x;
}
//============== addTo() ===================
int addTo(int *x)
{
*x= *x+1;
return *x;
}
//=============== print() ==================
int print(int *x)
{
cout << *x << " ";
return 0;
}