function

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.


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

using namespace 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)
{
    const int 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;
}



Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace 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.


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

using namespace 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)
{
    const int Maxcount = 10;
    int  base;

    for (x , base = 1 ; base <= Maxcount; base++ , x++)

        cout << x << "\t" << x * x << "\t" << "\t" << x * x * x << endl;



    return;
}

Last edited on
Topic archived. No new replies allowed.