functions not working :(

So I'm working with functions, and I'm building a conversion program. The conversions are fahrenheit to celsius, miles to kilometers, and liters to gallons. Each conversion has a different function, and so does the menu display. My problem right now is that the menu display works, but the rest of the functions don't, and I dont know what I'm doing wrong.

These are my errors:
1>C:\Users\morga\source\repos\ec2\ec2.cpp(44): error C4700: uninitialized local variable 'celsius' used
1>C:\Users\morga\source\repos\ec2\ec2.cpp(72): error C4716: 'farenheitToCelius': must return a value
1>C:\Users\morga\source\repos\ec2\ec2.cpp(47): error C4700: uninitialized local variable 'kilometers' used
1>C:\Users\morga\source\repos\ec2\ec2.cpp(50): error C4700: uninitialized local variable 'gallons' 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
#include <iostream>
#include <iomanip>
using namespace std;

void displayMenu();
double farenheitToCelius(double);
double milesToKilometers(double);
double litersToGallons(double);

int main()
{
	const int TEMP_CHOICE = 1,
		DISTANCE_CHOICE = 2,
		VOLUME_CHOICE = 3,
		QUIT_CHOICE = 4;
	int menuChoice,
		cTemp,
		kms,
		gals;

	cout << fixed << showpoint << setprecision(2);

	do
	{
		cout << "\t\tWelcome to the Conversion Program\n"
			<< "\t\t=================================\n\n";
		displayMenu();
		cout << "Please choose option 1-4 from the menu above: ";
		cin >> menuChoice;

		while (menuChoice < TEMP_CHOICE || menuChoice > QUIT_CHOICE)
		{
			cout << "Please enter a valid menu choice: ";
			cin >> menuChoice;
		}
		if (menuChoice != QUIT_CHOICE)
		{
			switch (menuChoice)
			{
			case TEMP_CHOICE:
				cout << farenheitToCelius(cTemp);
				break;
			case DISTANCE_CHOICE:
				cout << milesToKilometers(kms);
				break;
			case VOLUME_CHOICE:
				cout << litersToGallons(gals);
				break;
			}
		}
	} while (menuChoice != QUIT_CHOICE);
	return 0;
}
void displayMenu()
{
	cout << "1. Farenheit to Celsius\n"
		<< "2. Miles to Kilometers\n"
		<< "3. Liters to Gallons\n"
		<< "4. Exit the Program\n\n"
		<< "Please choose menu option 1-4: ";
}
double farenheitToCelcius(double farenheit)
{
	cout << "Please enter the temperature in degrees Farenheit: ";
	cin >> farenheit;

	double celsius = (farenheit - 32) * (5 / 9);
	return celsius;
}
double milesToKilometers(double miles)
{
	cout << "Please enter the distance in miles: ";
	cin >> miles;

	double kilometers = miles * 1.609;
	return kilometers;
}
double litersToGallons(double liters)
{
	cout << "Please enter the volume in liters: ";
	cin >> liters;

	double gallons = liters / 3.785;
	return gallons;
}
Last edited on
Line 6:
farenheitToCelius

Line 62:
farenheitToCelcius

These should be the same (and it's spelled Celsius and Fahrenheit, btw). You probably will need to fix your call on line 41, as well.

Other warnings,
41:37: warning: 'cTemp' may be used uninitialized in this function [-Wmaybe-uninitialized]
44:34: warning: 'kms' may be used uninitialized in this function [-Wmaybe-uninitialized]
47:33: warning: 'gals' may be used uninitialized in this function [-Wmaybe-uninitialized]

None of those input parameters to those functions are needed. You overwrite their uninitialized values anyway when you direct user input into them. Remove them and make them local variables to each function instead.

Example:
1
2
3
4
5
6
7
8
9
double farenheitToCelcius()
{
	double farenheit;
	cout << "Please enter the temperature in degrees Farenheit: ";
	cin >> farenheit;

	double celsius = (farenheit - 32) * (5 / 9);
	return celsius;
}


Other issue: (5 / 9) does integer division, because both arguments are integers. You want to force normal, floating-point division by doing (5.0 / 9) instead.
Last edited on
Okay, so I wasn't really worried about spelling but, I fixed that, and the 5.0 thing and even took out the parameters and it's still giving me the same exact errors.
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
#include <iostream>
#include <iomanip>

void displayMenu();
double fahrenheitToCelcius();
double milesToKilometers();
double litersToGallons();

int main()
{
   const int TEMP_CHOICE     = 1,
             DISTANCE_CHOICE = 2,
             VOLUME_CHOICE   = 3,
             QUIT_CHOICE     = 4;

   int menuChoice { };

   std::cout << std::fixed << std::showpoint << std::setprecision(2);

   do
   {
      std::cout << "\t\tWelcome to the Conversion Program\n"
         << "\t\t=================================\n\n";

      displayMenu();
      std::cin >> menuChoice;
      std::cout << '\n';

      while (menuChoice < TEMP_CHOICE || menuChoice > QUIT_CHOICE)
      {
         std::cout << "Please enter a valid menu choice: ";
         std::cin >> menuChoice;
      }

      if (menuChoice != QUIT_CHOICE)
      {
         switch (menuChoice)
         {
         case TEMP_CHOICE:
            double c;
            c = fahrenheitToCelcius();

            std::cout << "Celcius: " << c << "\n\n";
            break;

         case DISTANCE_CHOICE:
            double km;
            km = milesToKilometers();

            std::cout << "KM: " << km << "\n\n";
            break;

         case VOLUME_CHOICE:
            double g;
            g = litersToGallons();

            std::cout << "Gallons: " << g << "\n\n";
            break;
         }
      }
   }
   while (menuChoice != QUIT_CHOICE);

}

void displayMenu()
{
   std::cout << "1. Farenheit to Celsius\n"
      << "2. Miles to Kilometers\n"
      << "3. Liters to Gallons\n"
      << "4. Exit the Program\n\n"
      << "Please choose menu option 1-4: ";
}

double fahrenheitToCelcius()
{
   std::cout << "Please enter the temperature in degrees Farenheit: ";
   double fahrenheit;
   std::cin >> fahrenheit;

   return ((fahrenheit - 32.0) * (5.0 / 9.0));
}

double milesToKilometers()
{
   std::cout << "Please enter the distance in miles: ";
   double miles;
   std::cin >> miles;

   return (miles * 1.609);
}

double litersToGallons()
{
   std::cout << "Please enter the volume in liters: ";
   double liters;
   std::cin >> liters;

   return (liters / 3.785);
}
                Welcome to the Conversion Program
                =================================

1. Farenheit to Celsius
2. Miles to Kilometers
3. Liters to Gallons
4. Exit the Program

Please choose menu option 1-4: 1

Please enter the temperature in degrees Farenheit: 98.6
Celcius: 37.00

                Welcome to the Conversion Program
                =================================

1. Farenheit to Celsius
2. Miles to Kilometers
3. Liters to Gallons
4. Exit the Program

Please choose menu option 1-4: 2

Please enter the distance in miles: 1.5
KM: 2.41

                Welcome to the Conversion Program
                =================================

1. Farenheit to Celsius
2. Miles to Kilometers
3. Liters to Gallons
4. Exit the Program

Please choose menu option 1-4: 3

Please enter the volume in liters: 3.7
Gallons: 0.98

                Welcome to the Conversion Program
                =================================

1. Farenheit to Celsius
2. Miles to Kilometers
3. Liters to Gallons
4. Exit the Program

Please choose menu option 1-4: 4
Topic archived. No new replies allowed.