Simple c++ program error ? Help, thanks

Click For Summary

Discussion Overview

The discussion revolves around a simple C++ program designed to convert temperatures from Fahrenheit to Celsius. Participants are addressing an issue where the program enters an infinite loop when a temperature is inputted, specifically when non-integer values are provided.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • The original poster describes a problem with their temperature conversion program that enters an infinite loop upon inputting a temperature.
  • One participant asks for clarification on the input and output provided to the program and suggests checking for errors after reading from the input stream.
  • Another participant explains that inputting a value like '50F' causes the program to behave unexpectedly because 'F' is not an integer, leading to an error state in the input stream.
  • This participant emphasizes the importance of error checking in streams to avoid infinite loops and suggests that the program's structure could be improved by reducing code duplication.
  • The original poster later confirms they have resolved the issue, indicating they found a solution to the problem discussed.

Areas of Agreement / Disagreement

While the original poster resolves their issue, the discussion highlights a common programming error related to input handling, suggesting a shared understanding of the problem among participants. However, there is no explicit consensus on a single solution approach, as different strategies for error handling and code structure are discussed.

Contextual Notes

The discussion touches on the limitations of the program's input handling, particularly regarding non-integer values and the resulting state of the input stream. The need for error checking is emphasized, but specific implementations are not detailed.

nukeman
Messages
651
Reaction score
0

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 celsius " << 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;
}


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
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 count 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
 
nukeman said:
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. :smile:



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 count so you're probably safe not checking count for errors. (unless you are doing something a lot more sophisticated)
 
Oh, got it. Figured it out, thanks!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K