Search code examples
c++fileloopsfstreamundeclared-identifier

Counting number of integers in text file (undeclared identifier using fin>>x)


I have a text file with an unknown number of integers. I created a dynamic array without trouble, but the numbers have varied spacing and digits. I would normally go through each position to count the number of spaces used, subtracting space from it, but that won't work in this case.

I thought to use something like this: (which similar questions would suggest)

    do
    {

        if (!file.is_open())
        {
            file.open(donation);
        }

        fin >> temp;
        charCount++;
    } 
    while (file.peek() != EOF);

    cout << charCount;

Now, I have fstream included, and I am using namespace std, and I have no problem opening and reading the text file, but visual studio 2015 tells me:

Error C2065 'fin': undeclared identifier

I don't understand what I am doing that is triggering that type of response from the IDE.


Solution

  • file is the name of your ifstream, right? If so, line

    fin >> temp;
    

    should be

    file >> temp;