Блог пользователя Qualified

Автор Qualified, история, 5 лет назад, По-английски

Hi, I am trying to do 723B - Text Document Analysis and here is my code.

Code

When I input 37, which is the example input, the program just returns maxx and cnt. I don't understand this. Am I doing something wrong. BTW I am using GVim.

  • Проголосовать: нравится
  • -20
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +14 Проголосовать: не нравится

After you do cin >> n, the pointer for your input is still on the same line as n. So then getline returns the remainder of that line, which is just a newline character. Changing getline to cin >> s should fix the error, since s is guaranteed to not have spaces.

Also, your code doesn't separate words if the separator is ( or ). Printing s1 inside the while loop will demonstrate this.

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    What if the problem had spaces, how would I input that? But thanks for the reply!

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      If the problem has spaces, your getline should work as long as you add cin >> ws after cin >> n. This will take all the whitespace and discard it, which should put you at the start of the next line.

      But I think (or at least I hope) that the writers will make efforts to ensure you never need to do this. So far I haven't seen a CF problem requiring cin >> ws for input.

»
5 лет назад, # |
Rev. 3   Проголосовать: нравится -14 Проголосовать: не нравится