I skimmed the thread. I have some stuff to show off and some advice to give, but at this point I need to write some code to explain myself. We'll see if it helps any.
Originally Posted by
Ashl7
can't figure it out for the life of me.
Sounds like someone needs to re-evaluate their choices and/or priorities.
This was the offending line:
Code:
ins>>"Enter the amount of soda in the can again: ";
std::cin, for example, is not something that could insert new data into a string literal. Since literals are a kind of constant, I think Soma is expecting that you figured this one out. I think that you were just guilty of typing on the keyboard a little too quickly.
To be honest with you, I don't like that you want to display messages inside of the function that takes input for SodaCans. You could also set the stream state to bad so that you can handle bad input outside of the extraction operator, too.
Consider this:
Code:
std::cout << "Enter the brand of soda, the size, and then the amount of soda in the container, respectively: ";
while ( ! (std::cin >> can)) {
std::cout << "The amount in the container should not be more than the container size." << std::endl;
std::cin.clear();
std::cin.ignore(100000, '\n');
}
That is a lot cleaner is it not? Well the operator that works this way would look a lot like:
Code:
std::istream& operator >>( std::istream& ins, SodaCan *&can )
{
SodaCan * temp = new SodaCan;
ins >> temp->my_Brand >> temp->my_Size >> temp->my_Contents;
if (ins.eof()) {
ins.clear(std::ios::eofbit);
delete temp;
}
else if (ins.fail() || temp->my_Size > temp->my_Contents) {
ins.clear(std::ios::failbit);
delete temp;
}
else {
delete can;
can = temp;
}
return ins;
}
That's as simple as I can make it, at least. I can't be bothered to test it, as bad as that is. But you can assume that the code which you did not write, like ins.clear() will work. The error message is also kinda dumb if you actually sent EOF. I included it in the code because I thought it would bad to write an extraction operator and not deal with that situation. It would be brittle.
If you want to handle EOF silently, a simple condition like while( ! (std::cin >> can)) is very inclusive. You will have to write something like if(cin.fail()) to handle bad input inside of the loop.
Don't ignore the other guys advice just because I typed so much.
Finally, a tip on reading long dumps/big error messages. 99% of the time, the problem you need to deal with is explained in the first few words. Concentrate on that and comprehend those first few words the best you can; look at the related lines; see if it points out any obvious errors.