Qualified's blog

By Qualified, history, 4 years ago, In English

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.

  • Vote: I like it
  • -20
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +14 Vote: I do not like it

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.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.

»
4 years ago, # |
Rev. 3   Vote: I like it -14 Vote: I do not like it