Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   How would I do this? (http://www.programmingforums.org/showthread.php?t=13014)

357mag Apr 18th, 2007 5:59 AM

How would I do this?
 
I'm using Visual Studio 2005. I'm writing a program that inputs two integers from the user and adds them together and outputs their sum. That much I have. But I would like to add the line "Press any key to continue". And then when I hit like the spacebar the console window will close. I believe Visual C++ 6 had it set up this way with that Press any key to continue output to the console window, and I like it so I'd like to add that to the bottom of the console window. The way it's working now the console window will close if I hit the Enter key, but it won't close if I hit the Spacebar or any other key. Here is my program:


#include "stdafx.h"
#include "iostream"

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
int integer1, integer2, sum;

cout << "Enter first integer\n";
cin >> integer1;
cout << "Enter second integer\n";
cin >> integer2;
sum = integer1 + integer2;
cout << "The sum is " << sum << endl;

cin.get();
cin.get();
return 0;
}

Klarre Apr 18th, 2007 6:27 AM

http://www.programmingforums.org/for...o-cin-get.html

DaWei Apr 18th, 2007 6:59 AM

Please be polite enough to read the forum's rules/FAQ, particularly with respect to the use of code tags.

The answer is: you have crap in the stream from the previous input operations. Clear it out. See cin.ignore.

pegasus001 Apr 18th, 2007 8:19 AM

Seems like DaWei hit two pidgeons in one shot. :)

Here is an explanation.


All times are GMT -5. The time now is 2:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC