For my lab, I have to create a menu with options (which I did), but after, the program has to prompt whether the user wants to convert another temperature or quit but I have no idea what to do. Also, I don't know how to do the error check with strings. It works with integers but if anything else, an infinite loop occurs. Been trying to mess with the code all night but I'm to exhausted to keep attempting and researching.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float choice;
double cels;
double fahr;
cout << fixed << showpoint << setprecision(2);
do
{ // Stars will space different calculations if user chooses to continue
cout << "****************************************************\n";
cout << "****************************************************\n";
// Menu, gives three options to choose from
{
cout << "\n\tTemperature Conversion Calculator\n\n"
<< "1. Celsius to Fahrenheit\n"
<< "2. Fahrenheit to Celsius\n"
<< "3. Quit the Program\n"
<< "\nPlease select an option: ";
cin >> choice;
}
// Error check, makes sure user selects the given options
while (choice < 1 || choice > 3)
{
cout << "Please enter choices 1,2, or 3: ";
cin >> choice;
}
// If option 1 is selected, number for Celsius will be converted to Fahrenheit
while (choice == 1)
{
cout << "\nEnter a degree for Celsius: \370 ";
cin >> cels;
fahr = (cels * (9.0 / 5.0)) + 32.0;
cout << "The degrees in Fahrenheit is: \370"<<fahr <<endl; // Calculated temperature will be shown
}
while (choice == 2)
// If option 2 is selected, number for Fahrenheit will be converted to Celsius
{
cout << "Enter a degree for Fahrenheit: \370";
cin >>fahr;
cels = (5.0/9.0) * (fahr - 32);
cout << "The degrees in Celsius is: \370"<< cels <<endl; // Calculated temperature will be shown
}
} while (choice != 3);
cout << "\n\nThank you!\n\n";
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int choice = 0; //<-----------------
double cels;
double fahr;
do
{
cout << "\n\tTemperature Conversion Calculator\n\n"
<< "1. Celsius to Fahrenheit\n"
<< "2. Fahrenheit to Celsius\n"
<< "3. Quit the Program\n"
<< "\nPlease select an option: ";
cin >> choice;
// If option 1 is selected, number for Celsius will be converted to Fahrenheit
if (choice == 1)
{
cout << "\nEnter a degree for Celsius: ";
cin >> cels;
fahr = (cels * (9.0 / 5.0)) + 32.0;
cout << "The degrees in Fahrenheit is: "<<fahr <<endl; // Calculated temperature will be shown
choice = -1;
}
if (choice == 2)
{
cout << "Enter a degree for Fahrenheit: ";
cin >>fahr;
cels = (5.0/9.0) * (fahr - 32);
cout << "The degrees in Celsius is: "<< cels <<endl; // Calculated temperature will be shown
choice = -1;
}
if (choice == 3)
break;
}while (choice < 1 || choice > 3);
cout << "\n\nThank you!\n\n";
return 0;
}
Temperature Conversion Calculator
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Quit the Program
Please select an option: 1
Enter a degree for Celsius: 100
The degrees in Fahrenheit is: 212
Temperature Conversion Calculator
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Quit the Program
Please select an option: 2
Enter a degree for Fahrenheit: 212
The degrees in Celsius is: 100
Temperature Conversion Calculator
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Quit the Program
Please select an option: 3
Thank you!
Exit code: 0 (normal program termination)
Why you put \370 is anybody's guess ... anyway ... :)
Thanks! And \370 is the degree symbol! How can I ask the user if they would like to try again after they've finished converting a temperature and have y/n as a choice?
Well the degree symbol was killing your program. I suggest you forget about it.
It's up to you but I can't see any need for a y/n question. Option 3 does the same. You could use char for the response variable and play with that. Up to you.