HaiZuka's blog

By HaiZuka, history, 3 years ago, In English

Hello everyone.

I recently entered the contest Codeforces Round 699 (Div. 2), specifically I did the exercise 1481A - Space Navigation , which asked me to read two integers qx, qy and finally a string character S. I use C ++ programming language to do this. I used the fflush(stdin) function to clear the count memory while doing this, specifically my main code at the time was:

vector <string> ans;

int main() {
    int t, a, b;
    string s;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> a >> b;
        fflush(stdin);
        getline(cin, s);
        ans.push_back(solve(a, b, s));
    }
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
}

The problem I have is that the codeforces system does not seem to be executing my fflush () function, while I am still able to run the correct program using my computer.

I have to fix my program to:

vector <string> ans;

int main() {
    int t, a, b;
    string s;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> a >> b;
        cin >> s;
        ans.push_back(solve(a, b, s));
    }
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
}

With this program, the system accepted my program.

However, entering a string using the function std :: cin is not a good choice, if there is ' '(whitespace, # 32 ASCII) in the string then I don't really know how to read the data. :(

I write this article to ask you to share your experiences about the problem that I am facing to help my upcoming competitions go smoother.

Thank you for reading my article, good day.

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

| Write comment?
»
3 years ago, # |
  Vote: I like it +7 Vote: I do not like it

I'm not sure what fflush is supposed to do here, but you can use cin.ignore to ignore the stream until a '\n' is encountered.

See this article for a detailed answer.