getline(cin,input)

This is a discussion on getline(cin,input) within the C++ Programming forums, part of the General Programming Boards category; Ok I seem to be moving backwards with learning this language... I have two getline functions which are exactly the ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    getline(cin,input)

    Ok I seem to be moving backwards with learning this language...

    I have two getline functions which are exactly the same... yet one spits out an error.

    P:\KoA_Prog\main.cpp||In function `int main()':|
    P:\Koa_Prog\main.cpp|24|error: no matching function for call to `getline(std::istream&, int&)'|
    ||=== Build finished: 1 errors, 0 warnings ===|

    Code:
    #include <iostream>
    #include <sstream>
    
    #include "playerFunctions.h"
    #include "classes.h"
    //#include "playerInvent.h"
    
    using namespace std;
    
    int main()
    {
    
        cout << "Welcome to my learning curve C++ Adventure Game Version 1.\n\n\n";        // Introduces the game
    
        User NewUser;                                         //Create a User object
        std::string sTempUserName;                            //Create a temporary variable for input
        int iTempAge;                                         //Create a temporary variable for input
    
        cout << "Please enter the username of your choice: ";
        getline(cin,sTempUserName);                           //Store the user input in the temporary variable
    
        std::cout << "\nAnd now your Age: ";
        getline(cin,iTempAge);                                //Store the user input in the temporary variable
    
        NewUser.vSetUserName(sTempUserName);                  //Set the username
        NewUser.vSetAge(iTempAge);                            //Set the age
        return 0;
    }
    Now with the help of legit I thought I had wrapped my head around classes but im guessing there must be a hook in it somewhere, I fixed a few bugs so maybe I messed up something.

    The wierd thing is its only this line, so there must be a rule im missing.

    Edit:

    Oh and I have random std::'s in there because I got help by someone who uses it, where as I dont, and where i fixed stuff I removed it
    so tahts why it dodgy
    Last edited by Aliaks; 07-05-2009 at 09:58 PM.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    getline cannot be used to read any type but a basic_string. You could of course overload it, but it would make little sense. Can't you just use the >> operator?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ahhh tahts the missing rule Thanks.

    Yeah, I could, its just I once had that before and I noticed I could just entered all the varibles in 1 line if i put spaces between the words.. which I dont particually want.

    Is there a work around which would do the same thing but would make sense?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do the same thing as what?

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    The same thing as the getline function

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you didn't use the >> suggestion because?

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Quote Originally Posted by tabstop View Post
    And you didn't use the >> suggestion because?
    as I said before . . . .

    Yeah, I could, its just I once had that before and I noticed I could just entered all the varibles in 1 line if i put spaces between the words.. which I dont particually want.

    Is there a work around which would do the same thing but would make sense?
    I noticed when using a cin >> i could enter all cin data for any line.

    e.g. If i had 4 questions about details, by entering answer 1 [SPACE] answer 2 [SPACE] , etc
    or it was a comma i cant quite remember which it was now, I could fill all 4 inputs in a single line and i dont really want this.

    SO..

    I was wonderifn if there was a work around for it.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Aliaks View Post
    as I said before . . . .



    I noticed when using a cin >> i could enter all cin data for any line.

    e.g. If i had 4 questions about details, by entering answer 1 [SPACE] answer 2 [SPACE] , etc
    or it was a comma i cant quite remember which it was now, I could fill all 4 inputs in a single line and i dont really want this.

    SO..

    I was wonderifn if there was a work around for it.
    So ... don't do that? >> allows you to do that, but it doesn't require you to do that. If you want to forbid it, then you'll have to parse the input yourself, by reading in an entire line and making sure it contains one and only one number with no extra bits.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    and that would be the only way to do it?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Aliaks View Post
    and that would be the only way to do it?
    Well, yes. Obviously the bits are already there -- reading in one line to a string for example (you've seen getline), and parsing a number out of a string (meet strtol, which will also tell you whether there's more on the line).

  11. #11
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Quote Originally Posted by tabstop View Post
    Well, yes. Obviously the bits are already there -- reading in one line to a string for example (you've seen getline), and parsing a number out of a string (meet strtol, which will also tell you whether there's more on the line).
    Ok, ill give it a look over.

    Thanks

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    There are many ways to do this.

    tabstop's is a good one (although I might prefer stringstreams to strtol). Another is to just check the stream after you read the first integer. If the next character in the stream is not a newline then the user didn't hit enter after the input:
    Code:
    if (std::cin.get() != '\n')
    {
      // User didn't hit enter
      // use cin.ignore here to ignore extra characters
    }
    If you don't want to reject the entire input and you'd rather just ignore the extra stuff, then you don't even need to check for '\n'. Just always use ignore with some big number.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Oo I see

    I might try taht as im not so good with the language yet.

    Thanks

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Search the site for getline, cin, and ignore. You'll find lots of good examples, some easier than others to use or understand. If that reruns too many results you can just limit it to posts I've made. Hopefully you will see the comments and examples of others in the threads that come up.

Popular pages Recent additions subscribe to a feed

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