Question

I've been workingo n this for a long time so I figured it's time to ask a question as I have no idea what's going on. For whatever reason in this loop, it only works proper through the first time, then it goes back to the beginning of the file and starts to read from the beginning again. Thanks.

void readGasFile(int numTherms, int arrSize, string fileName, Customer cArray[])
 {
    cout << "reading gas file";

    int i;
    string temp;
    int temp2;

    ifstream inputFile;
    inputFile.open(fileName.c_str());

    if (inputFile.is_open())
    {
        cout << "reading input file";

        string dummyLine;
        getline(inputFile, dummyLine);

        //ignore first line

        /* *****************************************
        format:
        1212 <--- account number
        Lance, Ahmed
        1200  1212  <----- previous, current therms meter readings
        2323        <----- gas therms used
        ******************************************/

        for(i=0; i<arrSize; ++i)
        {
            cout << "start loop" << i << endl;

            inputFile >> temp2;
            cArray[i].setNumber(temp2);

            cout << temp2 << endl;;

            inputFile >> temp;
            cArray[i].setLastName(temp);

            inputFile >> temp;
            cArray[i].setFirstName(temp);

            inputFile >> temp2;
            cArray[i].setPrevious(temp2);

            inputFile >> temp2;
            cArray[i].setCurrent(temp2);

            inputFile >> temp2;
            cArray[i].setTherms(temp2);
        }
    }
    return;
}
Was it helpful?

Solution

Based on the code and without seeing the exact input it is impossible to tell but I'd bet that reading actually fails as some point. Since you don't check your inputs as you always should, your program doesn't notice it. As a side node, you read the name including the comma which is probably also not desirable. Here is how I would write the loop:

for(i = 0; i != arrSize
           && inFile >> number
           && std::getline(inFile, lastName, ',')
           && std::getline(inFile, firstName)
           && inFile >> previous
           && inFile >> current
           && inFile >> therms; ++i) {
    // set the various values
}

Obviously, the various variable would need to be declared appropriately. Realistically, I would probably consider writing an input operator. If you find that you can't read anything you'll need to find out where the input goes wrong, e.g., by outputting that you successfully read the value after each subexpression, e.g.:

for(i = 0; i != arrSize
           && inFile >> number && std::cout << "number=" << number << '\n'
           && std::getline(inFile, lastName, ',') && std::cout << "name='" << lastName << "'\n"
           && std::getline(inFile, firstName) && std::cout "first-name='" << firstName << '\n'
           && inFile >> previous && std::cout << "previous=" << previous << '\n'
           && inFile >> current && std::cout << "current=" << current << '\n'
           && inFile >> therms && std::cout << "therms=" << therms << '\n'; ++i) {
    // set the various values
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top