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();
};

 
Use char* instead of char ent[]

Code:
    Meal(char* ent = &quot;Pizza &quot;, int cal = 25);
    Meal operator+(Meal &aMeal);
    void displayMeal();
};

Meal::Meal(char* ent, int cal )

-pete
 
Thanks! However it is still displaying &quot;Pizza&quot;.
 
LOL Good one! You fooled me!

Code:
    Meal total(&quot;Daily Total: &quot;, 0);
// now your assigning a completely new value to total! So 
// whatever happened in the constructor is irrelevant
    total = breakfast + lunch + dinner;

-pete
 
So it should display &quot;Daily Total: &quot; right? And then after that I want it to take the three meals, add the calories and display that. However even with the

Meal total(&quot;Daily Total: &quot;, 0);
total = breakfast + lunch + dinner;

it still displays &quot;Pizza&quot;. Is there something wrong with the total line? I don't understand why it is not displaying &quot;Daily Total: &quot; and still &quot;Pizza&quot;?
 
Take a look at the second line:

[tt]total = breakfast + lunch + dinner;[/tt]

The key here is in your usage of operator+

The operator+ first creates a Meal with the default constructor. The default constructor makes [tt]exam.entree[/tt] equal &quot;Pizza &quot; and [tt]exam.calorie[/tt] equal 25.
Then, [tt]exam.calorie[/tt] is set to the sum of calories. However, [tt]exam.entree[/tt] has not been modified since the constructor -- and thus still equals &quot;Pizza &quot;!!!

Then, after breakfast+lunch+dinner have been added up, you assign this value to [tt]total[/tt]. You didn't define an [tt]operator=[/tt], so the computer will simply copy all members from the source object (i.e. [tt]breakfast+lunch+dinner[/tt]) to the destinaton object (i.e. [tt]total[/tt]).

The remedy here is to create an assignement operator for Meal which only copies the [tt]calorie[/tt] member and not the [tt]entree[/tt] member.
 
How do I go about doing this? I have tried everything that I can think of and nothing. In public can I use:

void operator=(Meal &otherMeal);

And if so then what do I need to copy only the calorie and not the entree?

 
Yes you could, that somewhat violates the concept of &quot;assignment&quot; but it will get you there, so something like this:

Code:
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);
	 Meal( const Meal& toCopy){
		 copy(toCopy);
	 }
	 const Meal& operator=(const Meal& rhs){
		 copy(rhs);
		 return *this;
	 }
    void displayMeal();

protected:
	void copy( const Meal& rhs){
		// do not copy entree
		calorie = rhs.calorie;
	}
};
-pete
 
Thank you so much, I was pulling my hair out for that one! Okay one more question if you don't mind. I want to write another main for the same program except for this time I want it to declare an array of 7 Meal objects. I let the user enter the values for the week. Then I want to total these meals and display calorie total for the end of the week.

My main looks like this:

{
Meal aMeal[7];
aMeal[0];
aMeal[1];
aMeal[2];
aMeal[3];
aMeal[4];
aMeal[5];
aMeal[6];

Meal temp;

for (int i = 0; i < 7; i++)
cin>>aMeal;

for (i = 0; i < 7; i++)
{
temp = temp + aMeal;
aMeal.displayMeal();
}

However there is something really wrong here. The displayMeal shows all 7 times when I only want it to display once. Also, it does not add the calories for the 7. What is wrong here? Can I have it display the total calories only once?

Debbie
 
Well, take a serious look at your code:

Code:
for ( i = 0; i < 7; i++ )
{
    ...
    aMeal.displayMeal();
}

What would you expect it to do besides call displayMeal 7 times?

If you want the function to be called only once, then call the function only once.
 
Your code:
[tt]
1: for (int i = 0; i < 7; i++)
2: cin>>aMeal;
3:
4: for (i = 0; i < 7; i++)
5: {
6: temp = temp + aMeal;
7: aMeal.displayMeal();
8: }
[/tt]

In line 2 you have cin>>aMeal. I would think this would cause a compile error, but I guess not. aMeal is an array, so thus you will need to access elements of the array by subscript.
Code:
cin>>aMeal[i];

In line 6 you have the same problem. Access individual elements of aMeal by subscript.

Code:
for (i = 0; i < 7; i++)
    {
       temp = temp + aMeal[i];
    }

Then in line 7 you have a certain meal being displayed, and it will be displayed 7 times, beacause it is in the for loop. As I understand, you want to only display the total, which will be in the temp variable. For that reason you need to call temp.displayMeal() instead of aMeal.displayMeal()!
 
Also,
[tt]aMeal[0];
aMeal[1];
aMeal[2];
[/tt]etc. are unnecessary.
 
He/She does have cin>>aMeal-with-square-brackets-i but forgot to turn off the process TGML, which is why the rest of the posting is italics. i keep doing that too.
Oops!
 
Oops, I never thought of that... I almost did it myself in the last post... LOL
 
Okay here the code is again. I think I have made all of the necessary changes that were suggested to me. It is adding the calories but it is still displaying &quot;Total weekly calories:&quot; 7 times. I have tried to change everything that I can possibly think of and nothing.

Debbie


#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 = 0);
Meal operator+(Meal &aMeal);
Meal( const Meal& toCopy)
{
copy(toCopy);
}
const Meal& operator=(const Meal& rhs){
copy(rhs);
return *this;
}
void displayMeal();

protected:
void copy( const Meal& rhs){
calorie = rhs.calorie;
}
};


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;Total weekly calories: &quot;<<calorie<<endl;
};

