I'm working on a simple spreadsheet application, and I'm trying to have the user enter a short label for a cell. I'm trying to use the getline function to take in the string, but when I run my code, it's skipping right over the line in question without even asking the user for input. Abbreviated code is below:
1 2 3 4 5 6
string label;
cout << "Enter a label to place in your cell. Labels must be less than 10 characters in length. Ex: PART NO" << endl;
getline (cin, label);
S[i][j].s = label;
I have also tried the cin.getline function using label as a char array with the same result.
1 2 3 4 5 6
char label[9];
cout << "Enter a label to place in your cell. Labels must be less than 10 characters in length. Ex: PART NO" << endl;
cin.getline (label, 9);
S[i][j].s = label;
Why, when I run the program, is it skipping the getline and not allowing for input?
that's because you use cin >> somewhere before getline. when you type something and hit enter, the characters you typed and a newline char are put into a stream. cin >> extracts the letters but leaves the newline char. this newline char is then found by getline and therefore an empty string is read. a solution would be to add a cin.ignore() before getline.