Question: Hello im doing a celsius to farheint conversion program but the problem i am having is when i ask the user which conversion. for example:

Hello im doing a celsius to farheint conversion program but the problem i am having is when i ask the user which conversion.

for example:

enter 1 for to F to C

Enter 2 for C to F.

when the user enters any other number besides 1 or 2 I want the program to stop and say" invalid number try again"

how can I put that into my code. Heres my code on the bottom. Would really appreciate the help.

#include

#include

using namespace std;

//Function declarations

float calcCelsius(float fahrenheit);

float calcFahrenheit(float celsius);

/* This function will convert the

* temperature from celsius to fahrenheit

*/

float calcCelsius(float fahrenheit)

{

//Converting fahrenheit to celsius

float celsius = (5.0 / 9.0)*(fahrenheit - 32.0);

return celsius;

}

/* This function will convert the

* temperature from fahrenheit to celsius

*/

float calcFahrenheit(float celsius)

{

//Converting celsius to fahrenheit

float fahrenheit = (9.0 / 5.0)*celsius + 32.0;

return fahrenheit;

}

int main()

{

//Declaring variables

int choice;

float start_temp, end_temp, temp_incr;

//Displaying menu

cout << "Which Temperature of Scale Conversion would you like to perform ?" << endl;

cout << "1.Convert F to C" << endl;

cout << "2.Convert C to F" << endl;

//getting the choice entered by the user

cout << " What is your choice ? ";

cin >> choice;

//Getting the starting temperature entered by the user

cout << " Starting Temperature :";

cin >> start_temp;

//Getting the ending temperature entered by the user

cout << "Ending Temperature :";

cin >> end_temp;

//Getting the temperature increase entered by the user

cout << "Temperature Increase :";

cin >> temp_incr;

//Setting the precision

std::cout << std::setprecision(1) << std::fixed;

//Based on the user's choice the corresponding case will get executed

switch (choice)

{

case 1: {

//Table which displaying the fahrenheit and celsius temperatures

cout << " Fahrenheit\t\tCelsius" << endl;

cout << "__________\t\t_______" << endl;

//This loop will display the fahrenheit and celsius temperatures table

for (int i = start_temp; i <= end_temp; i += temp_incr)

{

//Calling the calcCelsius() function by passing the fahrenheit as parameter

cout << i << "\t\t\t" << calcCelsius(i) << endl;

}

break;

}

case 2: {

//Table which displaying the celsius and fahrenheit temperatures

cout << " Celsius\t\tFahrenheit" << endl;

cout << "__________\t\t_______" << endl;

//This loop will display the celsius and fahrenheit temperatures table

for (int i = start_temp; i <= end_temp; i += temp_incr)

{

//Calling the calcFahrenheit() function by passing the celsius as parameter

cout << i << "\t\t\t" << calcFahrenheit(i) << endl;

}

break;

}

}

system("pause");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!