ostream& operator<<(ostream &out, const Meal &aMeal)
{
out<<&quot;Total weekly calories: &quot;<<aMeal.calorie<<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 meals[7];


Meal temp;

for (int i = 0; i < 7; i++)
cin>>meals;

for (i = 0; i < 7; i++)
{
temp = temp + meals;
temp.displayMeal();
}

getch();
};
 
Meal has a member function called displayMeal. When called, it prints the words &quot;Total weekly calories: &quot; and an integer member of the Meal object upon which it is called.

Code:
void Meal::displayMeal()
{
    cout<<&quot;Total weekly calories: &quot;<<calorie<<endl;
}

In main, you create a Meal called temp.

Code:
int main()
{
    Meal temp;

Later in main, you run through a for loop 7 times.

Code:
    for (i = 0; i < 7; i++)
    {

Inside the for loop, you call that function.

Code:
        temp.displayMeal();
    }
}

So what is it you don't understand? You call the function 7 times. It runs 7 times. Each time causes it to print output to the screen exactly as you've specified. I can't see where you're lost.

If you only want to run the function once, then just call it once.

If the function isn't doing what you want it to, then change the function do do what you want it to do.

What is it you expect the output to look like? What's wrong with what you're getting right now? What do you want it to do differently?
 
It is displaying this. I input 1 calorie.
Total Weekly Calories 1
Total Weekly Calories 2
Total Weekly Calories 3
Total Weekly Calories 4
Total Weekly Calories 5
Total Weekly Calories 6
Total Weekly Calories 7

I want it to only show the last line
Total Weekly Calories 7

What do you mean call it once? I have thought of everything that I can to get it to display only once and when I change something then sometimes it wont add correct.

Debbie
 
Here's a for loop.

Code:
for ( int i = 0; i < 7; i++ )
{
    func();
}

This for loop says:

1) Initialize i to 0.
2) If i < 7, continue to step 3. Otherwise, stop.
3) Do whatever is in the braces, i.e. call func.
4) Do i++ and go back to step 2.

In short, it says, &quot;Call func 7 times.&quot; That is, a less elegant way of writing the exact same thing would be:

Code:
func();
func();
func();
func();
func();
func();
func();

In other words, by placing it in the for loop that executes its contents 7 times, func gets called 7 times.

In more other words, you call func seven (7) times.


Applying the same concept to your code, which also contains a for loop that executes its contents 7 times, you should be able to see that the following lines of code

Code:
temp = temp + meals;
temp.displayMeal();

get called 7 times each.

Note the second of those two lines of code: &quot;temp.displayMeal();&quot; That line has the effect of calling the displayMeal member function of the Meal object temp.

The displayMeal function does output to the screen. Specifically, it outputs the words &quot;Total Weekly Calories: &quot; followed by the value of the calorie member of temp.

Since you call this function 7 times, and since each time it gets called, it outputs &quot;Total weekly calories: &quot; and a number to the screen, the words &quot;Total weekly calories: &quot; and a number are output on the screen seven times.

So if you don't want it to be called seven times, just call it once.

By that, I simply mean that you should refrain from calling it seven times.
 
I can get the &quot;Total weekly calories:&quot; to display only once but then the amount is incorrect. What now?

 
What amount does it show and what amount do you want? Did it show the correct amount before?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top