So, I've just started programming in C++ (as probably evidenced below), this is my first useful program. It converts a user-inputted Celsius measurement into a Fahrenheit measurement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main(){
int celsius, fahrenheit;
cout << " This program converts celsius measurements to fahrenheit measurements." << endl;
while(true){
cout << "Please input the celsius measurement to convert: ";
cin >> celsius;
fahrenheit = celsius * 9/5 + 32;
cout << endl <<"Your measurement in fahrenheit is: " << fahrenheit << endl;
}
}
Thanks for all the replies. I don't know what you mean about having it loop properly, I've tested it a few times, but never done more then 4-5 conversions at a time. When I was writing this, I couldn't really see people using this for more then 1-2 conversions at a time, so I don't know if clearing the buffer would matter.
Not a major issue, but I'd suggest the use of type double rather than int here. Not only will that allow the input and output of decimal values, it also reduces rounding errors.
For example 17C = 62.6F but using all integer values, the output is just 62.
Ok, having taken into consideration what has been said before, I've changed a few things in the program, such as instead of using int I now use double, along with writing the code so that if the user inputs 0 for the degrees in Celsius, the program will quit, and I believe making the buffer clear in every iteration of the loop. Updated code is below.
Trust me, my first "Hello World" was bad, I forgot to add the #include <iostream> , and then had to spend 20 minutes trying to figure out why cout and cin didn't work. This is merely the first program i've made that did something remotely useful. Also, I don't have a Unix system, just Windows 7. Don't know how much that actually changes anything in relation to programming.
Also, i was just now dicking around in the program, basically trying to break it to find bugs, and I realized that if the user inputs a string when asked what measurement they want to convert, the program gets stuck in an infinite loop and continually outputs some random number that I can't read. My question is, is there anyway I can make it so that if the user inputs a string instead of an int, that the program clears the input, says that what was inputted was not valid, and asks them to re-enter the number?
Yea that is what I meant by it looping properly. I guess I should have mentioned improper input as well. Yes there is a way to fix this and it is done using stringstream. You need to include sstream library in your code to use it.
I tried the first option, and it worked. I didn't use the second option because I didn't want to close the program when someone enters non-numeric input, I just wanted to tell them that the input was not valid.
The choice is yours, that's fine. My only concern is that 0 is a valid temperature, so I'd prefer to use something like -999 which is not a valid temperature in either C or F.
That would be an easy fix though. I'm not trying to claim that the way I did it is the only way, or even the best way. That's the beauty with C++, multiple people can have multiple solutions to the same issue. If I (or someone who had the source code) decided to make -999 the exit input, they would only have to change one line of the code. The way I have it set up now is just the way that I want the program to operate.