Help with ofstream
Dec 5, 2012 at 5:10pm UTC
So, I'm trying to write a program that will parse data from a few text files, append them, send the new string to a class, and then spit the worked text back out into another file. Here's the pertinent part of the main()
2 3 4 5 6 7 8
std::cout << std::endl;
MyOutFile << myProduct.GetAllTheThings() <<
Now, using the myProduct.Display(), I can see that all of the Setters are writing the strings to the appropriate variables, but the GetAllTheThings() method isn't outputting them to the output file.
The GetAllTheThings() method just looks like:
2 3 4 5 6
and those individual methods are just returning the variables.
So for the question, can you use a getter with ofstream? Or should I re-work this to simply create a string and output that with ofstream?
Dec 5, 2012 at 6:30pm UTC
GetAllTheThings immediately returns this->GetProductNum(), so it won't do anything more after that. Why do you have multiple return statements in succession like this?
Dec 5, 2012 at 6:42pm UTC
Well the GetProductNum() gets the ProductNum_ variable like this:
2 3 4
and there's about 20 variables that need to be returned, so I nest them inside GetAllTheThings so I don't have to write
MyOutFile << myProduct.GetProductNum() << myProduct.GetProductName() << myProduct.etc() << myProduct.GetImageURL() <<
Dec 5, 2012 at 8:29pm UTC
Well, the thing is that the only thing that function will ever do is
. None of the other return-statements will be executed.
Dec 5, 2012 at 8:43pm UTC
That's what I figured, I'm writing it now to overload the << operator to do force it to do what I want. So I'm putting this in my class.cpp (and the prototype in class.h)
2 3 4 5 6
operator<<(std::ostream & os, const Product & myProduct)
os << myProduct.ProductNum_ << '\t' << myProduct.ProductNameNew_ << '\t';
and then this in my main works:
Topic archived. No new replies allowed.