Problems with cin.get()

This is a discussion on Problems with cin.get() within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to input the title of a movie from the user. Here's the code that's giving me trouble: ...

  1. #1
    Stevek
    Guest

    Problems with cin.get()

    Hi,

    I'm trying to input the title of a movie from the user. Here's the code that's giving me trouble:

    typedef char string [100];
    string NewTitle;
    cin.get(NewTitle);

    I'm using MSVC++6 and it keeps giving me the error:

    "error C2664: 'class istream &__thiscall istream::get(char &)' : cannot convert parameter 1 from 'char [100]' to 'char &'
    A reference that is not to 'const' cannot be bound to a non-lvalue"

    I was using plain old cin>>, but my program would freak out whenever I entered whitespace....

    I'm pretty sure I've just screwed up the function syntax somehow, so if anyone can spot what I'm doing wrong, I'd appreciate it.

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    std::cin.getline(NewTitle, 100);

    try that.

    edited for error, fixed now.
    edit 2: cin.get() should work, but you need the same syntax as getline above.
    Last edited by alpha; 03-15-2003 at 10:38 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cin.get(NewTitle);
    The one argument definition of get only accepts a single char, for an array you must also include the size as the second argument:

    cin.get(NewTitle, 100);

    -Prelude
    My best code is written with the delete key.

  4. #4
    Stevek
    Guest

    new problem

    Thanks! The error's gone. But, a new problem's popped up in it's place. I have a cin statement before the cin.get() that seems to be overriding the cin.get().

    I'm not going to post all my program code (there's too much of it) so I just wrote a small test program that also has the same error in it:

    int main()
    {
    typedef char string [100];
    string NewTitle;

    int SomeNumber;
    cout<< "Enter a number: ";
    cin >> SomeNumber;
    cout << endl << SomeNumber << endl;

    cout << "Enter a title: ";
    cin.get(NewTitle, 100);
    cout << endl;
    cout << NewTitle << endl;

    return 0;
    }

    When I run it I'll get output like this:


    Enter a number: 3

    3
    Enter a title:

    Press any key to continue


    So as you can see, it looks like the cin.get() isn't being called at all; the program doesn't stop for input, I just entered "3" and it ended.

    Any ideas on how to fix this so that it won't fast-forward all the way to the end, past the cin.get() ?

    Thanks again

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Any ideas on how to fix this so that it won't fast-forward all the way to the end, past the cin.get() ?
    cin's >> operator leaves a newline in the input stream, get stops reading at a newline. This tends to be a bad combination, one solution is to place
    Code:
    cin.ignore();
    immediately after the first call to cin.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Stevek
    Guest

    All better

    Everything works now! Thanks, you've been a really big help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  4. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 12:58 AM
  5. cin.get problems
    By robid1 in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2004, 05:12 AM

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