C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-05-2009, 09:55 PM   #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.
Aliaks is offline   Reply With Quote
Old 07-05-2009, 10:04 PM   #2
Registered User
 
Join Date: Dec 2008
Posts: 66
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?
Ronix is offline   Reply With Quote
Old 07-05-2009, 10:06 PM   #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?
Aliaks is offline   Reply With Quote
Old 07-05-2009, 10:17 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Do the same thing as what?
tabstop is offline   Reply With Quote
Old 07-05-2009, 10:25 PM   #5
Registered User
 
Join Date: Jun 2009
Location: Adeliade, AU
Posts: 128
The same thing as the getline function
Aliaks is offline   Reply With Quote
Old 07-05-2009, 10:39 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
And you didn't use the >> suggestion because?
tabstop is offline   Reply With Quote
Old 07-05-2009, 10:46 PM   #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 . . . .

Quote:
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.
Aliaks is offline   Reply With Quote
Old 07-05-2009, 10:49 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-05-2009, 10:50 PM   #9
Registered User
 
Join Date: Jun 2009
Location: Adeliade, AU
Posts: 128
and that would be the only way to do it?
Aliaks is offline   Reply With Quote
Old 07-05-2009, 10:58 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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).
tabstop is offline   Reply With Quote
Old 07-05-2009, 11:15 PM   #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
Aliaks is offline   Reply With Quote
Old 07-05-2009, 11:24 PM   #12
Registered User
 
Join Date: Jan 2005
Posts: 7,097
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.
Daved is online now   Reply With Quote
Old 07-05-2009, 11:30 PM   #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
Aliaks is offline   Reply With Quote
Old 07-06-2009, 11:45 AM   #14
Registered User
 
Join Date: Jan 2005
Posts: 7,097
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.
Daved is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 01:17 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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