Thread: Pressing A Button on Dos..

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    25

    Pressing A Button on Dos..

    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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    it just closes man.. i dont know why i put the age i put 100 and then press enter and then it just closes..
    im still a noob...

    ....but im trying my best to help people out

    ...doing that will help me understand more and more about c++


  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    anyone be kind to help me alittle bit more pls
    im still a noob...

    ....but im trying my best to help people out

    ...doing that will help me understand more and more about c++


  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    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
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    #include <conio.h>

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

    or

    #include <windows.h>

    sleep(5000); //waits 5 seconds, then closes
    return0;
    PHP and XML
    Let's talk about SAX

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>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
    Code:
    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
    Code:
    #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;
    }
    *Cela*

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    24
    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
    }

  9. #9
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    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:
    Code:
    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;
    }
    Last edited by Carlos; 01-30-2003 at 07:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. screen color detection and button pressing
    By cwn11 in forum C Programming
    Replies: 1
    Last Post: 11-23-2008, 10:39 PM
  2. Virtually pressing a button
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2005, 10:19 AM
  3. Pressing a button works sometimes
    By johny145 in forum Windows Programming
    Replies: 14
    Last Post: 05-18-2005, 11:53 AM
  4. Pressing a button in another app
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2002, 12:53 PM
  5. DOS program versus DOS console program
    By scromer in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2002, 01:42 PM