Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This problem deals with C++. How do you check/read from cerr. Let\'s say I have

ID: 3748126 • Letter: T

Question

This problem deals with C++.

How do you check/read from cerr. Let's say I have multiple file arguments, and one of the files encountered an error. I am able to report that error to standard error (i.e., cerr << "uneven dimensions" << ' '; ); break. This error is fine; however, I want the compiler to keep going to the next file argument, which it does. But, it continues with that file argument onto the next function and the next until its completely done with that filename. So, my thinking is I need a condition to check to see if cerr has reported any error so I can stop the other functions from using that filename.

So, my question is, is there a way I can read from cerr in my main() function, so if I encounter an error within any of the functions, future functions will no longer execute for that filename. Essentially, I want the program to halt just for that file, and continue on with the other files.  

Lets say file1 fails inside read function. Well, func1 and func2 are no good to me now since they rely on the results of the read function. So, I want it to stop using that index and proceed to the other index (file2 and file3 - both of which encounter no errors).

Is there a way I can check cerr to see if there is any errors there? Or, some other method I can use to stop executing functions for that index?

read // pseudo-code if(condition fails) {

Explanation / Answer

#include <iostream>

#include <sstream>

class cerr_redirector

{

public:

cerr_redirector(std::ostream& os)

:backup_(std::cerr.rdbuf())

,sbuf_(os.rdbuf())

{

std::cerr.rdbuf(sbuf_);

}

~cerr_redirector()

{

std::cerr.rdbuf(backup_);

}

private:

cerr_redirector();

cerr_redirector(const cerr_redirector& copy);

cerr_redirector& operator =(const cerr_redirector& assign);

std::streambuf* backup_;

std::streambuf* sbuf_;

};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote