Variable Not Intialized?

Quick question. Can anyone explain to me how my variable (feet) is not initialized?

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
#include <iostream>
#include <string>
using namespace std;

void Feet2Inches (double feet)
{

double FeetInches;

cout << "Please type units in Feet." << endl;
cout << "Feet: " << endl;
cin >> feet;

FeetInches = feet * 12;
cout << feet << " feet = " << FeetInches << " inches." << endl;
}

int main ()
{

	int conversion;
	double feet, inches, meters, kilometers, pounds, ounces;

	cout << "Welcome to the unit converter!" << endl;
	cout << "Here is the list of units and conversions you can select to calculate the conversion." << endl;
	
	cout << "List of Conversions!" << endl;
	cout << "_____________________________________" << endl;
	cout << "1. Feet to Inches" << endl;
	cout << "2. Inches to Feet" << endl;
	cout << "3. Meters to Kilometers" << endl;
	cout << "4. Kilometers to Meters" << endl;
	cout << "5. Pounds to Ounces" << endl;
	cout << "6. Ounces to Pounds" << endl;

	cout << "Please select a conversion from the list (1-6)" << endl;
	cin >> conversion;

	while (conversion < 1 || conversion > 6)
	{
		std::cout << "Please know that you have selected an invalid conversion." << endl;
		cout << "Select a conversion from the list that is numbered (1-6)." << endl;
		cout << "Enter the number now..." << endl;
		cin >> conversion;
	}

	cout << "Okay!\n" << endl;

	switch (conversion)
	{
	case 1: 
	cout << "You selected (Feet to Inches)" << endl;
	Feet2Inches (feet);
	break;
	case 2:
	cout << "You selected (Inches to Feet)" << endl;
	break;
	case 3: 
	cout << "You selected (Meters to Kilometers)" << endl;
	break;
	case 4:
	cout << "You selected (Kilometers to Meters)" << endl;
	break;
	case 5:
	cout << "You selected (Pounds to Ounces)" << endl;
	break;
	case 6:
	cout << "You selected (Ounces to Pounds)" << endl;
	break;
	}


system("pause");
return 0;
}
Last edited on
In main(), where did you initialize (assign a value) to feet?

Since you passed the variable by value to the function, the function doesn't count.


On line 53 you callFeet2Inches(feet)before this however feet is never given a value.

I believe it would better suit your needs if instead you defined the function without parameters, i.e.void Feet2Inches(void), then simply call it as Feet2Inches();.
Last edited on
Yeah Too Explosive,

That makes alot of sense. I've been working alot recently with Arrays and I guess working with them made me get into the habit of using parameters. I totally see what you mean. It definitely would be best to go the simple route and do without parameters in the function(s).

Thanks
it would be better to make actual use of the parameters.
the i/o code may be in main, and the other functions would only perform calculations.

As it is, your `Feet2Inches()' function is quite useless. There are more interesting things to do than to print to screen.
ne555,

Could you propose me an example or suggestion?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double Feet2Inches (double feet){
   return feet * 12;
}

int main(){
  //...
	switch (conversion)
	{
	case 1: 
		cout << "You selected (Feet to Inches)" << endl;
		cout << "Please type units in Feet." << endl;
		cout << "Feet: " << endl;
		cin >> feet;
		cout << feet << " feet = "
			<< Feet2Inches (feet)<< " inches." << endl;
		break;
	//...
}
you may also capture the returned value into a variable, for later use.
Topic archived. No new replies allowed.