1

There is a some part of code in which class is defined as Meal. The class,Meal, has a object of named total and I want to show and take input for total's attributes. I went many solutions but didn't get any proper solution. Please help me in solving this problem. I would be grateful.

void operator <<(ostream &out, Meal &ob)
    {
        out<<"Entree: "<<ob.entree<<endl;
        out<<"Calorie: "<<ob.countCalorie<<endl;
    }
    void operator >>(istream &input, Meal &ob)
    {
        cout<<"Enter entree: ";
        input>>ob.entree;
        cout<<"Enter calorie: ";
        input>>ob.countCalorie;
    }


int main()
{
Meal *week[21], total;
string entree;
int cal;
for(int i = 0; i < 21; i++)
{
    cout<<"Enter "<<i+1<<" entree: ";
    cin>>entree;
    cout<<"Enter "<<i+1<<" calorie: ";
    cin>>cal;

    week[i] = new Meal(entree,cal);
}
for(int i = 0; i < 21 ; i++)
{
    total = total + week[i];
}
cout<<total;

cin>>total;

}

  • What is it intended to do that does not work right? A concrete example would help – Michael Veksler Mar 20 '19 at 12:27
  • I think you have to send a minimal and complete example of your probleme. – Gojita Mar 20 '19 at 12:28
  • In any case, your code has many problems including non-idiomatic operator<< (and >>) overloads, memory leaks, multiple uses of 21 rather than defining and using a constant, not checking input success/errors, doing output inside input overload – Michael Veksler Mar 20 '19 at 12:33
  • 1
    I flagged as dupe, just let me know in case you dont agree. And note that even if you fix your code, it is a rather atypical use of the in/out operator overloads, eg I would never expect a `operator>>` to print some output. Consider to use named methods instead – 463035818_is_not_an_ai Mar 20 '19 at 12:40
  • PS: for your question please read about [mcve] and you need to explain what is wrong about the code. What is it supposed to do, what does it do, how do the two differ? Any compiler errors? runtime errors? We cannot know what you consider as "proper solution" unless you tell us – 463035818_is_not_an_ai Mar 20 '19 at 12:42

0 Answers0