simple question (cout << "two words")

This is a discussion on simple question (cout << "two words") within the C++ Programming forums, part of the General Programming Boards category; I'm just learning C++, so please dont tell me I'm stupid. I know I'm stupid, but anyway. I'm writing a ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    simple question (cout << "two words")

    I'm just learning C++, so please dont tell me I'm stupid. I know I'm stupid, but anyway.

    I'm writing a simple password program, using nothing but a while loop, cin and cout. It works fine, except when i try to enter two words as the password.
    example(program output is in bold, i think):

    Enter password:
    foobar
    Password accepted.

    but when i run it and enter two words, it echos Enter password twice:
    Enter Password:
    foo bar
    Enter password:
    Enter Password:

    any help would be appreciated. I'm using Dev-C++ on windows 98, if it matters. Thanks in advance.


    bleach

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    When using "cin >>" it will only read up to the first space.
    You want to use cin.getline
    Code:
    char input[255];
    cin.getline(input,254,'\n');
    That will get either 254 chars, or a full line, whatever comes first. When getting the whole line, it will drop the \n and append a null character to the end of the string, input in this case

  3. #3
    Unregistered
    Guest

    Smile

    Another way using C++ strings is:

    #include <string>
    string input;
    cin.getline(input);

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    If you use the string class you CAN'T call the cin member function getline(), you must call the getline helper function from string.h

    string input;
    getline(cin,input);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple cout formatting question.
    By guitarist809 in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2006, 11:59 AM
  2. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 12:59 PM
  3. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 07:59 AM
  4. Very simple question
    By ZephidsEmbrace in forum C++ Programming
    Replies: 6
    Last Post: 07-27-2003, 02:21 AM
  5. Simple question...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2001, 11:36 AM

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