cmd close's too soon...

This is a discussion on cmd close's too soon... within the C++ Programming forums, part of the General Programming Boards category; Hello! I need some help on how to stop the consol from closing. I'll put a <---- Where it cuts ...

  1. #1
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33

    cmd close's too soon...

    Hello! I need some help on how to stop the consol from closing. I'll put a <---- Where it cuts off.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    int a, b, c;
    cout << "Hello!\nPlease Enter Any Whole Number:";
    cin >> a;
    cout << "Alright!  You entered ";
    cout << a;
    cout << "\nAlright! Now Enter Another Whole Number:";
    cin >> b;
    <----- (Cuts off here)
    cout << "Alright!  You entered ";
    cout << b;
    cout << "\nNow where going to add ";
    cout << a;
    cout << " and ";
    cout << b;
    cout << "!\n";
    c = a + b;
    cout << c;
    return 0;
    }
    I have no idea on how to make it wait to the end to close. Any help please? (I didn't comment the code since it's so basic.)
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    add a, im still new at this to...i had this problem as well
    Code:
    cin.get();
    so it waits for enter at the end

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your program works fine.
    If you mean with "program cuts off" that the window closes before you can see the output. Call it from the commandline or search the forum.
    Kurt

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Common problem - the second half of your program is entirely output. Once all the text has been written to the screen, the computer considers the program finished, so the console window in which it was running is finished. The simple solution is just to request some user input at the end, like rodrigorules suggested. (Running the program in a console like ZuK said would work - but it's a bit of an inconvenient solution most of the time). You can find several other solutions in the FAQ, and there are pros and cons of each - just pick one that works for you.

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by rodrigorules
    add a, im still new at this to...i had this problem as well
    Code:
    cin.get();
    so it waits for enter at the end
    Well, hasn't cleared the stream, so he would need two cin.get(); or a cin.ignore() and cin.get().

    dimirpaw, its in the FAQ.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    or you could just add:
    Code:
    while(2 == 3)
    {
    }
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by ElastoManiac
    or you could just add:
    Code:
    while(2 == 3)
    {
    }
    LOL, that made my day. I've seen that one before.I can't tell if you were joking or not.

    It doesn't work though because 2 doesn't equal 3. It turns into while(0) which never gets executed.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    i was joking...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    all depends if you put while after the code it would generate the code at least once. but still yet you still have to have cin.get();

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Raigne
    all depends if you put while after the code it would generate the code at least once. but still yet you still have to have cin.get();
    Well his code wasn't a do...while.

    That was funny ElastoManiac, don't get many jokes around here. There isn't even a laugh smilie.... ....
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33
    Yep I used cin.get(); more then once it wont work because when you input a number it takes it as your pressing enter to quit, and yes I'm reading the FAQ's, however I have other things to do so I cannot read it all now... However I'll try what Dae said, thanks.

    EDIT: Thanks Dae's works now!

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    int a, b, c;
    cout << "Hello!\nPlease Enter Any Whole Number:";
    cin >> a;
    cout << "Alright!  You entered ";
    cout << a;
    cout << "\nAlright! Now Enter Another Whole Number:";
    cin >> b;
    cout << "Alright!  You entered ";
    cout << b;
    cout << "\nNow where going to add ";
    cout << a;
    cout << " and ";
    cout << b;
    cout << "!\n";
    c = a + b;
    cout << c;
    cin.get();
    cin.get();
    return 0;
    }
    Last edited by dimirpaw; 11-26-2005 at 03:30 PM.
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

  12. #12
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by Dae
    There isn't even a laugh smilie.... ....
    ( this isn't a laugh, more like a happy expresion )
    Yeah, i miss smilies too, Why doesn't someone add some?
    Missing expresions:
    - angry
    - good
    - ........ed off
    - crying
    - ...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  13. #13
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by dimirpaw
    Yep I used cin.get(); more then once it wont work because when you input a number it takes it as your pressing enter to quit, and yes I'm reading the FAQ's, however I have other things to do so I cannot read it all now... However I'll try what Dae said, thanks.
    Actually, the entire code is being processed and printed. When the program reaches the end of main() it quits. You just aren't able to see the parts after you enter the second number being printed because it prints them, and reaches the end of main() where it quits. Its hard to see whats printed before it reaches the end and knows it needs to quit. I'm able to see the things printed before the app closes, for a split second. The reason it doesn't quit and asks you for numbers is because cin halts the position in the code and waits for input, so it hasn't reached the end of main(). The same goes for cin.get(), it halts the program and it waits for one character to remove from the stream.

    When you enter something in cin you push enter, the part entered is extracted and stored in the variable, but the 'enter' command is still in the stream.

    So when you call cin.get() later in the program (and you've called cin), you still have that enter command in the stream, so it simply extracts that and continues with the program, so you have not paused at all - just removed that one command. So you call cin.get() again to wait for another character because theres nothing in the stream (unless you didn't extract all the rest of the characters from the stream, which isn't done often). So it pauses, and waits for the enter command. I think its '\n' in the stream, but I'm not sure.. I haven't read up on much of C++ in a few months. If I'm wrong somewhere you can be sure someone will mention it or elaborate on it.

    Or cin.ignore() first will clear the entire stream, and then you can call cin.get() which will wait for a character to extract from the stream since cin.ignore() just cleared it.

    That was longer that it should have been >.<, read http://www.cplusplus.com/ref/iostream/istream/get.html for more info - cplusplus.com is a good reference for the details of the library.
    Last edited by Dae; 11-26-2005 at 03:54 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #14
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33
    Alright, I didn't read all of what you said since I gota go in a few minutes. I got a lot of C++ Books about 20 or so, on Game Programming, Database's, ect... I have this really big one called Visual C++ 101 Tips, not bad. (Most of the stuff in there can be related to any other compiler.)

    Well I hope I can learn C++, personally I don't care to Learn C then C++, I'm already in Expert in Visual Basic with well over 6 years of experince. Not bad it wouldn't really matter for learning C++. Only the concepts about int, ect... I can carry on.
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

  15. #15
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    This is much simplier, you just press enter at the end of program - and thats it.
    Code:
    #include <conio.h>
    
    // at the end
    cout<<"Press a key to continue...";
    getch();
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 08:35 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Error running program in cmd - cygwin.dll missing -
    By JFonseka in forum C Programming
    Replies: 5
    Last Post: 08-27-2007, 01:12 PM
  4. Program closes when startup form closes
    By dcboy in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 03:40 AM
  5. Little help with CMD
    By robid1 in forum C Programming
    Replies: 2
    Last Post: 02-23-2003, 04:09 AM

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