Hello,
I'm having a problem with my program returning the function in the manner i need it to. The program needs to be able to have a value inputted and return it's square and cubed value. I was able to print out values 1-10 with it's corresponding square and cube. However i can't figure out how to accept an input and make that input show it's square and cube.
#include <iostream>
#include <cmath>
usingnamespace std;
void Sq_Cub (int);
int main()
{
int num;
cout << "welcome please enter the number you wish to use:" << endl;
cin >> num;
Sq_Cub(num);
return 0;
}
void Sq_Cub(int x)
{
constint Maxcount = 10;
int x, base;
for (x , base = 1 ; base <= Maxcount; base++ , x++)
cout << x << "\t" << x * x << "\t" << "\t" << x * x * x << endl;
return;
}
Your Sq_Cub function has x as its input parameter, but then re-declare x as an int variable inside the function itself. I don't think that should even compile on a modern compiler, but regardless your new declaration of x is shadowing your input.
In general, a function should do one thing. I would put any loop outside of the function itself.
#include <iostream>
#include <cmath>
usingnamespace std;
void Sq_Cub (int);
int main()
{
int num;
cout << "welcome please enter the number you wish to use:" << endl;
cin >> num;
Sq_Cub(num);
return 0;
}
void Sq_Cub(int x)
{
cout << x << "\t" << x * x << "\t" << "\t" << x * x * x << endl;
}
Thank you so much I was having a hard time realizing this issue with having the function accept the parameter. I was able to fix the code and is now working:
Hello,
I'm having a problem with my program returning the function in the manner i need it to. The program needs to be able to have a value inputted and return it's square and cubed value. I was able to print out values 1-10 with it's corresponding square and cube. However i can't figure out how to accept an input and make that input show it's square and cube.
#include <iostream>
#include <cmath>
usingnamespace std;
void Sq_Cub (int);
int main()
{
int num;
cout << "welcome please enter the number you wish to use:" << endl;
cin >> num;
Sq_Cub(num);
return 0;
}
void Sq_Cub(int x)
{
constint Maxcount = 10;
int base;
for (x , base = 1 ; base <= Maxcount; base++ , x++)
cout << x << "\t" << x * x << "\t" << "\t" << x * x * x << endl;
return;
}