GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2008, 16:29
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Problem Setting a char to exit program


Hello, i want this program to exit when the user enters Q or q ... but for some reason when i enter Q or q the program goes into an infinite loop... can someone help me?

CPP / C++ / C Code:

#include <cstdlib>
#include <iostream>


using namespace std;

void terminator();

int main(int argc, char *argv[])
{
    
    double temperature;
    char Q, q;
    
    
    
    cout << "This program will ask the user for a certain temperature and it will \n";
    cout << "let the user know if either water, mercury, oxygen, or ethyl alcohol\n";
    cout << "would freeze or boil at that temperature\n";
    cout <<"Enter exit to finish the program\n\n\n";
    
        
    while (temperature != 'Q' || temperature != 'q')
          {
               cout << "Please, Enter a temperature: \n";
               cin >> temperature;
               
               if (temperature <= -362 )
               cout << "At this temperature: water, mercury, oxygen, and ethyl alcohol will freeze\n";
                    
               if (temperature <= -173 && temperature >= -306){
               cout << "At this temperature: water, mercury, and ethyl alcohol will freeze\n";
               cout << "Oxygen will boil.\n";}
               
               if (temperature <= -306 && temperature > -362)
               cout << "At this temperature: water, mercury, and ethyl alcohol will freeze\n";
               
               if (temperature <= -38 && temperature > -173){
               cout << "At this temperature: water and mercury will freeze\n";
               cout << "Oxygen will boil.\n";}
               
               if (temperature <= 32 && temperature > -38){
               cout << "At this temperature: water will freeze\n";
               cout << "Oxygen will boil.\n";}
               
               if (temperature > 32 && temperature < 172)
               cout << "At this temperature: oxygen will boil\n";
                
               if (temperature >= 172 && temperature < 212)
               cout << "At this temperature: oxygen and ethyl alcohol will boil\n";
                
               if (temperature >= 212 && temperature < 676)
               cout << "At this temperature: oxygen, ethyl alcohol, and water will boil\n";
                
               if (temperature >= 676)
               cout << "At this temperature: oxygen, ethyl alcohol, water, and mercury will boil\n";
                           
               
          }
          
          terminator();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void terminator()
     {
                  cout << "Have a nice day!" << endl;
                  
     }
  #2  
Old 01-Nov-2008, 17:03
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Problem Setting a char to exit program


Quote:
Originally Posted by bostero22
CPP / C++ / C Code:
    double temperature;
    char Q, q;
         
    while (temperature != 'Q' || temperature != 'q')
          {
  • It is a bad idea to test for a condition without either initializing or assigning the value which is being tested. You are banking on the fact temperature is not 'Q' or 'q'.
  • Secondly, temperature is defined as a double. What do you expect will happen if a char is assigned?
  • Lastly, given that you are doing a number of computations before wanting to break out of the loop, this is situation in which a do-while loop is warranted:
    CPP / C++ / C Code:
    #include <iostream>
    
    using namespace std;
    
    int
    main() {
        char c;
    
        do {
            int i;
    
            cout << "input integer value: ";
            cin >> i;
            cout << i << " was received.  Continue?";
            cin >> c;
        } while (tolower(c) != 'q');
    
        return 0;
    }
Lastly, when questions like this come up (& they come up for everybody...), take the time to reduce the problem down to the smallest case which still duplicates the same problem. If the fix isn't evident, you will have a clearer understanding of what still needs to be resolved.
  #3  
Old 01-Nov-2008, 17:25
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 37
bostero22 is an unknown quantity at this point

Re: Problem Setting a char to exit program


thanks I got it to work right
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Executing Program Via command prompt: Windows vista Relay C Programming Language 5 09-Mar-2007 13:57
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03
Dynamic memory allocation of unknown/variable data length Radi0ShacK C++ Forum 18 14-Feb-2006 22:26
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 11:09
Time Problem in Program MrSmiley C Programming Language 1 03-Nov-2003 13:00

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:38.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.