Ok, I have looked through the forum and it looks like I am doing what I am supposed to do. I am using a class, and trying to get my getline function to work to pull in a whole name. I have the include <string> header file. When we go down to the function, the getline command is not really getting a line. Its like the code completely skips the fact that it should be taking in input when it comes to the biller. This is what I got.
void bill::aBills()
{
// Add Bills to the application.
system("cls");
cout << "This is the Add Bills screen.\n";
char choice;
do // Runs question until a correct response is made.
{
cout << "\n\nPlease select a category that matches the bill: "
<< "Medical (M), House (H), Credit (C): ";
cin >> choice;
choice = toupper(choice);
switch(choice) // Gives user a choice of the categories to choose from.
{
case'M': bCategory = "Medical";
break;
case'H': bCategory = "House";
break;
case'C': bCategory = "Credit";
break;
default : cout << "Incorrect Selection, please try again!\n\n";
system("pause");
break;
}
}while ((choice != 'M') && (choice != 'H') && (choice != 'C'));
cout << "\n\nPlease insert the bill account #: ";
cin >> account;
cout << "\n\nPlease insert the name of the Biller: ";
getline(cin, bName);
setCategory(bCategory);
setAccount(account);
setBiller(bName);
system("pause");
}
Please help me to figure out what I am doing wrong, or missing. The code that I am getting a problem on is line 37 and 38. It does not seem to be allowing input. When I run the program, it goes to the cout, and the getline does not allow the user to input data. Please help! What am I doing wrong?? Trying to get it to take in a full name, with spaces.
Mixing cin and getline doesn't always work the way you expect. This is because cin leaves the '\n' character in the stream after extracting, and then getline will just eat up that and keep going. Instead, you probably want to throw in a call to ignore(), like so:
1 2 3 4 5 6 7 8
std::cin >> account;
// either remove one character from the stream, hopefully '\n'
std::cin.ignore();
// or remove every character up to and including '\n'
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, bName);
Either is fine, I prefer the second one (in case the user randomly threw in a bunch of junk after the account name). Keep in mind that for the second one you need to #include <limits> .
cin >> account;
cin.ignore();
cout << "\n\nPlease insert the name of the Biller: ";
getline(cin, bName);
or the 2nd option
1 2 3 4 5 6 7
cin >> account;
// not too sure about how this should be worded. Please explain if you will.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n\nPlease insert the name of the Biller: ";
getline(cin, bName);
Yes. The second one is literally this: The function will go through the input stream, character by character. Every character it just throws away (ignores), until it reaches a newline character ('\n'). Then, it throws away that character too and the function ends, and the program continues. This is to remove all the random things that they may have typed in after inputting 'account'. In case you are wondering about the 'numeric_limits' stuff, that parameter specifies the maximum number of characters to ignore. If you pass the maximum number of characters that can be held in the stream (the max number for streamsize) then you are guaranteed to have removed every unnecessary character from the input stream. As I said, I generally prefer the second option. Hope this helps!