1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple c++ program error ? Help, thanks!

  1. Oct 1, 2011 #1
    1. The problem statement, all variables and given/known data

    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 (Text):

    / 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;
    }

     

    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Oct 1, 2011 #2

    Hurkyl

    User Avatar
    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.
     
  4. Oct 1, 2011 #3
    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
     
  5. Oct 1, 2011 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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 cout so you're probably safe not checking cout for errors. (unless you are doing something a lot more sophisticated)
     
  6. Oct 1, 2011 #5
    Oh, got it. Figured it out, thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Simple c++ program error ? Help, thanks!
  1. C program help needed (Replies: 3)

Loading...