window closing prematurely

This is a discussion on window closing prematurely within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { char name[20]; cout<<"Please enter your name: "; cin>> name[20]; cin.ignore(); cout ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    window closing prematurely

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char name[20];
    
    cout<<"Please enter your name: ";
    cin>> name[20];
    cin.ignore();
    cout << "\n" <<name<< "" <<endl;
    cout << "Press enter to exit" << endl;
    cin.ignore();
    return 0;
    }
    After the name is enterd, and the user presses enter, the window closes without doing the other things.

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Add a cin.get() at the end to pause the program.

    get rid of the [20]

    near cin>>name[20]
    Last edited by treenef; 08-15-2005 at 05:27 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Read the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Learn the C++ string class even if your tutorial doesn't teach it first. It is preferred over C style strings (that you are using) in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM

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