Having trouble with second method

Hello,

My homework is asking me to make two methods with the first one being "enter one number and store the number in the respective global variable. Call this method userInput. Use float or double for the variable." The second being
"The second method will use the global variable, cube the value, and display he result to the screen. Call this method cube."
So far this is what I got, any help is appreciated. I just don't know how to do the second void or what to put in the (). I know this code is all over the place

#include <iostream>

using namespace std;

void userInput(float x)

{
cout << "The cube of the given number is " << (x*x*x) << endl;
}

int main()
{
float x;
cout << "Please enter a number " << endl;
cin >> x;
userInput(x);
return 0;
}
There is nothing really wrong with this code as such. It asks for a number, obtains a number and then displays the number cubed. The name of the function could be called cube rather than userInput....

It just isn't as the homework asks. IMO it's better as it uses a function param rather than a global variable (which are not good practice!).

As per the homework:

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

using namespace std;

float num {};    // Global variable - BAD!

void userInput()
{
	cout << "Please enter a number: ";
	cin >> num;
}

void cube ()
{
	cout << "The cube of the given number is " << (num * num * num) << '\n';
}

int main()
{
	userInput();
	cube();
}


which is what is asked - but not how you would do it!
Yeah I'm not a fan of using the global variables either but sadly I have to. Thank you for this! I was getting confused on the second method but now looking at it once it's solved makes sense and will be imbedded in my brain, thank you!
Topic archived. No new replies allowed.