Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have this code and in the main fu 1

Status
Not open for further replies.

bnd98

Technical User
Apr 1, 2003
17
US
I have this code and in the main function I want to display "Daily Total: " however it is displaying "Pizza" instead due to the constructor? Any suggestions on how to fix this problem?

#include<iostream.h>
#include<conio.h>
#include<string.h>

class Meal
{
friend ostream& operator<<(ostream &out, const Meal &aMeal);
friend istream& operator>>(istream &in, Meal &aMeal);
private:
char entree[20];
int calorie;
public:
Meal(char ent[] = &quot;Pizza &quot;, int cal = 25);
Meal operator+(Meal &aMeal);
void displayMeal();
};

Meal::Meal(char ent[], int cal )
{
strcpy(entree, ent);
calorie = cal;
};


Meal Meal::eek:perator+(Meal &aMeal)
{
Meal exam;
exam.calorie = calorie + aMeal.calorie;
return(exam);
};

void Meal::displayMeal()
{
cout<<&quot;The entree: &quot;<<entree<<&quot;has&quot;<<calorie<< &quot; calories.&quot;<<endl;
};

ostream& operator<<(ostream &out, const Meal &aMeal)
{
out<<aMeal.entree<<aMeal.calorie<<&quot; calories &quot;<<endl;
return(out);
};

istream& operator>>(istream &in, Meal &aMeal)
{
cout<<endl; //clears
cout<<&quot;Enter the entree name: &quot;;
in>>aMeal.entree;
cout<<&quot;Enter the amount of calories: &quot;;
in>>aMeal.calorie;
return(in);
};

void main()
{
Meal breakfast(&quot;Bagel &quot;, 100);
Meal lunch(&quot;Hamburger &quot;, 325);
Meal dinner(&quot;Steak &quot;, 350);
Meal total(&quot;Daily Total: &quot;, 0);
total = breakfast + lunch + dinner;
cout<<&quot;Breakfast: &quot;<<breakfast<<endl;
cout<<&quot;Lunch: &quot;<<lunch<<endl;
cout<<&quot;Dinner: &quot;<<dinner<<endl;
cout<<total<<endl;
getch();
};

 
All I had to do was take temp.displayMeal();
out of the loop.

Everything works fine. Thank you for your help...didn't realize it was such a simple error.
 
No problem. Just trying to get you to understand what was going on instead of just writing the code for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top