getting input from user
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
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:
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.
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:
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.
What it was probably shooting for was the following:
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!]
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!]
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: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.string bank;cout << "Input Bank Name :";std::getline(cin, bank);
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
Quote:Original post by bigjoe11aPost your full source (be sure to use [source] tags).
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
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
}
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
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement