what's missing

Using the switch statement, write a program that allows the user to convert either from degrees Celsius to Fahrenheit or degrees
Fahrenheit to Celsius. Use the following formulas:
degreesC = 5(degreesF-32)/9
degreesF = (9(degreesC)/5) + 32
Prompt the user to enter a temperature and either a 'C'(or 'c') for
Celsius or an 'F'(or 'f') for Fahrenheit: allow
either upper or lower case,
but if anything other than 'C', 'c', 'F', or 'f' is entered, print an error
message that tells the user how to enter a valid selection (upper or lower case 'C' or 'F').



the code
/01 #include <iostream>
02 #include <windows.h>
03 using namespace std;
04
05 char F, C, M;
06 int Ct, Ft, T;
07
08 int main()
09 {
10 char wait4User;
11 do{
12 cout << "Please enter which option you would like to use : \n";
13 cout << "Fahrenheit = F \n";
14 cout << "Celsuis = C \n";
15 cout << "Exit = E \n";
16 cin >> M;
17 if (M == 'F' || M == 'f') {
18 do {
19 cout << "Please enter the temperature in Fahrenheit (999 returns to menu): ";
20 cin >> T;
21 if (T != 999){
22 Ct = (T - 32) * 5/9;
23 cout << "The tempurate in Celsius is : " << Ct << "\n";
24 }
25 if (T == 999){
26 break;
27 }
28 }while (T != 999);
29 }
30 if (M == 'C' || M == 'c') {
31 do {
32 cout << "Please enter the temperature in Celsius (999 returns to menu): ";
33 cin >> T;
34 if (T != 999){
35 Ft = T * 9/5 + 32;
36 cout << "The tempurate in Farenheit is : " << Ft << "\n";
37 }
38 if (T == 999){
39 break;
40 }
41 }while (T != 999);
42 }
43 }while (M != 'E' || M != 'e');
44 if (M == 'E' || M == 'e'){
45 return 0;
46 }
47 cin >> wait4User;
48 return 0;
49 }
Last edited on
@Ali Tamim

??? !!

You haven't asked a question. But, if you were wondering why the program doesn't end when you pressed the 'E', it's because you are checking for the 'E' with an OR instead of an AND.
Line 43 should look like }while (M != 'E' && M != 'e');
Ok lots of weird things happening in your code.

Global variables line 05 and 06 place them in main.

Multiple returns in your function. There is a time and a place for that but not here rethink you control structures.

You did not make a switch statement.

http://www.cplusplus.com/doc/tutorial/control/

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
#include <iostream>

int main(){

	char ch;
	char ans = 'y';

	while (ans == 'y'){
		std::cout << "Enter a letter A or B: ";
		std::cin >> ch;
		switch (ch){
		case 'A':
			std::cout << "You entered: " << ch << std::endl;
			break;
		case 'B':
			std::cout << "You entered: " << ch << std::endl;
			break;
		default:
			std::cout << "Wrong input." << std::endl;
		}
		std::cout << "Again? y/n:";
		std::cin >> ans;
	}

	return 0;
}
Last edited on
i see, thank you
Topic archived. No new replies allowed.