cin.getline not prompting for input after hitting Enter

Hello. I'm having trouble with a project I'm working on. The idea is to create a Mad Lib type prompt, where it asks for the user's input based on the tags within the file created. The problem I have is that the cin.getline() isn't working the way I want it to, and I'm clueless as to how to do it.

I want cin.getline to ALWAYS prompt for input until the user finally does put something in. Instead, it will just skip and go to the next iteration of the loop, and display the next prompt.

However, after the first time this happens, if the user keeps hitting enter, they press it twice, and it skips to the next iteration. This can be repeated until the end, but I want cin.getline() to only move on when there has been input.

Here is the text it analyzes:

<web_site_name> <verb> <plural_noun> <plural_noun> <proper_noun> <adjective> <noun> <noun> <boolean_operator> <noun> <favorite_website> <another_website> <adjective>

And here's my code for analyzing the text within the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**********************************************************************
* This function processes the words in the file to find which ones are
* the tokens.
***********************************************************************/
void readWords(char words[][32], char answers[])
{
   for (int i = 0; words[i]; i++) //begin receiving input from the file
   {
      fin >> words[i];
      if (words[i][0] == '<') //how to recognize if it's a tag or not
      {
         words[i][0] = '\t';
         for (int j = 0; words[i][j]; j++)//What to do at the end of a tag
         {
            if (words[i][j] == '>')
              words[i][j] = ':';
            if (words[i][j] == '_')//when there are spaces in words of a tag
               words[i][j] = ' ';
         }
         switch (words[i][1])//switch statement for special characters
         {
            case '{':
               cout << "";
               //words[i][1] = ' "';
               break;
            case '}':
               cout << "";
               //words[i][1] = '" ';
               break;
            case '[':
               cout << " ";
               //words[i][1] = ' \'';
               break;
            case ']':
               cout << "";
               //words[i][1] = '\' ';
               break;
            case '#':
               cout << "";
               //words[i][1] = '\n';
               break;
            default:
              words[i][1] = toupper(words[i][1]);//capitalize tag's first letter
         }
         if (words[i][1] == '{' || words[i][1] == '}' ||
            words[i][1] == '[' || words[i][1] == ']' ||
            words[i][1] == '#')//if statement to prevent printing these symbols
         {
            continue;
         }
            cout << words[i] << " ";//display the tags as prompts
            cin.ignore();
            cin.getline(answers, 256);//prompt the user for input to an array
      }
      if (fin.eof())
      {
         return;
      }
   }
   fin.close();
}


The code runs just fine otherwise. The cin.getline I'm talking about is on lines 45-47. I'm so confused. Can someone help me? Thank you so much.
Last edited on
1
2
3
do{
   cin.getline(answers, 256);
}while( strlen(answers) == 0 );
Topic archived. No new replies allowed.