Reputation: 97
My program skips some code when I use the getline(cin,variablehere) function. I don't know whats wrong with the code. See the output below
#include <iostream>
#include <string>
using namespace std;
int main()
{
string getfirstname;
string lastname;
string address;
int contactnumber;
cout << "Enter First name : ";
getline(cin, getfirstname);
cin.ignore();
cout << "Enter Last name : ";
getline(cin, lastname);
cin.ignore();
cout << "Enter Address : ";
getline(cin, address);
cin.ignore();
cout << "Enter Contact number : ";
cin >> contactnumber;
cin.ignore();
CurrentNumberOfContacts += 1;
cout << "Successfully added to contact list!" << endl << endl;
cout << "Would you like to add another contact ? [Y/N] ";
cin >> response;
//more lines of codes below
return 0;
}
I have inputed 'int' as data type because it will contain numbers only
Upvotes: 0
Views: 3937
Reputation: 11
The answer provided by @Galic is quite good but If you want to read a line of characters without discarding the leading spaces you need another solution.
You could do:
char a='\n';
while (a=='\n')
{
cin.get(a);
}
cin.unget();
before doing your first getline. This assumes no trailing space resulting from a previous cin and that your first input line is not empty.
Upvotes: 1
Reputation: 48635
I recommend removing all the cin.ignore()
commands.
One of the problems with user input is that the >>
operator does not take the RETURN character out of the stream so if you follow it with a getline()
the getline()
will read the RETURN character instead of what you want to type in.
So I would change all your getline()
to this:
// cin >> ws will skip any RETURN characters
// that may be left in the stream
getline(cin >> ws, lastname);
Also remove all of your cin.ignore()
commands. They are not doing anything useful when used after a getline()
command and if you change your getline()
commands as I showed they should not be necessary at all.
So this should work:
int main()
{
string getfirstname;
string lastname;
string address;
char response;
int contactnumber;
int CurrentNumberOfContacts = 0;
cout << "Enter First name : ";
getline(cin >> ws, getfirstname);
cout << "Enter Last name : ";
getline(cin >> ws, lastname);
cout << "Enter Address : ";
getline(cin >> ws, address);
cout << "Enter Contact number : ";
cin >> contactnumber;
CurrentNumberOfContacts += 1;
cout << "Successfully added to contact list!" << endl << endl;
cout << "Would you like to add another contact ? [Y/N] ";
cin >> response;
//more lines of codes below
return 0;
}
Strictly speaking not all of your getline()
functions need to employ the cin >> ws
trick. I suppose the (incomplete) rules are as follows:
If you use a std::getline()
after a >>
then use:
std::getline(cin >> ws, line);
Otherwise just use:
std::getline(cin, line);
Upvotes: 4
Reputation: 125
2 things. First, you don't really need cin.ignore() in this case as your using
getline().
before
cin >> variable
Second, I don't know why your program doesn't run, but I would suggest using a
getline()
call and see if that works. But I see no reason why your code is not working.
Upvotes: 1
Reputation: 8603
cin >>
and getline
do not cooperate very well. They have different strategies for how to deal with whitespace. getline
removes the newline character, but cin >>
leaves it. This means that after you use cin >>
to read something, there will be a newline character left waiting in the input stream for the next getline
to "use". Which means it will read an empty line into the string.
Upvotes: 1