Jump to content
  • Advertisement
Sign in to follow this  
bigjoe11a

getting input from user

This topic is 3906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't loaded c++ for dummies. and one of the options it had was how to get a string of chars or letters from the user. string bank; cout << "Input Bank Name :"; cin.getline(bank, 50); How ever, every time I compile It get this error. savings.cpp(16) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::string' to 'char *' Can some one help me with this. It's bad that std was a pain. I follow the book to the t and I still get errors. Joe

Share this post


Link to post
Share on other sites
Advertisement
The member version of getline() expects a char*, and an integer indicating the maximum number of characters to be read (and, optionally, a delimiter to be used).

What you want is the non-member getline() function:
string bank;
cout << "Input Bank Name :";
std::getline(cin, bank);
Note that the non-member version does not take a 'buffer size' argument for a reason: std::string objects manage their own storage and resize as needed, so there's no need to worry about arrays, buffer sizes, and so on.

Share this post


Link to post
Share on other sites
cin.getline() expects a char* for the first argument. What you're probably looking for is std::getline(), which takes a stream (cin in this case) and a string.

In code:
string bank;
cout << "Input Bank Name: ";
getline( cin, bank );
This will also allow strings longer than some arbitrary length chosen as a limit.

Share this post


Link to post
Share on other sites
What it was probably shooting for was the following:
string bank;
cout << "Input Bank Name :";
std::getline(cin, bank);

If you're using namespace std, you don't need the std:: before getline, but you should know where the function resides.

The bottom line is that it sounds like you need better learning material. Might I suggest Thinking In C++ or C++: A Dialog? Both free online, and invaluable when learning C++.

[Edit: Double drat!]

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
The member version of getline() expects a char*, and an integer indicating the maximum number of characters to be read (and, optionally, a delimiter to be used).

What you want is the non-member getline() function:
string bank;
cout << "Input Bank Name :";
std::getline(cin, bank);
Note that the non-member version does not take a 'buffer size' argument for a reason: std::string objects manage their own storage and resize as needed, so there's no need to worry about arrays, buffer sizes, and so on.


Well I have it setup in a loop so that when the user press x that ends the input. Its just that it seems to be doing doubles. It seems to prompt more then 2 times at one time. If you get the idea. when you run the program it looks like this

Enter Bank Name :

Enter Bank Name :

any ideas

Share this post


Link to post
Share on other sites
Quote:
Original post by bigjoe11a
Well I have it setup in a loop so that when the user press x that ends the input. Its just that it seems to be doing doubles. It seems to prompt more then 2 times at one time. If you get the idea. when you run the program it looks like this

Enter Bank Name :

Enter Bank Name :

any ideas
Post your full source (be sure to use [source] tags).

Share this post


Link to post
Share on other sites
Here you go

void bankinfo()
{

cout << "Input a Bank Name (input a x or X to end list)\n";


string bank;
while(true)
{

cout << "\n\nInput Bank Name :";
getline(cin, bank);
if(bank.compare("x") == 0 || bank.compare("X") == 0)
{
break;
}
banks.push_back(bank);
}

banks.sort();
while(!banks.empty())
{
string bank = banks.front();
cout << bank << endl;

banks.pop_front();

}


system("pause");

}

int main()
{
// my menu is here
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!