Thread: Problem with cin...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Problem with cin...

    How can I do it so that when I use cin to store an array, it keeps on storing even after a space, so that it is something like this:
    Code:
        char Name[20];
        cin >> Name;
        cout << Name << endl;
    and lets say I input "Marcos Marin", and then it outputs "Marcos Marin", not just "Marcos". see what I mean?

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Use getline():
    Code:
    char name[20];
    cin.getline(name, 20, '\n');

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Now im just a newbie but i dont see the problem since a space is a char just like any other, it doesnt count as a null or anything. So if you type the whole string at once and enter, the if i outputs anything it should output the whole string?
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks that worked, but now it seems that I have to press enter twice when I enter something, and its kind of annoying, is there any other way? thaks for your help anyway.
    Why drink and drive when you can smoke and fly?

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thernkst: For some strange reason it kind of stops stroing data after a space.
    Why drink and drive when you can smoke and fly?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "Thernkst: For some strange reason it kind of stops stroing data after a space."

    It's not strange at all, but that's besides the point. The operator >> is defined to work that way: it ignores any whitespace preceding the input and stops reading input when it encounters a space. So, input like this is all equivalent:
    Code:
    Mark Sam Joe
        Mark     Sam    Joe
      Mark Sam    Joe
    "...but now it seems that I have to press enter twice when I enter something, and its kind of annoying, is there any other way?"

    That means you must be using VC++6.0 as your compiler. Having to hit return twice to enter input when using getline() is a result of a bug in the compiler. You can fix it by changing a line of code in the system file as described here:

    http://support.microsoft.com/default...;EN-US;q240015
    Last edited by 7stud; 04-21-2003 at 12:25 AM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Of course that idiotic bug fix article doesn't tell you what file the code is in. After an exhaustive search, I found it in the file:

    C++Microsoft Visual Studio\VC98\Include\String

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  2. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 08:43 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with cin
    By ErionD in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 11:27 AM