Thread: Display

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Question Display

    In the following code, at the end, I would like to have "Total Weekly Calories: " displayed along with the total amount.

    However I have "Total Weekly Calories:" display seven times and the correct total is in the seventh spot. How can I get "Total...to show only once and the correct amount to display? I have tried everything that I can think of and still can not come up with anything. I can get the "Total Weekly Calories" to display once but then the amount of calories is incorrect. Please see if you can see my errors?

    Code:
    #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[] = "Pizza", 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){
            // do not copy entree
            calorie = rhs.calorie;
        }
    };
    
    
    Meal::Meal(char ent[], int cal )
    {
    	strcpy(entree,ent);
    	calorie = cal;
    };
    
    
    Meal Meal::operator+(Meal &aMeal)
    {
    	Meal exam;
    	exam.calorie = calorie + aMeal.calorie;
    	return(exam);
    };
    
    void Meal::displayMeal()
    {
    	cout<<"Total weekly calories: "<<calorie<<endl;
    };
    
    ostream& operator<<(ostream &out, const Meal &aMeal)
    {
    	out<<"Total weekly calories: "<<aMeal.calorie<<endl;
    	return(out);
    };
    
    istream& operator>>(istream &in, Meal &aMeal)
    {
    	cout<<endl; //clears
    	cout<<"Enter the entree name: ";
    	in>>aMeal.entree;
    	cout<<"Enter the amount of calories: ";
    	in>>aMeal.calorie;	
    	return(in);
    };
    
    void main()
    {
    	Meal meals[7];
    
    
    	Meal temp;
    
    	for (int i = 0; i < 7; i++)
    		cin>>meals[i];
    		
    	for (i = 0; i < 7; ++i)
    	{
    		temp = temp + meals[i];
    		temp.displayMeal();
    	}
    	
    		getch();
    };

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    for (i = 0; i < 7; ++i)
    {
    temp = temp + meals[i];
    temp.displayMeal();
    }


    First change the for loop condition to:

    for(i=0; i<7; i++)

    then move the line:

    temp.displayMeal();

    out of the loop and place that as the next line after the loop.
    Last edited by 7stud; 04-06-2003 at 06:10 PM.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by 7stud
    for (i = 0; i < 7; ++i)
    {
    temp = temp + meals[i];
    temp.displayMeal();
    }


    First change the for loop condition to:

    for(i=0; i<7; i++)

    then move the line:

    temp.displayMeal();

    out of the loop and place that as the next line after the loop.
    I fail to see a difference in the for loop update. ++i and i++ in this case doesn't make a difference except to increment the counter i.

    now in a case such as this:

    while(array[j++])

    would be different from

    while(array[++j])

    edit:

    in your for loops, i is only within scope of the first one, since they are not nested. so either declare i above where you declared meals[7], or do something like this:

    for(int i = 0; i < 7; i++) for your second for loop.

    and do what 7stud said as far as taking out temp.displayMeal() from the for loop.
    Last edited by alpha; 04-06-2003 at 06:00 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    alpha,

    Thanks, I didn't realize that.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Thumbs up

    Thank you both very much. I spent a lot of time on that one area and I do not know how I did not catch that error. You have both been very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Display Color dialog
    By Cherry65 in forum Windows Programming
    Replies: 4
    Last Post: 02-17-2009, 11:41 AM
  2. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  3. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  4. Help needed Please
    By jereland in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 05:30 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM