Troubles with Functions

I am having trouble getting a char function to work, I have everything else working, but for some reason when i try to run my program it is telling my that I am not entering a valid input. I have tried many different things but I couldn't figure it out.

The other functions work, just the displayMenu one does not.


#include <iostream>
#include <iomanip>
using namespace std;

//Prefix functions
char displayMenu(char);
double getFahrenheit(double,double);
double getCelsius(double,double);

int main()
{
// Setting my Variables
char choice;
double C,F;

// Menu options, Start loop
do
{
displayMenu(choice);
switch(choice)
{
// Everything for Choice A,a
case 'a':
case 'A':
{
getFahrenheit(C,F);
break;
}
//Everything for Choice B,b
case 'b':
case 'B':
{
getCelsius(F,C);
break;
}
// End Program
case 'C':
case 'c':
{
return 0;
break;
}

default:
{
// Option for if they key'd in something other than what was given
cout << "You did not key in either A B or C\n " << endl;
}
}
}
while(choice != 'C' && choice != 'c');

return 0;
}



char displayMenu(char choice)
{
cout << "\t Welcome to the Temperature Converter!!\n\n";
cout << "A) Celsius - Fahrenheit " << endl;
cout << "B) Fahrenheit - Celsius " << endl;
cout << "C) Close Program " << endl;
cout << "Choose one of the following letters and press enter." << endl;
cin >> choice;
return choice;
}


double getFahrenheit(double C,double F)
{
cout << setprecision(1) << fixed << showpoint;
cout << "\tCelsius - Fahrenheit\n " << endl;
cout << "Enter a degree in Celsius, Then push enter\n";
cin >> C;
F = (9.0/5)*C+32;
cout <<"\n\tYour degree's in Fahrenheit is " << endl;
cout <<"\n" << setw(25)<< F << "\n" << endl;
return F;
}

double getCelsius(double F, double C)
{
cout << setprecision(1) << fixed << showpoint;
cout << "\tFahrenheit - Celsius\n " << endl;
cout <<"Enter a degree in Fahrenheit, Then push enter\n";
cin >> F;
C = (F-32)/(9.0/5);
cout <<"\n\t Your Degree's in Celsius is " << endl;
cout <<"\n" << setw(25)<< C << endl << "\n";
return C;
}
Your functions don't really make sense. You are never using the value returned by any of your functions, and none of your functions actually need arguments the way you have them set up. Consider something 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
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
#include <iostream>
#include <iomanip>
using namespace std;

//Prefix functions
char displayMenu();
void getFahrenheit();
void getCelsius();

int main()
{
	// Setting my Variables
	char choice;
	double C, F;

	// Menu options, Start loop
	do
	{
		choice = displayMenu();
		switch (choice)
		{
			// Everything for Choice A,a
		case 'a':
		case 'A':
		{
			getFahrenheit();
			break;
		}
		//Everything for Choice B,b
		case 'b':
		case 'B':
		{
			getCelsius();
			break;
		}
		// End Program
		case 'C':
		case 'c':
		{
			return 0;
			break;
		}

		default:
		{
			// Option for if they key'd in something other than what was given
			cout << "You did not key in either A B or C\n " << endl;
		}
		}
	} while (choice != 'C' && choice != 'c');

	return 0;
}



char displayMenu()
{
	char choice;
	cout << "\t Welcome to the Temperature Converter!!\n\n";
	cout << "A) Celsius - Fahrenheit " << endl;
	cout << "B) Fahrenheit - Celsius " << endl;
	cout << "C) Close Program " << endl;
	cout << "Choose one of the following letters and press enter." << endl;
	cin >> choice;
	return choice;
}


void getFahrenheit()
{
	double C;
	double F;
	cout << setprecision(1) << fixed << showpoint;
	cout << "\tCelsius - Fahrenheit\n " << endl;
	cout << "Enter a degree in Celsius, Then push enter\n";
	cin >> C;
	F = (9.0 / 5)*C + 32;
	cout << "\n\tYour degree's in Fahrenheit is " << endl;
	cout << "\n" << setw(25) << F << "\n" << endl;
	return;
}

void getCelsius()
{
	double C;
	double F;
	cout << setprecision(1) << fixed << showpoint;
	cout << "\tFahrenheit - Celsius\n " << endl;
	cout << "Enter a degree in Fahrenheit, Then push enter\n";
	cin >> F;
	C = (F - 32) / (9.0 / 5);
	cout << "\n\t Your Degree's in Celsius is " << endl;
	cout << "\n" << setw(25) << C << endl << "\n";
	return;
}


I changed your getFarenheit and getCelsius functions to void since you weren't using the value returned, and I changed all of your functions to not have arguments, since you weren't actually passing in values to be used by these functions. We are now using the value returned by displayMenu() by setting choice = displayMenu() in our main function.
So, since getFahrenheit and getCelsius are only actually doing an equation and "cout" F or C they are aloud to be void? Considering I am not actually reusing those values.

and if I am return a value do I need to = the value to that function ?

choice = displayMenu ? and how come I dont have to have any arguments for it ?

*I am also getting an infinite loop if I key in a letter for one of my Celsius or Fahrenheit.
Last edited on
Topic archived. No new replies allowed.