![]() |
| | #1 |
| C++ SharK Join Date: Mar 2004 Location: Denmark
Posts: 62
| Console window waiting on cin.ignore() or cin.ignore(2) cin.ignore; cin.ignore; or one time: cin.ignore(2); to make the console window wait for a keypress. One cin.ignore, doesn't seem to do the work Code: /**************************************************************
* This program sums the numbers in a user-specified range, *
* omitting the if test that sets the upper and lower bounds. *
**************************************************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int Num_1;
int Num_2;
int Num_one = 0;
int Num_two = 0;
int Range = 0;
cout << "Exercise 1.20:" << endl << endl;
cout << "Enter two numbers !" << endl << endl;
cout << "Enter number one: ";
cin >> Num_one;
Num_1 = Num_one;
cout << "Enter number two: ";
cin >> Num_two;
Num_2 = Num_two;
if ( Num_1 < Num_2 )
for ( Num_one = Num_one ; Num_one <= Num_two ; ++Num_one )
{
Range += Num_one;
if ( Num_one >= Num_two )
{
cout << endl << "The sum of all numbers from " << Num_1
<< " to " << Num_2 << " is: " << Range << endl;
}
}
// else if ( Num_one > Num_two ) // This works, but I "haven't learned" this
// technique yet...
if ( Num_1 >= Num_2 )
for ( Num_two = Num_two ; Num_two <= Num_one ; ++Num_two )
{
Range += Num_two;
if ( Num_one <= Num_two )
{
cout << endl << "The sum of all numbers from " << Num_1
<< " to " << Num_2 << " is: " << Range << endl;
}
}
cout << endl << "Press [Enter] to exit... ;-)" << endl;
cin.ignore(2); // To make the console window, wait for a keypress !
return 0;
}
__________________ Studying programming languages, you'll ALWAYS be a student ;-) Last edited by The SharK; 07-16-2006 at 08:50 AM. |
| The SharK is offline | |
| | #2 |
| The Richness... Join Date: Jan 2006 Location: Ireland
Posts: 469
| You typically don't use a cin.ignore to wait for a key press - you use cin.get. The thing is, when you read in a value using cin >> variable, say an int, when you press the enter key, the enter key is left in the input buffer. The call to cin.ignore just reads that value and discards it - if you use cin.ignore (2), and there is only one character in the buffer, it'll wait for another character (newline I mean) - thats's why you need cin.ignore (2). Since there is typically only one newline in the buffer, the usual format is: Code: //body of program . . . cin.ignore (); cin.get ();
__________________ No No's: fflush (stdin); gets (); void main (); Goodies: Example of fgets (); The FAQ, C/C++ Reference My Gear: OS - Windows XP IDE - MS Visual C++ 2008 Express Edition ASCII stupid question, get a stupid ANSI |
| Richie T is offline | |
| | #3 |
| C++ SharK Join Date: Mar 2004 Location: Denmark
Posts: 62
| Hi Richie T Oh, I see ! So I'll use: Code: cin.ignore(); cin.get(); then use cin.get() to wait for the new keypress "Enter/newline"
__________________ Studying programming languages, you'll ALWAYS be a student ;-) |
| The SharK is offline | |
| | #4 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Flush the input buffer Why does my program enter an infinite loop if the user inputs invalid data? (C++) Stop my Windows Console from disappearing everytime I run my program?
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Console window in VC 2008 | swgh | Tech Board | 0 | 06-05-2008 03:13 PM |
| My Window Class | Epo | Game Programming | 2 | 07-10-2005 02:33 PM |
| Pong is completed!!! | Shamino | Game Programming | 11 | 05-26-2005 10:50 AM |
| Problem with creating new window, from another window | Garfield | Windows Programming | 6 | 01-11-2004 02:10 PM |
| Tab Controls - API | -KEN- | Windows Programming | 7 | 06-02-2002 09:44 AM |