Program that converts temperatures - doesn't work


EDIT:
So I'm writing a program that converts temperatures from one scale to the other unless you enter Q to quit.
It doesnt run properly and says" ' F' is an invalid choice' " or even C.

Here is my completed program:
(I know it is long but im supposed to use while loop)

#include <iostream>


using namespace std;

int main()
{
    char choice;
    double tempToConvert;
    
    cout << "This program converts a temperature from one scale to the other." << endl;
    cout << "Choose one of the following ..." << endl;
    cout << "    C: to convert from Celsius to Fahrenheit" << endl;
    cout << "    F: to convert from Fahrenheit to Celsius" << endl;
    cout << "    Q: to quit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;
    
    
    
    while (choice == 'F' || choice == 'f')  
    {
        cout << "Enter the Fahrenheit temperature to convert: ";
        cin >> tempToConvert;
        
        cout << tempToConvert << " degrees Fahrenheit is " << ((tempToConvert-32)/1.8)
             << " degrees Celsius. " << endl;
        cout << endl;
        
        cout << "Choose one of the following ..." << endl;
        cout << "    C: to convert from Celsius to Fahrenheit" << endl;
        cout << "    F: to convert from Fahrenheit to Celsius" << endl;
        cout << "    Q: to quit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
    }
    
    while (choice == 'C' || choice == 'c' )
    {
        cout << "Enter the Fahrenheit temperature to convert: ";
        cin >> tempToConvert;
          
        cout << tempToConvert << " degrees Celsius is "
             << " degrees Celsius. " << endl;
        cout << endl;
        
        cout << "Choose one of the following ..." << endl;
        cout << "    C: to convert from Celsius to Fahrenheit" << endl;
        cout << "    F: to convert from Fahrenheit to Celsius" << endl;
        cout << "    Q: to quit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
    }
    while ((choice != 'C') || (choice != 'c') || (choice != 'F') || (choice != 'f') || (choice != 'Q') || (choice != 'q'))
    {
        cout << choice << " is not a valid choice. " << endl;
        cout << endl;
        
        cout << "Choose one of the following ..." << endl;
        cout << "    C: to convert from Celsius to Fahrenheit" << endl;
        cout << "    F: to convert from Fahrenheit to Celsius" << endl;
        cout << "    Q: to quit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
    }
    
    
    if (choice == 'Q' || choice == 'q')
    {
        system("pause"):
    }   
    

system("pause");

}   


Last edited on
hi, i can help you but are you sure you have to use While for EACH test ?!?! bcs it's really nonsense. So the problems are : the test for the valid choices, you have to use && and not ||, bcs it has to test if it's not this AND not this AND not this AND not this...
The other problem is for example when you choose c, you enter the second while loop but then when you choose f it just exits the while loop and there is not other loop to continue .
You forgot the convert celcius calculus.
I don't know why you used those system("pause"); but you have to exit of the main correctly;
I don't know if it helps you, if you got any question i am ready to help but i definitely needs to know if you have to use that stupid while thing everywhere and if you know the do while loop.
Cya :)
Yeah, what MagicDark said:
1) Replace the while's with if's.
2) Fix your conditionals.
3) Replace 'system("pause")' with 'return 0' to quit properly.
I think maybe you were supposed to enclose the whole process in a single while loop.

I was about to mention the reasons for some of your problems but MagicDark pointed them out.

I think maybe you were supposed to use something like this.

I took your code and just moved it around a bit.
By the way , copying and pasting blocks of code is quicker, but easier to miss the changes you need to make, like was mentioned, you forgot the conversions, and I believe the both said Fahrenheit ..
I didn't finish up your conversions, or straighten up everything, Just figured if you got up and running you could sort the rest out as you go.

anyway check this out and see if it makes more sense.
a few less while loops.

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
 

    char choice ;
  
	double tempToConvert;
    
    cout << "This program converts a temperature from one scale to the other." << endl;
    cout << "Choose one of the following ..." << endl;
    cout << "    C: to convert from Celsius to Fahrenheit" << endl;
    cout << "    F: to convert from Fahrenheit to Celsius" << endl;
    cout << "    Q: to quit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;
	cout << '\n';

	while (choice != 'q' && choice != 'Q') // exit if user enters q or Q
	{
				
		if (choice == 'F' || choice == 'f')
		{
		
			cout << "Enter the Fahrenheit temperature to convert: ";
        	cin >> tempToConvert;
        
			cout  << tempToConvert << " degrees Fahrenheit is " << ((tempToConvert-32)/1.8)
             << " degrees Celsius. " << endl ;
		}

		if (choice == 'C'|| choice == 'c')
		{
		
			cout << "Enter the Celsius temperature to convert: ";
			cin >> tempToConvert;
			cout << tempToConvert << " degrees Celsius is " << " degrees Celsius. " << endl;
        
		}
		else 
			cout << endl << choice << " is not a valid choice." << endl;
				
		cout << endl;
		cout << "This program converts a temperature from one scale to the other." << endl;
		cout << "Choose one of the following ..." << endl;
		cout << "    C: to convert from Celsius to Fahrenheit" << endl;
		cout << "    F: to convert from Fahrenheit to Celsius" << endl;
		cout << "    Q: to quit" << endl;
		cout << "Enter your choice: ";
				
		cin >> choice;
	
	} // end while loop


I think he's using the system pause just to keep the window open when he runs it from an IDE.
Last edited on
@civilVIU

Doing your program this way, means you only have to show a menu once, since it'll print each time the while starts. Also, by using toupper(), you can check only one case, instead of both lower and upper. Makes it easier to read, also. Put in a Pause routine at the end of the program.
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
// Convert Temperatures.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

void Pause();

int main()
{

	char choice=' ';
	float tempToConvert;
	
	cout << "This program converts a temperature from one scale to the other." << endl << endl;
	while (toupper(choice) != 'Q')  // exit if user enters q or Q
	{
		cout << "Choose one of the following ..." << endl;
		cout << "    C: to convert from Celsius to Fahrenheit" << endl;
		cout << "    F: to convert from Fahrenheit to Celsius" << endl;
		cout << "    Q: to quit" << endl;
		cout << "Enter your choice: ";
		cin >> choice;
		cout << '\n';

		if (toupper(choice) == 'F' )
		{

			cout << "Enter the Fahrenheit temperature to convert: ";
			cin >> tempToConvert;

			cout  << tempToConvert << " degrees Fahrenheit is " << ((tempToConvert-32)/1.8)
				<< " degrees Celsius. " << endl ;
		}

		else if (toupper(choice) == 'C')
		{

			cout << "Enter the Celsius temperature to convert: ";
			cin >> tempToConvert;
			cout << tempToConvert << " degrees Celsius is " << (((tempToConvert*9)/5)+32.0) << " degrees Fahrenheit. " << endl;

		}
		else if (toupper(choice) !='F' && toupper(choice) != 'C' && toupper(choice) != 'Q') 
		{
			cout << endl << choice << " is not a valid choice." << endl;
		}
		cout << endl << endl;
	}
	Pause();
} // end while loop

void Pause()
/* Wait for a return key to continue */
{
char YN;
while ( (YN = getchar() ) != '\n') ; /* Just wait for Enter key  */
return;
}  /* End of Pause() */
Topic archived. No new replies allowed.