So, I've been going at this all morning (just to let the people here know that I'm not one to just look for the answers an not do assignments myself).
I'm basically trying to make a simple program that reads integers out of a text file and only adds the positive ones and not the negatives.
All is well except it won't take the last integer (the last line, I presume.)
I took the negative out, nothing to do with that. I put more numbers in and I made the txt file less, no answer. No matter what the last number is, the program won't read it. I've been researching online and I've been seeing that it might be an issue with "while "!inFile.eof())".
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream inFile;
ofstream outFile;
int total = 0;
int inputVal,throwaway = 0;
inFile.open("data.txt");
outFile.open("out.txt");
inFile >> inputVal;
while (!inFile.eof())
{
if (inputVal <= 0)
{
inputVal += throwaway;
}
else
total += inputVal;
inFile >> inputVal;
}
cout << "The total is: "<< total << endl;
outFile << "The total is: "<< total << endl;
inFile.close();
outFile.close();
return 0;
}
One must remember that one can reach the end of file on an input extraction that went perfectly well. Don't test against eof unless that is actually what you're wanting to test against.