Thread: cmd close's too soon...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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)

  2. #2
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

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