uninitialized local variable error

Hey guys im having 2 errors within my code and im not sure what im doing wrong.
i get these 2 errors:
warning C4700: uninitialized local variable 'meters' used
warning C4700: uninitialized local variable 'fractionalFeet' used

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
 #include<iostream>
using namespace std;

// Constant factor converting feet to meters:  3.28 feet = 1 meter
const double FEET_TO_METERS_FACTOR = 0.304878048;          // (1 / 3.28)

// TODO: Declare 4 function prototypes

//       - Name: getFeet
//         Parameters: <none>
//         Returns: an int for the number of feet entered by the user
int getFeet();


//		 - Name: getInches
//         Parameters: <none>
//         Returns: a double for the number of inches entered by the user
double getInches();


//       - Name: convertToMeters
//         Parameters:
//         - 1 int for feet (pass by value)
//         - 1 double for inches (pass by value)
//         Returns: a double for the number of meters
double convertToMeters(int feet, double inches);


//       - Name: displayResults
//         Parameters:
//		   - 1 int for feet (pass by value)
//         - 1 double for inches (pass by value)
//		   - 1 double for meters (pass by value)
//		   Returns: <nothing>
void displayResults(int feet, double inches, double meters);


// ********************************************

int main()
{
	// Declare program variables.

	int feet;
	double inches, meters;

	// TODO: Call function to get feet input.
	//       An int value is returned - assign to the correct variable
	cin >> feet;
	int getFeet(feet);

	// TODO: Call function to get inches input.
	//       A double value is returned - assign to the correct variable
	cin >> inches;
	double getInches(inches);

	// TODO: Call function to convert feet and inches to meters.
	//       Pass the two arguments to the function
	//       Assign the returned value to the correct variable
	convertToMeters(feet, inches);


	// TODO: Call function to print the results.
	//       Pass the correct three arguments to the function
	//       Nothing is returned, so do NOT provide a variable assignment
	displayResults(feet, inches, meters);


	cout << "End program - ";
	return 0;
}

// ********************************************

// TODO: Define getFeet()
// Refer to the function prototype above for parameter and return type info
//
//    Repeat
//       - Prompt the user to enter feet
//       - Read the user's keyboard response
//    Until a value of 0 or more is entered
//
// Note: You will need to declare a local variable inside the function to hold
//       the user's entry for feet (be sure to use correct data type)
int getFeet()
{
	int feet;
	cin >> feet;
	while (feet <= 0)
	{
		cout << "Enter feet: ";
		cin >> feet;
	}
	return feet;
}

// TODO: Define getInches()
// Refer to the function prototype above for parameter and return type info
//
//    Repeat
//       - Prompt the user to enter inches
//       - Read the user's keyboard response
//    Until a value of 0 or more and less than 12 is entered
//
// Note: You will need to declare a local variable inside the function to hold
// the user's entry for inches (be sure to use correct data type)
double getInches()
{
	double inches;
	cin >> inches;
	while (inches > 0 && inches < 12)
	{
		cout << "Enter inches: ";
		cin >> inches;
	}
	return inches;
}

// ********************************************

// TODO: Define convertToMeters()
// Refer to the function prototype above for parameter and return type info
//
//		 - Declare a new local variable called fractionalFeet that is type double
//       - Type cast feet to be a double and assign to the fractionalFeet variable
//       - On the same line where you are assigning the converted feet value to
//       - fractionalFeet, also convert inches to feet (inches / 12) and add to the feet
//       - Finally, return the result of multiplying fractionalFeet by FEET_TO_METERS_FACTOR
double convertToMeters(int feet, double inches)
{
	double fractionalFeet;
	double cast_feet(fractionalFeet);
	double(fractionalFeet * FEET_TO_METERS_FACTOR);
	return fractionalFeet;
}


// ********************************************

// TOTO: Define displayResults()
//       Output statement to format results as shown
//       using values passed to parameters:
//       e.g.
//
//       10'-6.25" = 3.20757 meter(s).
//
//       Use string literals along with value parameters:
//       "'"
//       "\" = "
//       " meter(s)."
void displayResults(int feet, double inches, double meters)
{
	cout << endl;
	cout << feet << "'-" << inches << "\" = " << meters << " meter(s)." << endl;
}
I want to understand your train of thought.
int getFeet(); you declare a function called `getFeet()' that does not receive any parameters.

1
2
	cin >> feet;
	int getFeet(feet);
¿what do you think you are doing here?
oh shoot i didnt even notice that. i gave getFeet a parameter when its not suppose to have one correct?

So i noticed I did that for feet and inches and i removed it. but im still getting the same errors regardless. :(
Last edited on
You do realise that you didn't actually answer ne555's question, right?
At line 45 you declare variable meters without initializing it. At line 66 you pass it to displayResults, but you haven't initialized it.

At line 131 you declare variable fractionalFeet without initializing it. At line 132 you use it but it's still uninitialized.

Other comments:
getFeet() and getInches() should prompt the user upon entry. Right now they only prompt if the user enters an incorrect value.

In main, I think you want
1
2
int feet  = getFeet();
double inches = getInches();

thanks dhayden ill take a look at it when i have time later today. Ill post up any results i get later on when i try it out
Last edited on
Okay so i have seemed to have fixed my 2 errors and my program is almost complete. My only problem is now that when i get prompted to enter a number i for some reason have to put it in twice instead of just once for the inches and feet.

I believe this error is somewhere in here
1
2
3
4
5
6
7
8
9
10
11
12
int getFeet()
{
	int feet;
	cout << "Enter feet: ";
	cin >> feet;
	while (feet <= 0)
	{
		cout << "Enter feet: ";
		cin >> feet;
	}
	return feet;
}
Yep, On line 49:

1
2
	cin >> feet; // First Input. If you delete this one, then you should be ask for 1 input.
	int getFeet(feet); // Second Input 


Also, when you are calling the function, you cannot call it with the function type.

For example:

int getFeet(feet); // You do not have parameters in your function

should be:
feet = getFeet(); // You do not have parameters in your function; however, you are returning the value back to the function.

You got one more like those. Let's see if you can find it.
Last edited on
You're the man chicofeo! understood what you said at first and fixed my error for feet and inches. Thank you so much finally got the program running with no errors and correct results :D
Topic archived. No new replies allowed.