I am taking a fundamental C++ course for the first time and seem to be having trouble. The teacher is specific of course. I have to write a code that converts a given value from Celsius to Fahrenheit. I got that part easily enough, however, my problem occurs in the error control. Obviously I must cout an error report if a character that is not a numerical value is entered. No matter waht i have tried I either get the error message when 0 is inputted or I get characters such as letters assigned the value 0 and receive an answer. I don't need any fancy stuff but my teacher told me that I needed to use strings in order to make this work I just don't see how i could. With any alterations to my code please give an explanation so i could understand and learn the concepts. Also I am using codeblocks if it is at all important.
/*
Martin Ruben Canchola Jr.
Course: COSC 1436.002 Programming FUndamentals
Lab #5: Program for Celsius to Fahrenheit
Due Date: September 28, 2014
Instructor: Korinne Caruso
This program converts values of Celsius to Fahrenheit given the formula F=(9/5)*C+32
must also have error control and be able to formulate float values.
*/
#include <iostream>
usingnamespace std;
int main()
{
float Celsius;
float Fahrenheit;
//cout statements for user interface
cout<<"Welcome to Temperature converter. Please enter a value for degrees Celsius.";
//the cin statement for Celsius
cin>>Celsius;
//enter if/else statement here for error control
if (Celsius)
{
//the formula for fahrenheit. must come after cin to get the cin value of Celsius.
Fahrenheit=(9.0/5)*Celsius+32;
cout<<Celsius<<" Degrees celsius = "<<Fahrenheit<<" degrees fahrenheit.\n";
}
//trailing else statement
else
{
cout<<"Sorry. Please try again using a valid number.";
}
return 0;
}
//Work with strings to get this shit to work. Look at chapter 2-4 in order to find what you need.
This might help if you take your teachers advice and input a string. Maybe test if the string is a number and, if so, use it as your variable Celsius or Fahrenheit. Otherwise ... error message!!
http://www.cplusplus.com/reference/cstdlib/atof/
PS You also need to look closely at your line 31. Write out in words what you want the program to decide on at this point. I don't think you mean "Celsius?". You might have to ask the user which conversion is requirejavascript:editbox2.editPreview()d eg by inputing a 'C' or an 'F' in addition to the number (or string) to be the temperature.
None of this worked. That atof didn't help and i copy pasted the code to try it out and it still gave input for letter characters. The isdigit also is not working. I tried assigning Celsius as a string but now it is telling me (error: no match for 'opertor<' in 'Celsius < '0' '. This is due tomorrow and im kinda stressing the matter since I am moving to a new house today and don't have much time to work on it.
#include <iostream>
#include <stdlib.h>
usingnamespace std;
int main()
{
float Celsius;
float Fahrenheit;
char strCelsius[120];
//cout statements for user interface
cout<<"Welcome to Temperature converter. Please enter a value for degrees Celsius.\n";
//the cin statement for Celsius
cin >> strCelsius;
Celsius = atof(strCelsius);
//enter if/else statement here for error control
if ( Celsius == 0.0 )
cout << "Warning: check your temperature, it is reading as zero.\n";
else
cout << "Bingo: number accepted, no warning needed.\n";
//the formula for fahrenheit. must come after cin to get the cin value of Celsius.
Fahrenheit=(9.0/5)*Celsius+32;
cout<<Celsius<<" Degrees celsius = "<<Fahrenheit<<" degrees fahrenheit.\n";
// //trailing else statement
// else
// {
// cout<<"Sorry. Please try again using a valid number.";
// }
return 0;
}