# Simple c++ program error ? Help, thanks!

• Comp Sci

## Homework Statement

Its a simple temp conversion program.

NOW, my problem is when ever I enter a temperature to convert, it just keeps repeating NON stop until i hit ctr-C - Here is the code below. What am i missing and how do i fix this little error?

Code:
/ This program ...This program converts temperatures from Fahrenheit to Celsius.

#include <iostream>
using namespace std;

int main()
{
const int SENTINEL_VALUE = -99;
int degree;
double conversion;

cout << " This proram converts temperatures from Fahrenheit to Celsius.";
cout << " Enter the temperature to convert (-99 to quit): ";
cin >> degree;

while ( degree != SENTINEL_VALUE )
{
conversion = (degree  - 32) * 5.0/9 + 0;
cout << degree << " degrees farhenheit is " << conversion << " degrees celcius " << endl;

cout << " " << endl;
cout << " This proram converts temperatures from Fahrenheit to Celsius.";
cout << " Enter the temperature to convert (-99 to quit): ";
cin >> degree;

}

cout << "\nNormal program exit\n";
return 0;
}

## Answers and Replies

Hurkyl
Staff Emeritus
Science Advisor
Gold Member
What input do you give? What output do you give?

Also, you ought to either check for errors after you read from an istream (cin is an istream) or set the flag to raise exceptions on error.

P.S. an unrelated question: have you been taught the break statement? One coding philosophy is to be very reluctant to duplicate code -- by this philosophy, your loop should start before the initial cout statements, rather than where you do the test to exit the loop. Instead, your test should be done with an if statement, and using break to exit.

When I run the program, and say input 50F, it outputs the correct output, but keeps repeating the answer over and over.... and I do not know why

Hurkyl
Staff Emeritus
Science Advisor
Gold Member
When I run the program, and say input 50F, it outputs the correct output, but keeps repeating the answer over and over.... and I do not know why
This is a wonderful example of why you should check for errors.

You asked it to "read a int value", so it saw that '50' was a number but '50F' was not, so it just read '50' and put that value into degree.

Then, when you asked to "read an int value", the value 'F' is still sitting there waiting to be read. But it can't turn 'F' into an int value, so it puts the stream into error instead of reading anything. The value of degree remains unchanged.

Then, you ask it to "read an int value", but now we have two problems: the stream is in error, and even if it wasn't, 'F' is still waiting to be read! So it does nothing again. And again. And again.

But if you were checking for errors, it would have at least told you "something went wrong when reading input" -- and hopefully you could have noticed for yourself that 'F' is not an integer and you'd have figured it out quickly and by yourself.

This is a very common error to make -- something going wrong in a stream, you don't check, and *bang* infinite loop. So when I see an infinite loop, errors using the streams is one of the first things I look for.

p.s. you should check writing to streams for errors too -- although it's really hard to screw up when writing to cout so you're probably safe not checking cout for errors. (unless you are doing something a lot more sophisticated)

Oh, got it. Figured it out, thanks!