capturing user input into an array

I'm creating a madlib game. The code I've included below, extracts words from a string, modifies them, and displays the modification to the user. The user see's something like this:
Plural noun: and the user writes their answer
I capture the answer with cin.getline(answers[numAnswer]) (See actual code for issue.)

Then the next item is formatted and displayed.
Adjective: The user gives their answer. I capture the answer with cin.getline, etc.

Everything is working well except for one thing. The first modified text doesn't wait for the user's input. The modified text displays properly, but it then goes right to the next modified text. The user is unable to have input. However, the answer array recognizes that array[0] is empty. It doesn't take the second answer and put it in the answer[0] spot. I can't figure out why the code immediately skips the first chance to do cin.getline().

Below is the code for this one function. I don't know if you need to have the input text file, etc. to see what's going on. I don't think so. I think the problem lies right in the lines for cin.getline().

Thanks in advance!

int askQuestions(char text[][256], char answers[][256], int num)
{
// counter number for answers array
int numAnswer = 0;
char userInput[256];

// loop through text array
for (int i = 0; i < num; i++)
{
// loop through any string that begins with '<'
if (text[i][0] == '<' && isalpha(text[i][1]))
{
//handle first alpha char
text[i][1] = toupper(text[i][1]);
cout << "\t" << text[i][1];

// handle the rest of the chars before closing >
int j = 2;
while (text[i][j] != '>')
{
if (text[i][j] == '_')
text[i][j] = ' ';

cout << text[i][j];
j++;
}

cout << ": ";

cin.getline(answers[numAnswer], 256);

numAnswer++;
}

}

return numAnswer;
}


Sounds like you need to use cin.ignore(); after your cin statement that is giving you problems.
That definitely allowed me to get the first prompt to display without the second one immediately showing up. However, it throws my array off. It doesn't recognize the first answer. The second prompt is put into array[0]. I'll take a longer look at this, but if you have any ideas, I'd be very appreciative. Not knowing how cin.ignore works, I may be trying to fix the wrong issue. Thank you!!

ps--the array isn't recording the first letter in each user input so instead of boys I get oys and girls I get irls. I think that might have something to do with the cin.ignore also. Maybe not. Any help is very appreciated.
Actually I don't think you should use cin.ignore() in this function.

The getline function will read characters from the input buffer until if finds a newline character '\n'. If the buffer is empty (that is, the user has not yet typed anything) then the function should behave properly. On the other hand, if some other part of the program has previously requested user input, then there could be one or more characters remaining in the buffer, and particularly there would be a newline already there. Thus the need for cin.ignore() arises in some other part of the program, and that is where it should be used.

Whenever there is something like this:
1
2
    int num;
    cin >> num;
there will be some trailing character(s) remaining in the buffer (the user types 123 and presses 'enter', the cin >> num reads the '123' but leaves the newline in the input buffer.

You can add something like this:
 
cin(1000, '\n'); 
just after that user input, to empty the input buffer. The number 1000 here is an upper limit, the function will ignore up to that many characters, or until it finds a newline.
http://www.cplusplus.com/reference/istream/istream/ignore/
Topic archived. No new replies allowed.