If you are viewing my post, thank you for taking the time to hep me out. It is greatly appreciated. Here is what I'm trying to do:
Write a function called Square2 that will take as arguments an integer and a double. With the use of references i want my function to return the square of both values. When the Square2 function returns I want it to print out the square of both values. I am stuck, becasue I compile the code but there are no errors.. what am i doing wrong?? Any helpful direction is greatly appreciated. Heres the code I have so far:

``````#include <iostream>
using namespace std;
void getInts(int &a, double &b);
void square2(int &a, double &b);
int main()
{
int x;
double y;
getInts(x,y);
cout<< x << " squared and " <<y<< " squared is " <<endl;
square2(x,y);
cout<< x << " " <<y<< endl;
return 0;
}
void getInts(int &a, double &b)
//getting user supplied input
{
cout << "Please enter a whole number and a decimal number " << endl;
cin >> a >> b;
}
void square2(int &a, double &b)
//square the user supplied numbers and put out at main
{
int c;
double d;
c=a*a;
d=b*b;

}``````

If you want main() tgo know the squared values then square2() has to chane the values of a and b.

square2() calculates the squares of the two values using local parameters. Those values (c and d) are destroyed when square2() returns back to main so main() never knows about them.

What you want to do is set the value of a and b to the values of c and …

## All 7 Replies

If you want main() tgo know the squared values then square2() has to chane the values of a and b.

I dont understand what you mean?

square2() calculates the squares of the two values using local parameters. Those values (c and d) are destroyed when square2() returns back to main so main() never knows about them.

What you want to do is set the value of a and b to the values of c and d before square2() returns. Or just simply code the function like this

``````void square2(int& a, double& b)
{
a = a * a;
b = b * b;
}``````

Its funny that you used that in the square2 function, because I just did the same thing before reading your post.. Is it okay within the c++ language to code like that?

Thank you for your help =)

Its funny that you used that in the square2 function, because I just did the same thing before reading your post.. Is it okay within the c++ language to code like that?

Did you try it?

yeah it worked, this is what i changed it to:

``````#include <iostream>
using namespace std;
void getInts(int &a, double &b);
void square2(int &a, double &b);
int main()
{
int x;
double y;
getInts(x,y);
square2(x,y);
cout<< x << " and " <<y<< " are your inputs squared" <<endl;
return 0;
}
void getInts(int &a, double &b)
//getting user supplied input
{
cout << "Please enter a whole number and a decimal number, I will square them for you" << endl;
cin >> a >> b;
}
void square2(int &a, double &b)
//square the user supplied numbers and put out at main
{
int terms;
a=a*a;
b=b*b;

}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.