-1

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.

wiregh0st
  • 57
  • 10
  • 4
    And by *prime read* you mean... any reading operation executed as the first one in regard to a certain stream? Anyway, the safest and most reliable way of doing that is `while (inFile >> op1 >> rNum) { ... }`. Both you and your professor should read [why `f.eof()` is almost always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong). – Fureeish Jul 17 '19 at 13:35
  • 2
    Besides, "*However, my professor argues that priming a read is important and necessary.*" - have you considered *asking them **why*** he claims that? – Fureeish Jul 17 '19 at 13:37
  • Yes and yes. Commented under your answer. – wiregh0st Jul 17 '19 at 14:02

2 Answers2

3

Why do I need a prime read in c++?

You don't.

However, my professor argues that priming a read is important and necessary.

Did you ask them for an explanation? Or they just stated that and refused to explain?


To elaborate - both your and your professor's approaches are wrong. Both because they use std::fstream::eof as the loop condition, which is almost always wrong.

What you should do is move the reading to the loop condition, like so:

while (inFile >> op1 >> rNum) {
    if (op1 == '+') {
        addFunct(total, rNum);
    }
    else if (op1 == '-') {
        subFunct(total, rNum);
    }
}

This way you will not miss any data from the file. You will also correctly not enter the loop as soon as the reading fails, either due to end-of-file or inability to parse file content to char followed by an int.

This works because if a >> read fails, it sets certain failbits of the associated stream. Then, the >> operator returns the stream itself (in this case inFile) which is implicitly convertible to bool. It evaluates to true if there are no failbits set and to false otherwise.

AVH
  • 11,349
  • 4
  • 34
  • 43
Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • 1
    Professor could not offer an explanation which puzzles me. I pressed for an answer and even received points off for not using a prime read, however, even after much research, I never found an answer to satisfy needing a prime read. Thank you for your explanation (especially the in-depth part) and for linking the other stackoverflow question. Your expertise is much appreciated! – wiregh0st Jul 17 '19 at 14:01
0

Priming read just loads the while loop for the first time it tests the condition. I think the professor thought it was important because it is a standard way to setup while loops for structured programing and Flow charting.

Rick
  • 1