Degrees Conversion

Hey guys, how is it going?. two questions, first I was wondering why when I run my code it never shows me what the converted value is, and second I want to avoid the user to input first a value that doesn't work, like strings. Also is it possible to set anything in the options so if they put something different than 1 and 2 it asks them again? thank you so much.

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

int main()
{
  int options;
  double temperatureF, converted_temperatureC,converted_temperatureF,temperatureC;
    cout<<"this program convers from"<< endl <<"Type 1 to convert Fahrenheit to Celsius, 2 to convert Celsius to Fahrenheit: ";
    cin>> options;
      if (options == 1 || options == 2)
      {
         if (options == 1)
           { 
             cout<<"Enter temperature in Fahrenheit: ";
             cin>> temperatureF;
             converted_temperatureC = (temperatureF - 32) * (5.0/9.0);
             cout<<"In Celsius that’s "<< converted_temperatureC;
            }
        if (options == 2)
        { 
            cout<<"Enter temperature in Celsius: ";
            cin>> temperatureC;
            converted_temperatureF = (temperatureC * 9.0/5.0) + 32;
            cout<<"In Fahrenheit that’s "<< converted_temperatureF;
            
        }
    }
  
  return 0;
}
Hello andreswp10,

why when I run my code it never shows me what the converted value is

What do you mean lines 18 and 25 print the converted value to the screen. What else are you looking for?

Lines 16 and 23 are formatted input expecting a number in this case. If you enter anything other than a number the input stream will fail. This can be checked in a while loop while (!cin). inside the loop you can print an error message, clear the stream and clear the input buffer before asking for input again. This the while loop will continue until a number is input.

Follow the first if with an else to inform the used of invalid input before the program ends. I suggest a pause to give the user a chance to read the message before the program ends. The other option is to create a function to print the menu and get user input. Followed by validating the input and returning a goof number. When I do something like this I generally use the menu function.

Hope that helps,

Andy
Hello Andy, what I mean is that when I input cin>> temperatureF; the number in the line above it doesn't run the operation of the next following lines.

1
2
converted_temperatureC = (temperatureF - 32) * (5.0/9.0);
             cout<<"In Celsius that’s "<< converted_temperatureC;

is never displayed
Last edited on
I believe your special character ’ is breaking it. Try the standard '.

Guessing you could also use wcout to handle it or some other fixes.
Seems mixing cout with wcout is a bad idea though. Not something I know too much about.
Last edited on
Thank you so much that was it hahahaha.
Topic archived. No new replies allowed.