i tried

I spent 2 days of trying something this is what i came up with, but i am still stuck on the choice parts. am i doing it right? this is what i did.

QUESTION
Write a program that repeatedly ask the user to choose one of the choices:

1. Choice 1 : Convert Fahrenheit to Celsius using the formula :
°F = °C x 9/5 + 32

2. Choice 2: Convert Celsius to Fahrenheit using the formula :
°C = (°F - 32) x 5/9

3. Choice 3: Exit the Program


The program should read the user input and then display the result or an error message as appropriate, a sample program run is provided below:

Sample run:

Welcome to the Temperature Converter Application
Please type 1 for Fahrenheit to Celsius conversion
Type 2 for Celsius to Fahrenheit conversion.
Or type -1 to exit the program
1
Please enter your temperature in Fahrenheit
86

Computing...
The temperature in Celsius is 30

Please type 1 for Fahrenheit to Celsius conversion
Type 2 for Celsius to Fahrenheit conversion.
Or type -1 to exit the program

5
That is not an option.
Please type 1 for Fahrenheit to Celsius conversion
Type 2 for Celsius to Fahrenheit conversion.
Or type -1 to exit the program

2
Please enter your temperature in Celsius
20

Computing...
The temperature in Fahrenheit is 68


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
  #include <iostream>                        
using namespace std;                       

int main() 

{                               
    float celsius;                         
    float fahrenheit;

cout<<"Choose on of the choices"<<endl;
cout<<"1. Convert Fahrenheit to Celsius"<<endl;
cout<<"2. Convert Celsius to Fahrenheit"<<endl;
cout<<"3. Exit the Program"<<endl;


    cout << "Enter Celsius temperature: "; 
    cin >> celsius;
    fahrenheit = (celsius * 9.0) / 5.0 + 32;
    cout << "Fahrenheit = " << fahrenheit << endl;

    cout << "Enter Fahrenheit  temperature: "; 
    cin >> fahrenheit;
    celsius = (fahrenheit - 32) * 5/9;
    cout << "Celsius = " << celsius << endl;

system("PAUSE");
    return 0;                             
}
You need to put your code in a loop so that the user is forced to either enter 1, 2, or -1. You need to look at the user's input to direct your code to the valid choices.
I did not write the code all i did was put in comments where the code should go you write in the blanks on what i commented for it to work.


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
  #include <iostream>                        
using namespace std;                       

int main() 

{                               
    float celsius;                         
    float fahrenheit;
    
    
// Point 1: Make a do while loop starting here. Loop will exit when users choice input equal -1.


cout<<"Choose on of the choices"<<endl;
cout<<"1. Convert Fahrenheit to Celsius"<<endl;
cout<<"2. Convert Celsius to Fahrenheit"<<endl;
cout<<"3. Exit the Program"<<endl;


    // Point 2: Make if statement syaing if users choice input equals to 1 or 2 what ever this part of the code does for the option picked.
    
    
    cout << "Enter Celsius temperature: "; 
    cin >> celsius;
    fahrenheit = (celsius * 9.0) / 5.0 + 32;
    cout << "Fahrenheit = " << fahrenheit << endl;
    
    
    // End Point 2: Close braces for this code in between here and up till point 2.
    // Point 3: Make if else statment for users input of 1 or 2.


    cout << "Enter Fahrenheit  temperature: "; 
    cin >> fahrenheit;
    celsius = (fahrenheit - 32) * 5/9;
    cout << "Celsius = " << celsius << endl;
    
    
    // End Point 3: End the braces here.
    
    
    // Point 4: The code will still loop if the user has not entered 1, 2, or -1. Loop will end if user typed in -1.
    
// End Point 1: End do while here


system("PAUSE");
    return 0;                             
}
In main,
Add a string variable called Choice.
Add an integer variable called iChoice

Points 1, 2, and 3:

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

iChoice = 0;

while( iChoice != -1 )
    {
    cin >> Choice;

    iChoice = atoi( Choice.c_str() );

    switch( iChoice )
        {
        case 1:    //    Point 1
            // Put your "Enter Celsius temperature: " code here.

            break;

        case 2:    //    Point 2
            // Put your Enter Fahrenheit temperature: "  code here

            break;

        case 3:    //    Point 3
            iChoice = -1;

            break;

        default:
            break;

        }    /*    switch( iChoice )    */


Point 4:

1
2
3

    }    /*    while( iChoice != -1 )    */
Topic archived. No new replies allowed.