Display problem

This is a discussion on Display problem within the C++ Programming forums, part of the General Programming Boards category; The program will run but does not display right. Perhaps I did not initialize something...but I can not find it ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Question Display problem

    The program will run but does not display right. Perhaps I did not initialize something...but I can not find it and have been looking over it for awhile. Can anyone help and let me know what it is I am missing.

    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[], int cal = 25);
    	Meal();
    	Meal operator+(Meal &aMeal);
    	void displayMeal();
    };
    
    Meal::Meal(char ent[], int cal )
    {
    	strcpy(entree, ent);
    	calorie = cal;
    };
    
    Meal::Meal()
    {
    	calorie = 150;
    };
    
    Meal Meal::operator+(Meal &aMeal)
    {
    	Meal exam;
    	exam.calorie = calorie + aMeal.calorie;
    	return(exam);
    };
    
    void Meal::displayMeal()
    {
    	cout<<"The entree: "<<entree<<"has"<<calorie<< " calories."<<endl;
    };
    
    ostream& operator<<(ostream &out, const Meal &aMeal)
    {
    	out<<aMeal.entree<<aMeal.calorie<<" calories "<<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 breakfast("Bagel ", 100);
    	Meal lunch("Hamburger ", 325);
    	Meal dinner("Steak ", 350);
    	Meal total(" ", 0);
    	total = breakfast + lunch + dinner;
    	cout<<"Breakfast: "<<breakfast<<endl;
    	cout<<"Lunch: "<<lunch<<endl;
    	cout<<"Dinner: "<<dinner<<endl;
    	cout<<"Daily Total: "<<total<<endl;
    	getch();
    };
    Thanks for any help!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    
    class Meal
    {
    	friend ostream& operator<<(ostream &out, const Meal &aMeal); // these 2 funcs better done as not friends but ok as is
    	friend istream& operator>>(istream &in, Meal &aMeal);
    private:
    	char entree[20];
    	int calorie;
    public:
    	Meal(char ent[], int cal = 25);
    	Meal();
    	Meal operator+(Meal &aMeal); // nonmember better
    	void displayMeal();
    };
    
    Meal::Meal(char ent[], int cal )
    {
    	strcpy(entree, ent); // first real error. use strncpy() to stop buffer overrun
    	calorie = cal;
    };
    
    Meal::Meal()
    {
    	calorie = 150;
    };
    
    Meal Meal:perator+(Meal &aMeal)
    {
    	Meal exam; // calls default con. I think u meant Meal exam(*this);
    	exam.calorie = calorie + aMeal.calorie;
    	return(exam);
    };
    
    void Meal::displayMeal()
    {
    	cout<<"The entree: "<<entree<<"has"<<calorie<< " calories."<<endl;
    };
    
    ostream& operator<<(ostream &out, const Meal &aMeal)
    {
    	out<<aMeal.entree<<aMeal.calorie<<" calories "<<endl;// needs spaces
    	return(out);
    };
    
    istream& operator>>(istream &in, Meal &aMeal)
    {
    	cout<<endl; //clears
    	cout<<"Enter the entree name: ";
    	in>>aMeal.entree;  // do not do this
    	cout<<"Enter the amount of calories: ";
    	in>>aMeal.calorie;	// so-so
    	return(in);
    };
    
    void main() // main returns an int ALWAYS
    {
    	Meal breakfast("Bagel ", 100);
    	Meal lunch("Hamburger ", 325);
    	Meal dinner("Steak ", 350);
    	Meal total(" ", 0);
    	total = breakfast + lunch + dinner;
    	cout<<"Breakfast: "<<breakfast<<endl;
    	cout<<"Lunch: "<<lunch<<endl;
    	cout<<"Dinner: "<<dinner<<endl;
    	cout<<"Daily Total: "<<total<<endl;
    	getch();
    };
    otherwise ok
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    The fields for Meal did need to be set with arguements or uses default values when no arguements are provided. I have changed public to:

    Code:
    public:
    	Meal(char ent[] = "Pizza ", int cal = 25);
    	Meal operator+(Meal &aMeal);
    	void displayMeal();
    Then I changed main to:

    Code:
    {
    	Meal breakfast("Bagel ", 100);
    	Meal lunch("Hamburger ", 325);
    	Meal dinner("Steak ", 350);
    	Meal total("Daily Total: ", 0);
    	total = breakfast + lunch + dinner;
    	cout<<"Breakfast: "<<breakfast<<endl;
    	cout<<"Lunch: "<<lunch<<endl;
    	cout<<"Dinner: "<<dinner<<endl;
    	cout<<total<<endl;
    	getch();
    };
    But I want "Daily Total: " to show up and instead is displays "Pizza". How can I fix that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 07:39 AM
  2. Display problem
    By Birdhaus in forum C++ Programming
    Replies: 1
    Last Post: 09-06-2006, 03:52 PM
  3. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  4. Display problem
    By RoD in forum C++ Programming
    Replies: 11
    Last Post: 10-09-2002, 05:25 PM
  5. display problem after retrieving from text
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-29-2002, 05:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21