PDA

View Full Version : Pressing A Button on Dos..



yakabod
01-24-2003, 08:00 PM
Ok im still a newbie so i make just simple lil progs. Now when i compile and then run it (For Example) It will ask me my age. then i enter 100 then press ENTER. The Dos Closes. i tried the cin.get(); but didnt work also. I have XP and using the newbie compiler the Dev-C++. So please somoene help i would really appreciate it :)

Hammer
01-24-2003, 08:19 PM
I'm going to guess that the cin.get() is eating the newline left over from the read of the number. Lookup cin.ignore(), and use that as a method of clearing the input buffer after you've entered your number.

yakabod
01-24-2003, 08:23 PM
:( it just closes man.. i dont know why i put the age i put 100 and then press enter and then it just closes..

yakabod
01-24-2003, 10:41 PM
anyone be kind to help me alittle bit more pls

ronin
01-24-2003, 11:25 PM
just use another variable

cout << age << endl;

cin >> junkvariable;

return 0;

I get that too using Dev C (ver 4.??), but it does have system("pause") that you can use provided that you include stdlib.h

Waldo2k2
01-25-2003, 03:20 PM
#include <conio.h>

getch(); //user hits enter to close
return 0;

or

#include <windows.h>

sleep(5000); //waits 5 seconds, then closes
return0;

Cela
01-26-2003, 02:40 PM
>>sleep(5000); //waits 5 seconds, then closes
With windows.h it's Sleep, not sleep. :-)

>>So please somoene help i would really appreciate it
Sometimes there's more than just one character left in the stream, the easiest way to fix that is using


system("PAUSE");

It's not the best way since it's not portable and really slow compared to everything else. Or you could do something like this and sidestep the problem


#include <iostream>

using namespace std;

istream& discard(istream& in)
{
char ch;

while (in.get(ch) && ch != '\n')
{}

return in;
}

int main()
{
char pause;

// Do lots of stuff

cin>> discard >> pause;
}

PawelK
01-29-2003, 03:26 AM
Try this one

#include <conio.h>
#include <iostream.h>

int age;

void main()
{
cout << "Age:";
cin >> age;
cout << "You are" << age << "old";
getch(); //wait until you press a button
}

or try this one

#include <dos.h>
#include <iostream.h>

int age;

void main()
{
cout << "Age:";
cin >> age;
cout << "You are" << age << "old";
sleep(1); //wait one secound until the window close
}

Carlos
01-30-2003, 07:46 AM
Why don't you first open a DOS command promt, and run your application from it, instead of letting your app open it's own DOS window?

You could that simply avoid this mess :)

Just keep things simple!

If you really want an elegant solution, prepare a loop, and check for e.g. Esc key:


void main()
{
cout << "Age:";
cin >> age;
cout << "You are" << age << "old\n\n";

cout << "Press Esc to exit..."; // let the poor user know what's happenning, or else he'll press Ctrl-Alt-Del ;)

bool do_loop = true;
int ch;
while (do_loop)
{
ch = getch(); // read keys
if (!ch)
ch = getch(); // for extended keys!
if(ch == 27) do_loop = false;
}