Temperature conversion using a menu and using prototypes/switch

Hi there, I am in my first programming class, and need a little help...The assignment is to create a program that converts Fahr to Celsius and vice versa using prototypes, a menu and a switch, and the output to be displayed as a table...Here's what I have so far, it was running, when I had case one, then I "broke" it somehow doing case 2...I'm not very proficient at debugging, U keep getting the error that the initialization skipped the 'fahr' and 'celsius' and I'm sure there is plenty more wrong, lol...Any help is appreciated, thank!


#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;

//prototypes
float FahrToCel(float fahr);
float CelToFahr(float celsius);

///Entry point to the application
int main(void)
{
//display a menu
cout << "Menu" << endl;
cout << "1) Convert Fahrenheit to Celsius" << endl;
cout << "2) Convert Celsius to Fahrenheit" << endl;
cout << "3) Exit" << endl;

//get user's choice
cout << "Enter your choice:" ;
int choice = 0;
cin >> choice;

//cases
switch(choice)
case 1:
{
cout << "Enter temperature in Fahrenheit ";
float fahr = 0.0;
cin >> fahr;

//convert the temp
float celsius = FahrToCel( fahr );

//display
cout << "\n" << fahr << (char)248 << "fahrenheit is" << fixed << setprecision(2)
<< celsius << (char)248 << "celsius" << endl;
break;

case 2:
cout << "Enter temperature in Celsius ";
float celsius = 0.0;
cin >> celsius;

//convert the temp
float fahr = CelToFahr( celsius );

//display
cout << "\n" << celsius << (char)248 << "celsius is" << fixed << setprecision(2)
<< fahr << (char)248 << "Fahrenheit" << endl;
break;
case 3:
cout <<"Goodbye" << endl;
break;
}
//pause
cout << "\nPress any key to continue...";
_getch();
return 0;
}
///convert Fahr to Cel
float FahrToCel (float fahr)
{
return (fahr - 32) / 1.8;
}
@kara

You had forgotten some brackets '{}' and left off the convert to Fahrenheit function. Well, here is the program with corrections. I changed the floats to double, as errors were shown, "Conversion from Double to Float..". Now all you need to do, is finish it with the table display
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
// Fahr2Cel.cpp : main project file.

#include<iostream>
#include<conio.h>
#include<iomanip>

using namespace std;

//prototypes
double FahrToCel(double fahr);
double CelToFahr(double celsius);

///Entry point to the application
int main()
{
	//display a menu
	cout << "Menu" << endl;
	cout << "1) Convert Fahrenheit to Celsius" << endl;
	cout << "2) Convert Celsius to Fahrenheit" << endl;
	cout << "3) Exit" << endl;

	//get user's choice
	cout << "Enter your choice:" ;
	int choice = 0;
	cin >> choice;

	//cases
	switch(choice)
	{
	case 1:
		{
			cout << "Enter temperature in Fahrenheit ";
			double fahr = 0.0;
			cin >> fahr;

			//convert the temp
			double celsius = FahrToCel( fahr );

			//display
			cout << "\n" << fahr << (char)248 << " Fahrenheit is " << fixed << setprecision(2)
				<< celsius << (char)248 << " Celsius" << endl;
			break;
		}

	case 2:
		{
			cout << "Enter temperature in Celsius ";
			double celsius = 0.0;
			cin >> celsius;

			//convert the temp
			double fahr = CelToFahr( celsius );

			//display
			cout << "\n" << celsius << (char)248 << " celsius is " << fixed << setprecision(2)
				<< fahr << (char)248 << " Fahrenheit" << endl;
			break;
		}
	case 3:
		{
			cout <<"Goodbye" << endl;

			//pause
			cout << "\nPress any key to continue...";
			_getch();
		}
	}
	return 0;
}
///convert Fahr to Cel
double FahrToCel (double fahr)
{
	return (5.0/9.0)*(fahr -32.0);;
}

double CelToFahr( double celsius )
{
	return (celsius*9.0/5.0)+32.0;
}
Last edited on
Topic archived. No new replies allowed.