views:

187

answers:

3

I have created a class that allows the user to input their mailing address, order date, type of cookie ordered and the quantity. There were other errors, but I stayed late and with the assistance of my prof, I have fixed them. Now all that is left is that I need to be able to change code to overload the I/O stream operators so that the objects may be used in standard input and output statements.

I'm not sure what all part of the code everyone will need to see, but I'm going to post the parts I believe are needed for what I'm trying to do.

I need to have it where in the output(), I have cout << order << endl; I will look over the net and will hopefully have it ready by tonight. Thanks for everyone's input.

Was instructed to take down my code due to other students from class copying my code pieces to do their work (knew it was possible but didn't think about it)

However, my code is complete.

I greatly appreciate all input. Thanks a lot and Happy Holidays!

+1  A: 

As far as comparison is concerned, you'd be better off comparing all upper or all lower (rather than each word's first letter upper), it's simpler to set things that way.

Moreover you should get into the habit of putting braces around code

Why do you have a magic number of 6 in your loop? Especially when you only have five (5) elements.

Perhaps the loop should be

...

int loop_size = sizeof(flavors)/sizeof(flavors[0]);

for (int i = 0; i < loop_size; ++i)

{

   if (flavors[i] == cookieOrdered)
   {
       valid_option = true;
       break;
  }

}
Liz Albin
A: 

Hint: lookup Case insensitive string comparison in C++

TimW
+1  A: 

Implement two functions: basic_ostream & operator<< (basic_ostream& ostr, const CookieOrder& co) basic_istream & operator>> (basic_istream& istr, CookieOrder& co)

the operator<<= function will be called when you use cout << order << endl; and the operator>> function will be called when you use the >> (stream extraction) operator. Be very careful how you implement the stream extraction operator.

You may want to declare either of these as friend to the CookieOrder, as that will allow the function to access the private parts of the class as if the function is a member of the class.


edit to respond to changes in the question

delcare your class as before:

class CookieOrder {
public:
// public methods as before
private:
// private parts as before
};
basic_ostream & operator<< (basic_ostream& ostr, const CookieOrder& co);
basic_istream & operator>> (basic_istream& istr, CookieOrder& co);

Implement the two functions using only the public interface of the CookieOrder class.

For example:

basic_ostream & operator<< (basic_ostream& ostr, const CookieOrder& co)
{
ostr << co.get_customerName() << endl;
/* the rest of the output */
}

These functions are not members of the CookieOrder class, they are normal functions with no special access to the CookieOrder class or instanaces of the class.

Andrew
How would I do it for the input part. b/c it I dont want to set the cookie type or quantity until the program runs the methods to check for valid input from the user.
Ryujin89
Thank you very very much. You greatly helped me in solving the issue. I was able to figure it out and now have everything working. THANKS! Happy Holidays!
Ryujin89
`basic_[io]stream` classes are templates, you either need to specify template parameters for them, make the operator overloads into function templates (and specify template parameters for `basic_[io]stream`), or use `[io]stream` (take off the `basic_`) which are the common `<char>` instantiations.
Roger Pate