-1

so here's the code:

#include <iostream>
#include "Trie.h"
using namespace std;

void main()
{
    string input;
    Trie dictionary('\0');
    while (true)
    {
        getline(cin, input);
        if (input[0] != '.')
        {
            dictionary.analyzeText(input);
        }
    }
    cout << endl;
    while (input[0] != '.')
    {
        getline(cin, input);
        dictionary.analyzeTextP2(input);
    }
    system("pause>null");
}

It's not final version but when I enter an input such as:

The quick brown fox
totally jumper over
the lazy
terrier dog
.

The . in the last line should be the mark to finish getting an input from user, however getline doesn't get it at the fifth iteration, no idea why. I've made sure that textAnalyze() function doesn't interact with the fifth line.

So, how come the . gets swallowed or what's causing it? (and I know I need a break; in the first loop, it's okay)

bolov
  • 72,283
  • 15
  • 145
  • 224
AmitBL
  • 89
  • 4
  • 12
  • 1
    you need to debug your program. Inspect what `input` holds after each `getline` – bolov Jan 21 '18 at 20:49
  • @RSahu : AmitBL explicitly mentioned that in the question already. – Clifford Jan 21 '18 at 20:56
  • Is the `.` followed by newline? If it is not a line, then `getline()` will be waiting indefinitely, and you will never know the cause the loop also does not terminate - you cannot tell the difference between `getline()` "loosing" a character - which is unlikely and it simply not returning at all - much more likely . It would have been simpler too to have just added `else{ break ;}` that to explain its omission in the text - that's just a distraction. – Clifford Jan 21 '18 at 21:01
  • How is your program supposed to work? You have an endless loop at the beginning which consumes all the input. "I know I need a break", fine, so let's suppose you add the break, the point is still consumed in the first loop, what is the second loop supposed to read? – Bob__ Jan 21 '18 at 21:03

1 Answers1

0

and I know I need a break; in the first loop, it's okay.

That is the source of the problem.

Let's simplify the input to just two lines:

The quick brown fox
.

After the first call to getline, the value of input is "The quick brown fox".

The call dictionary.analyzeText(input); is executed.

After the second call to getline, the value of input is ".".

The call dictionary.analyzeText(input); is not executed.

In the next call to getline, getline returns with failbit set to true, the value of input remains unchanged.

In the next call to getline, getline returns with failbit set to true, the value of input remains unchanged.

and it goes on and on.

The value of input remains set to "." but the program never gets out of the while loop.

You need something like:

while ( getline(cin, input) && input[0] != '\0' )
{
   dictionary.analyzeText(input);
}

The second block of code for dealing with user input can simply be removed.

R Sahu
  • 204,454
  • 14
  • 159
  • 270