CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: getline

  1. #1
    Join Date
    Mar 2002
    Posts
    7

    getline

    I'm a new user to C++ (I know up to some intermediate Java though). Java has the readln function that can take a whole line and put into a string. I know C++ doesn't do strings aside from char [max] and the string class, so I did #include <string> and the string works well, except when I try to import a line into the string. For instance,
    cout << What is your name?
    I know cin only reads up to the space and sends this back. So I did some research and foudn getline. However, getline works fine only when it is the first input/output i'm doing in the program( in my main class). If I do some input in a menu style function, and then call on an input function, where I ask for the person's name ala:

    string name;

    cout << "what is your name?";
    getline(cin, name);
    cout << "hello " << name;
    cout << "how old are you?"
    cin >> age;

    The program will not even stop to let me input my name. For instance:
    What is your name? Hello, How Old are you?

    It then pauses for me to input my age. What's wrong here though???
    Is there a simple way to input a line into string or cstring or anything like that? ANy help would be GREATLY appreciated.


  2. #2
    Join Date
    Mar 2002
    Posts
    7

    Re: getline

    I couldn't find the edit button unfortunatlely, but I have this to add to my previous post, perhaps it will elucidate the problem further:

    Just a comment to add about the post above ( I couldn't find an edit button, I'm sorry):

    void main()
    {
    void test2();
    string name;
    double apples;

    cout << "what is your name?";
    getline (cin, name);
    cout << "How many apples do you eat?";
    cin >> apples;
    cout << "In method one" << name << " eats " << apples << " apples"
    test2();
    }

    void test2()
    {
    string name, yesno;

    cout << "we are in method2. What is your name?";
    getline (cin, name);
    cout << "do you like apples?";
    getline (cin, yesno);
    cout << "In method two, does " << name << " like apples? " << yesno;
    }

    When I do this, method waits for input on both commands, but method two again skips the first input chance and skips right down to do you like apples after printing what is your name. Meanwhile, if instead of asking # of apples in method one, I asked do you like apples yes/no and took the input into a string via getline so that all input in both methods was via getline, it allows me a chance for input at the correct times. Writing numbers to the cin screws something up somewhere. Does anyone know what I can do to fix this??? Any help would be greatly appreciated.


  3. #3
    Join Date
    Jun 2001
    Posts
    362

    Re: getline

    I think the problem is that after

    cin >> apples;



    the cin buffer still contains '\n'.
    getline reads from the cin buffer until it reads the delimiting cahracter (which is '\n' by default, I think). So when you then do

    getline (cin, name);



    getline reads until it sees a '\n' as usual. But this time the '\n' is already in the buffer so it doesn't even stop to let the user input anything.

    If this is actually the problem, you can probably fix it by clearing the cin buffer after getting the number:

    cin >> apples;
    cin.ignore(10,'\n');




    As a sidenote, it's not good practice to declare functions in side of functions like you do with

    void main()
    {
    void test2();
    ...
    ...
    }




    You should do it outside the function:

    void test2();

    void main()
    {
    ...
    ...
    }




    Hope this helps.

    Brandon


  4. #4
    Join Date
    Mar 2002
    Posts
    7

    Re: getline

    Thanks for your help. I got aroudn it by just writing getline(cin, string) before the "question" statement and then again after. This clears the cin I guess. How does your cin.ignore work? What are the paramaters??? THanks for your help and your pointer on the method declerations (i'm just starting C++ after getting pretty far with Java and we don't do method declerations in java hehe ).


  5. #5
    Join Date
    Jun 2001
    Posts
    362

    Re: getline

    it is defined as (cin is a type of basic_istream):

    basic_istream& ignore(
    streamsize _Count = 1,
    int_type _Delim = traits_type::eof( )
    );




    Parameters
    _Count
    The number of elements to skip from the current read position.
    _Delim
    The element that, if encountered before count, causes ignore to return and allowing all elements after _Delim to be read.

    See http://msdn.microsoft.com/library/de...reamignore.asp for details.

    Brandon


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured