Uninitialized variable - using functions

I can not figure out why when I go to try and compile it tells me lines
31, 38, 45 are uninitialized local variables. They will be initialized with the function right?

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
#include <iostream>
using namespace std;
void displayMenu();
double fahrenheitToCilsuis(double fTemp);
double milesToKilometers(double miles);
double litersToGallons(double liters);
int main()
{
	void displayMenu();
	return 0;
}
void displayMenu()
{
	double fTemp, miles, liters, gallons, kilometers, cTemp;
	int choice;

	cout << "Welcome to the Conversion Program.";
	cout << "==================================";
	cout << "1. Fahrenheit to Celsius";
	cout << "2. Miles to kilometers";
	cout << "3. Liters to Gallons";
	cout << "4. Exit from the program";
	cin >> choice;
	switch (choice)
	{
	case 1:
		cout << "Enter degrees in Fahrenheit: ";
		cin >> fTemp;
		fahrenheitToCilsuis(fTemp);
		cTemp = fahrenheitToCilsuis(fTemp);
		cout << fTemp << " degrees Fahrenheit is " << cTemp << " degrees celsius.";
		break;
	case 2:
		cout << "Enter distance in Miles: ";
		cin >> miles;
		milesToKilometers(miles);
		kilometers = milesToKilometers(miles);
		cout << miles << " miles is equal to " << kilometers << " kilometers.";
		break;
	case 3:
		cout << "Enter volume in liters: ";
		cin >> liters;
		litersToGallons(liters);
		gallons = litersToGallons(liters);
		cout << liters << " liters is equal to " << gallons << " gallons.";
		break;
	default:
		return;
	}
}
double fahrenheitToCilsuis(double fTemp)
{
	double cTemp;
	cTemp = (fTemp - 32.0) * (5.0 / 9.0);
	return cTemp;
}
double milesToKilometers(double miles)
{
	double kilometers;
	kilometers = miles * 1.60934;
	return kilometers;
}
double litersToGallons(double liters)
{
	double gallons;
	gallons = liters / 3.785;
	return gallons;
}
Line 9: void displayMenu(); should be displayMenu();.

More on calling functions: http://www.cplusplus.com/doc/tutorial/functions/

A suggestion from me would be to place a new line \n like this:

1
2
3
4
5
6
	cout << "Welcome to the Conversion Program.\n";
	cout << "==================================\n";
	cout << "1. Fahrenheit to Celsius\n";
	cout << "2. Miles to kilometers\n";
	cout << "3. Liters to Gallons\n";
	cout << "4. Exit from the program\n";


Hello Peon,

At first I wanted to write this as you did your code, so that you would get an idea of how hard your code is to read.

This is only part of what was said:
Repeater once wrote:

You know you're not being charged for every letter you use, right? If you want people to be able to read your code (which you do). ...


To that I would add use some blank lines to break up your code and make it easier to read. Then your code would look like this:
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
#include <iostream>
#include <iomanip>  // <--- "iostream" manipulators.

using namespace std;

void displayMenu();
double fahrenheitToCilsuis(double fTemp);
double milesToKilometers(double miles);
double litersToGallons(double liters);

int main()
{
    void displayMenu();

    return 0;
}

void displayMenu()
{
    double fTemp{}, miles{}, liters{}, gallons{}, kilometers{}, cTemp{};
    int choice;

    cout <<
        "Welcome to the Conversion Program.\n"
        "==================================\n"
        "1. Fahrenheit to Celsius\n"
        "2. Miles to kilometers\n"
        "3. Liters to Gallons\n"
        "4. Exit from the program\n"
        "Enter choice: ";
    cin >> choice;

    std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Added.

    switch (choice)
    {
        case 1:
            cout << "Enter degrees in Fahrenheit: ";
            cin >> fTemp;

            fahrenheitToCilsuis(fTemp);

            cTemp = fahrenheitToCilsuis(fTemp);

            cout << fTemp << " degrees Fahrenheit is " << cTemp << " degrees celsius.";

            break;

As you can see this makes the code easier to read, and before I forget, proper indenting helps. Overall it looks like the indenting is good.

As chicofeo has pointed out. Line 13 is a vary nice prototype, but should not be there. It is also a duplicate of line 6. This causes "main" to run through the prototype and execute the return statement leaving the program. Remove the "void" in line 13 and it becomes a function call.

You said:

I can not figure out why when I go to try and compile it tells me lines
31, 38, 45 are uninitialized local variables.


What you did not mention is what IDE/compiler you are using. When I compiled this with VS 2017 Community, (the free version), I could not duplicate this error. And that was before I changed line 20.

It is always a good idea to initialize your variables when they are defined. If for no other reason to know that they do not contain a garbage value.

From the 2011 standards you have the {}s, known as the uniform initializer. Empty {}s will set the variable to a type of zero based on the variables' type. You can also put something inside the {}s if you need a value other than zero.

"std::strings", "std::vectors" and other containers are empty when defined and do not need to be initialized unless you need to give it a starting value.

The "cout" statement is another option in addition to what chicofeo has shown you. Two points here:
1. it gives a better visual representation of what it will look like on the screen and
2. It makes it easier to modify if needed.

Since I have been doing this for awhile I added the last line to the menu to give you an idea of something that looks better. You are free to use whatever you like for the prompt of the last line.

Line 41 is a useless function call that is not needed. Since you do not collect the return value of the function all it does is waste time for no reason.

Line 43 is correct.

Al this holds true for the other 2 case statements.

Here is a suggestion for you. Change the function "displayMenu" to
1
2
3
4
5
6
7
8
9
10
11
int mainMenu()
{
    int choice{};

    do
    {

    } while (invalid choice);

    return choice;
}

This way you display the menu and check for a valid choice only returning a valid choice.

I have not worked with this yet, but there are 2 possible choices for "main"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    int menuChoice{};

    menuChoice = mainMenu();

    switch (menuChoice)
    {
        default:
            break;
    }
    //displayMenu();

    return 0;
}


Or
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    switch (mainMenu())
    {
        default:
            break;
    }
    //displayMenu();

    return 0;
}

In the second option "mainMenu" will be called first an the returned value will drive the "switch". This works as long as you do not need the return value outside the "switch".

I think I would use the first choice and put lines 5 - 11 in a do/while loop to keep the program running until you enter a 4.

Something to keep in mind. "main" should direct the flow of the program and a function should do 1 thing and do it well. Yes it is one way to code the program where a function most of the program, but is is not the best way to go about it.

Andy
Topic archived. No new replies allowed.