I have the following code, but I need to add steps that will give an error when
ID: 3637669 • Letter: I
Question
I have the following code, but I need to add steps that will give an error when the user inputs invalid data types, i.e. letters and special characters.//For cin and cout
#include <iostream>
//For setpercision and fixed manipulators
#include <iomanip>
using namespace std;
int main()
{
//Variable declarations
double cel[10], fah[10];
int n, i;
//Request user to enter n of temperatures to convert
cout<<"How many Celcius temperatures do you want to convert to Fahrenheit temperatures? ";
cin>>n;
//Test loop control conditions
while(n<1||n>10)
{
//When number entered is less than 1 or greater than 10 imform user of limits
cout<<"Number must be between 1 and 10 ";
//Request user to enter a number within the limits
cout<<"Re-enter number of Celcius temperatures you want to convert to Fahrenheit temperatures? ";
//Update the loop control variable
cin>>n;
}
//Statement sets i to execute the number of times entered by the user
for( i=0; i<n; i++)
{
//Request user to entr the value of the Celcius temperature they wish to convert to Fahrenheit temperature
//for they value of n they entered
cout<<"Please enter the temperatures in Celsius: ";
//Initialize Celcius temperatures
cin>>cel[i];
//Performs the conversion calculations
fah[i]=9./5.*cel[i]+32.;
}
//Outputs the input value for Celcius temperature and the converted value for Fahrenheit temperature
cout<<"Celsius Fahrenheit ";
//Statement sets i to execute the number of times entered by the user to format output
for(i=0;i<n;i++)
//Statement formats the out; 1 column for Celcius and 1 column for Fahrenheit
//The temperature will convert the values within 1 decimal
cout<<setprecision(1)<<fixed<<cel[i]<<" "<<fah[i]<<endl;
//Stops the command prompt form closing
system("pause");
//Indicates program success
return 0;
}
Explanation / Answer
Take the temperature in Celsius and multiply 1.8. Add 32 degrees. The result is degrees Fahrenheit.