I can't catch the end of the file.



  • The challenge is to check the balance of the numbers in the file. Reading the file in a sympathetic manner, but the program is cycling (no end of the file). What am I doing wrong?

    #include <fstream>
    #include <iostream>
    using namespace std;
    

    int main()
    {
    int flag=0, ctr=0;
    char с;
    setlocale(LC_ALL, "rus");
    ifstream fin("text.txt");
    fin >> с;
    while (с!=EOF)
    {
    if (с == '{')
    ctr += 1;
    if (с == '}')
    ctr -= 1;
    if (ctr < 0)
    flag = 1;
    fin >> с;
    }
    fin.close();
    if ((ctr == 0) && (flag == 0))
    cout << "OK" << endl;
    else
    cout << "not OK" << endl;
    return 0;
    }



  • Replace.

    while (с!=EOF)
    

    while (fin)
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2