Simple c++ program error ? Help, thanks

In summary, the conversation was about a simple temperature conversion program that was repeating the output endlessly until the user hit Ctrl-C. The code for the program was provided and the individual asked for help in identifying the error and fixing it. The expert advised checking for errors when reading from or writing to streams and suggested using the break statement to avoid duplicating code. The individual then realized their mistake and thanked the expert for their help.
  • #1
nukeman
655
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 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;
}


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
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.
 
  • #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
 
  • #4
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 cout so you're probably safe not checking cout for errors. (unless you are doing something a lot more sophisticated)
 
  • #5
Oh, got it. Figured it out, thanks!
 

FAQ: Simple c++ program error ? Help, thanks

1. Why am I getting an error when compiling my simple c++ program?

There could be a number of reasons why you are getting an error when compiling your program. Some common reasons include syntax errors, missing semicolons, or using undefined variables. It is important to carefully review your code and use debugging tools to identify and fix any errors.

2. How can I fix a "missing main" error in my c++ program?

The "missing main" error indicates that your program is missing the main function, which is the entry point for a c++ program. To fix this error, make sure you have a main function declared with the correct syntax and that it is included in your program.

3. Why am I getting a "symbol could not be resolved" error in my c++ program?

This error occurs when the compiler cannot find a declaration for a symbol that is being used in your program. Make sure that all variables, functions, and objects used in your code have been properly declared and defined.

4. How do I resolve a "segmentation fault" error in my c++ program?

A segmentation fault occurs when your program tries to access memory that it does not have permission to access. This can be caused by many factors, such as accessing an array out of bounds or dereferencing a null pointer. Use debugging tools to identify and fix the specific cause of the error.

5. What should I do if my c++ program is still not working after fixing all errors?

If your program is still not functioning properly after fixing all errors, it could be due to logical errors in your code. Use debugging tools and methods such as printing out variables and stepping through your code to identify and fix any logical errors.

Similar threads

Replies
3
Views
929
Replies
2
Views
2K
Replies
8
Views
1K
Replies
3
Views
1K
Replies
7
Views
1K
Replies
6
Views
3K
Replies
23
Views
2K
Back
Top