Need help with functions

I am having trouble figuring out why my code asks me to enter a number twice. I need to figure out how to make it just square the input from getValue();

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
33
34
35
36
#include <iostream>
#include <cmath>

using namespace std;

int getValue();
void squareValue();

int main()
{

	int num;
	num = getValue();
	

	cout << "The value entered is " << num << endl;
	squareValue();

	return 0;
}

int getValue()
{
	int val;
	cout << "Enter a number " << endl;
	cin >> val;
	return val;
}

void squareValue()
{

	cout << "The value squared is " << pow(getValue(), 2) << endl;

	return;
}
Last edited on
You're calling getValue() twice, once on line 13 and again on line 33.
I suggest modifying the squareValue() function's signature and passing a value to it.
Last edited on
Topic archived. No new replies allowed.