Thread: Window closes everytime I push enter

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Window closes everytime I push enter

    Hey guys, I'm using Dev-C++ compiler for my coding, but everytime I write a program that prompts the user to do input, and they press enter to well, enter the input, the program closes on me without letting me see the result. How do I get around this? I've tried doing

    cin.get() and

    cin.ignore(cin.rdbuf()->in_avail()+1)

    None of these seem to work. Anyone got any ideas? Thanks.

    -Kai

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    The other reason could be that you are getting a fatal runtime error, but that's unlikely

  3. #3
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    You're using cin to get that input, right? cin 'looks' at the input stream, and reads everything until whitespace. It then leaves the whitespace in the stream (newline for sure - spaces, tabs, etc, suddenly I'm not so sure about...).

    Anyway, so the user enters some input and hits enter. Your program takes the input, assigns it to whatever variable, and leaves the '\n' in the stream. Then you're program's almost done, and gets to cin.get(). cin.get() looks at the stream, grabs the newline, and continues on with life - which is why your window closes, provided all my assumptions about your program are correct. If you're going to go between cin and cin.get(), use cin.ignore() between cin and cin.get().
    There is a difference between tedious and difficult.

  4. #4
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    try getch(); maybe...??

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Put a

    Code:
    system("pause")
    at the end of your program..

    and i will see the results...

    bb

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    getch() is not standard, system() leaves security leak.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    some extra info.

    maybe this works too?

    Code:
    cin.clear();
    cin.ignore(1000, '\n');
    cin.get();
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Banzai10
    Put a

    Code:
    system("pause")
    at the end of your program..

    and i will see the results...

    bb
    Quote Originally Posted by ReLiEnThAwK
    try getch(); maybe...??
    Both are bad. Use Decrypt's method...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  4. Dos Window closes after I press enter
    By ProgrammingDlux in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2002, 12:13 PM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM