Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell display drive driver drivers email error ethernet excel firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook 2007 outlook express partition problem router slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
c++ dos console window problem


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
N.N.P's Avatar
Junior Member with 27 posts.
 
Join Date: Jul 2004
Experience: Beginner
27-Oct-2004, 03:50 PM #1
c++ dos console window problem
Hi, im having a very basic problem with all programs i write to run in a console window. If I dont use cin.get() the window just closes. Is this normal? if it is then I have another problem/question. At what stage of the program should I put cin.get(). for example, in this program (a very basic calculator) I have to put it in two different places to get the console window to stay open. Why is this, I thought i should only have to put it in at the end and it should wait for me to press return before exiting.

Code:
#include <iostream>


using namespace std;

int main()
{
    float num1, num2; 
    char op;
    
    cout<<"Please enter calculation"<<endl;
    cin>>num1>>op>>num2;
    cin.get(); //why do I need cin.get() in two places? here.....
    
    if (op == '+') {
        cout<<"the answer is"<<num1 + num2<<endl;
        } 
    else if (op == '-') {
        cout<<"the answer is"<<num1 - num2<<endl; 
    } else if (op =='*') {
        cout<<"the answer is"<<num1 * num2<<endl;
    } else if (op == '/') {
        cout<<"the answer is "<<num1 / num2<<endl; 
    } else {
        cout<<"You have entered an invalid statement"<<endl;
    }                  
  
 cin.get(); // and here?
}
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
27-Oct-2004, 06:59 PM #2
Because when you run a console application from Windows, it is configured to close the console when the program has finished. The last cin.get() keeps the program from ending and thereby leaves the window open. If you open a command prompt and run the program from the command line you'll see the same result only it is manifest by the console returning to the entry prompt. You can leave the last cin.get() in place but I would suggest displaying something like "press any key to continue" so the user knows what to do at that point. You should not need the cin.get() at the top of the program at all.

Welcome to TSG.
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
27-Oct-2004, 07:10 PM #3
Okay, admittedly I don't fully understand the iostream objects in C++ but when I removed the first cin.get( ) from your code and ran it, it didn't stop on the last one but just returned to the C: prompt. The only possible explanation that I can give is that there is something in the keyboard buffer that the last cin.get() "gets" and with the first one on place, it is gone so the second one suspends the program exit.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
28-Oct-2004, 05:23 PM #4
I usually use cin.ignore() instead of system("pause") in cstdlib , but just like cin.get() , cin.ignore() will have to be done twice unless you clear the buffer with fflush(stdin); in cstdio.

The ideal way would be to use

string input;
getline(cin,input);

, validate it as a string, tokenize based on the operators, do the necessary conversion to float and then computer the value.

It's more code, but if done right, you can input whatever you want, like

4 + 77 + 1056 - 22 / 3

Order of operation, parenthesis and brackets would have to be considered.

Just pointing out that I myself avoid cin like the plague.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run

Last edited by Shadow2531 : 28-Oct-2004 05:31 PM.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 04:10 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.