I have been back and forth with my professor regarding why a prime read is or is not needed in c++.
#include <iostream>
#include <fstream>
int addFunct(int& total, int& rNum);
int subFunct(int& total, int& rNum);
int main() {
char op1;
int rNum = 0;
int total = 0;
std::ifstream inFile;
inFile.open("C:\\Users\\Administrator\\Desktop\\c++ files\\input.txt");
if (inFile.fail()) {
std::cout << "no read";
}
do {
inFile >> op1 >> rNum;
if (op1 == '+') {
addFunct(total, rNum);
}
else if (op1 == '-') {
subFunct(total, rNum);
}
}
while (!inFile.eof()); //super while loop
inFile.close();
std::cout << "El total es: " << total;
return 0;
}
int addFunct(int& total, int& rNum) {
total = total + rNum;
return total;
}
int subFunct(int& total, int& rNum) {
total = total - rNum;
return total;
}
If I place a prime read above my DO statement (e.g. inFile >> op1 >> rNum;) then the last value in the file will not be read. However, my professor argues that priming a read is important and necessary. I am wondering why this is and how I can resolve the issue of not performing operations on the last read in number.