Newbie: help me please!!!

This is a discussion on Newbie: help me please!!! within the C++ Programming forums, part of the General Programming Boards category; Now that I've figured out how to maintain an open DOS window with cin.get, whenever I run my program and ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    1

    Newbie: help me please!!!

    Now that I've figured out how to maintain an open DOS window with cin.get, whenever I run my program and press ENTER, the window automatically closes? Does anyone know why this is and how to fix it? T|-|4|\||< j00!












    L33T R|_|L35!

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Now that I've figured out how to maintain an open DOS window with cin.get
    *stabs fork through eye*

    -edit-
    But honestly speaking....The reason it closes is because your program finishes running. Without a cin.get() (or getch() ) at the end, it finishes running and closes.

    -edit 2-
    example:

    Code:
        #include <iostream.h>
        
        int main()
        {
        	 cout << "Blah";
        // You won't see this text because
        // the dos console will go away right away
        
        	 return 0;
        }
    When you run this program, this is what happens:

    *DOS console pops up*
    Code:
     |------------------------------------------|
     |DOS Console, wheeee!			    |
     |------------------------------------------|
     |					    |
     | Blah					    |
     |					    |
     |------------------------------------------|
    *DOS console pops away, right away, before your eyes have a chance to even process what was written*

    However, if you put a cin.get() in there or some other method of pausing the program, it will wait until

    a) the user pressed enter (with cin.get())
    b) any key is pressed (with getch())
    c) the program deems it's finished (with a while loop)

    So add an extra step in there right before "DOS console pops away" like so: "DOS console stays active, waiting for input or doing some other sort of processing"
    Last edited by jverkoey; 07-02-2004 at 11:09 PM.

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    man im impressed you took the time to draw that dos box !

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb #include<cstdlib>

    I would like to offer
    Code:
    system("pause");
    as a method to halt your program until a keypress.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by The Brain
    I would like to offer
    Code:
    system("pause");
    as a method to halt your program until a keypress.
    thats overkill for a simple app. heres (a better explanation than i can give) why.

    i've seen several people use system("PAUSE") when they want to delay their programs. i'm not sure who's teaching this method, but it's a bad habit.
    by calling system(), you are invoking the default shell. the shell then executes the commandline given to it ("PAUSE", in this case). that is, it runs the 'pause.exe' program. so, now your simple c program is relying on two external programs for a stupid thing like pressing a single key. what if someone deleted or renamed 'pause.exe'? what if someone tried to compile your program on unix, or a mac? it wouldn't work. you'd get an annoying shell message and no pause. besides, why have the overhead of launching 2 programs, when you can accomplish the same thing in c?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    FAQ!

    And say it with me now: "Leet speak isn't cool"

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Wh47, 7h4n705?

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    C++:

    Code:
    #include <iostream>
    
    void wait()
    {
        std::cin.clear();
    
     // std::cin.ignore( std::cin.rdbuf()->in_avail() );
        std::streambuf * pbuf = std::cin.rdbuf();
        std::streamsize size  = pbuf->in_avail();
        std::cin.ignore(size);
    
        std::cin.get();
    }
    
    int main()
    {
        //...
        wait();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 07:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 08:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21