Function not working...

According to the compiler, my variable is being used without being initialized. I've gone over my notes, but I can't seem to determine what the problem is. Any help is appreciated.

-JPL

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  // Attached:  HW_7a
// ========================================
// Assignment:  HW_7a
// ========================================
// Programmer: Justin Le
// Class:  CMPR 120
// ========================================

#include <iostream>
#include <cmath>
#include <math.h>

using namespace std;

void mainMenu();
float getSideA();
float getSideB();
float calcSideC(float SideA, float SideB);
void displaySideC();

int main()
{
	float SideA;
	float SideB;
	float SideC;
	
	mainMenu();

	SideA = getSideA();

	SideB = getSideB();

	SideC = calcSideC(SideA, SideB);

	displaySideC();

	return 0;
}
// ========================================
void mainMenu()
{
	cout << "Enter two sides of a right triangle." << endl << endl;
}
// ========================================
float getSideA()
{
	float SideA;

	cout << "Side A:\nPlease enter the dimension:\t";
	cin >> SideA;

	return SideA;
}
// ========================================
float getSideB()
{
	float SideB;

	cout << "Side B:\nPlease enter the dimension:\t";
	cin >> SideB;

	return SideB;
}
// ========================================
float calcSideC(float SideA, float SideB)
{
	float SideC;

	SideC = sqrt((SideA*SideA)+(SideB*SideB));

	return SideC;
}
// ========================================
void displaySideC()
{
	float SideC;

	cout << "The dimension of Side C is " << SideC << "." << endl << endl;
}
// ======================================== 
1
2
3
4
5
6
void displaySideC()
{
	float SideC;

	cout << "The dimension of Side C is " << SideC << "." << endl << endl;
}


You declare a local variable SideC, and immediately output its value without ever having assigned a value to it first.
Oh, that does make sense. So, how would I be able to declare SideC in the calcSideC function and assign it that number, then bring SideC down to displaySideC?

SideA and SideB got returned successfully, and yet sideC is failing to do so. Why is this?

Sorry if it's a stupid question, I started programming about 2 months ago, and we literally just learned functions today.
There's no mystery to it - you're already doing it for your calcSideC function. You've already figured out how to pass values into a function, and you've already got the calculated value for SideC being passed back to the main function. So...
Ah, I just realized my mistake, looking at it with fresh eyes after a night's sleep...

I forgot to declare a global function value.

Before
1
2
3
4
5
void mainMenu();
float getSideA();
float getSideB();
float calcSideC(float SideA, float SideB);
void displaySideC();


After
1
2
3
4
5
6
void mainMenu();
float getSideA();
float getSideB();
float sideC;
float calcSideC(float SideA, float SideB);
void displaySideC(float SideC);



Thanks, Mikey, for your help. :)
Last edited on
No problem :)
Topic archived. No new replies allowed.