Thread: Window Wont Stay Open

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    14

    Window Wont Stay Open

    I use the latest version of Dev C++, with the following code:

    Code:
    #include <iostream.h>
    int main()
    {
        char response[25];
        cout << "Hello World";
        cin >> response;
        cout << "Oh thats nice";
        return 0;
    }
    And when this executes it'll wait for my input and then immediatly close, without displaying "Oh thats nice". How do I stop the window from closing?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Hello wes welcome to the boards heres a link that will answer all your questions
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Woop?

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    14
    I can't believe I missed that in the FAQ!

    Well, this does the same thing, and it exits early:

    Code:
    #include <stdio.h> 
    #include <iostream.h>
    
    int main(void)
    {
      int ch;
      char response[25];
      cout << "What's up?";
      cin >> response;
      cout << "That's cool...";
      printf ("Press [Enter] to continue");
      while ((ch = getchar()) != '\n' && ch != EOF);
      return(0);
    }
    What's wrong? Thanks for the help

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Because your cin is leaving a newline in the input stream btw: i would use this instead considering your using c++
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    int main(void)
    { 
      char response[25];
      cout << "What's up?";
      cin >> response;
      cout << "That's cool...";
      cin.ignore(80,'\n');//so that it will stop at the end of the program
      cout<<"Press [Enter] to continue";
      cin.get();//this will wait for a key press
      return 0;
    }
    Woop?

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    14
    I'm such a newbie

    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 07:23 AM
  2. How to change window style at runtime?
    By Mr. Bitmap in forum Windows Programming
    Replies: 5
    Last Post: 06-09-2002, 04:49 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM