Classes and such

This is a discussion on Classes and such within the C++ Programming forums, part of the General Programming Boards category; Looks like a great board I will be honest here. I am 31 years old and I have recently decided ...

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

    Smile Classes and such

    Looks like a great board I will be honest here. I am 31 years old and I have recently decided to take some classes, to see what may spark some interest. Well right now I need some help!
    I would like to see if any one can help me figure out this mess I have made.

    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    class Meal
    {
    	friend ostream& operator<<(ostream& out, const Meal &aMeal);
    	friend istream& operator<<(istream& in, Meal &aMeal);
    public:
    	char entree[20];
    	int calorie;
    	Meal(char ent[]); //constructor
    	int operator + (Meal &meals); //overload operator +
    
    };
    
    Meal::Meal(char ent[])
    {
    	strcpy(entree, ent);
    	calorie = calorie;
    };
    
    int Meal::operator + (Meal &meals)
    {
    	int cals;
    	cals = calorie + meals.calorie;
    	return (cals);
    };
    
    ostream& operator<<(ostream& out, const Meal &aMeal)
    {
    	out<<"The total calories for all of the meals is: "<<endl;
    	return(out);
    };
    
    istream& operator<<(istream& in, Meal &aMeal)
    {
    	cout<<"Enter the entree: ";
    	in>>aMeal.entree;
    	cout<<"Enter the amount of calories: ";
    	in>>aMeal.calorie;
    	return(in);
    };
    
    
    int main(void)
    {
    	Meal meals[21];
    	for(int i = 0; i < 21; i++)
    		meals[i];
    	cin>>i;
    	cout<<i;
    
    	return 0;
    };
    Now what I am aiming for is displaying the total amount of calories for entrees that the user will input (for all 21 meals). But somewhere I have missed a few things.

    An overload extraction operator is needed to prompt the user for an entree' name and calorie count for a meal.

    Also need the statement total = breakfast + lunch + dinner; in the program... but is it possible to get this code to run without that statement?

    Thanks in advance for any replys.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    for the instream operator, it should be >> not << . in your constructor:
    Code:
    calorie = calorie;
    doesn't do anything. either pass the constructor another parameter, or delete this line.

    with the overloaded + operator the way it is, you can return void
    Code:
    void Meal::operator +(Meal &meals)
    {
        calorie += meal.calorie;
    }
    your for loop in main also doesn't do anything. it just cycles through the array.

    you may also want to think about using getline() for the istream operator. but ideally, you should not want to have cout and cin in the definition of istream. that being said, you may want to make them public. also, you may want to make the entree and calorie data members private.

Popular pages Recent additions subscribe to a feed

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