![]() |
| | #1 |
| Registered User Join Date: Jun 2009 Location: Adeliade, AU
Posts: 128
| getline(cin,input) 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;
}
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 | |
| | #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 | |
| | #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 | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Do the same thing as what? |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: Jun 2009 Location: Adeliade, AU
Posts: 128
| The same thing as the getline function |
| Aliaks is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| And you didn't use the >> suggestion because? |
| tabstop is offline | |
| | #7 | |
| Registered User Join Date: Jun 2009 Location: Adeliade, AU
Posts: 128
| as I said before . . . . Quote:
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 | |
| | #8 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #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 | |
| | #10 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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 | |
| | #11 | |
| Registered User Join Date: Jun 2009 Location: Adeliade, AU
Posts: 128
| Quote:
Thanks | |
| Aliaks is offline | |
| | #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
}
|
| Daved is online now | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|