Help with code

Hello Everyone,
program in total adds the number of calories multiplied by number of donuts which is wrong, I'm trying to make it display the total number of calories by adding the total calories of order 1, 2 and 3, any suggestions?
#include <iostream>
using namespace std;

class Donut
{
private:
string type;
int quantity;
int numOfCalories;
public:

Donut()
{

type="";
quantity=0;
numOfCalories=0;
}
Donut(string t,int q,int n)
{
type=t;
quantity=q;
numOfCalories=n;
}

friend ostream &operator<<( ostream &output, const Donut &D )
{
cout << "Type of Donut : " << D.type <<endl<< " Quantity : " << D.quantity<<endl<< " Number Of Calories : "<<D.numOfCalories<<endl<<" Total calories for this order : " << D.numOfCalories*D.quantity;
return output;
}

friend istream &operator>>( istream &input, Donut &D )
{
cin >> D.type;
cin >> D.quantity;
cin >> D.numOfCalories;
return input;
}

Donut operator+(const Donut& D)
{
Donut total;
total.type="Daily total";
total.quantity=quantity*D.quantity;
total.numOfCalories=quantity*D.quantity+quantity*D.quantity+quantity*D.quantity;
return total;

}
};
int main()
{
Donut order1;
Donut order2;
Donut order3;
Donut total;

cout << "Order1: " << endl;
cout <<"Enter type of Donut(jelly filled, sprinkles, cruller, chocolate, boston-cream),";
cout <<"quantity, and number of calories in each donut: "<<endl;
cin >> order1;
cout << "Order2: " << endl;
cout <<"Enter type of Donut(jelly filled, sprinkles, cruller, chocolate, boston-cream),";
cout <<"quantity, and number of calories in each donut: "<<endl;
cin >> order2;
cout << "Order3: " << endl;
cout <<"Enter type of Donut(jelly filled, sprinkles, cruller, chocolate, boston-cream),";
cout <<"quantity, and number of calories in each donut: "<<endl;
cin >> order3;

total=order1+order2+order3;

cout<<order1<<endl;
cout<<order2<<endl;
cout<<order3<<endl;
cout<<total<<endl;


return 0;
}
1
2
total.quantity=quantity*D.quantity;  // Why are you multiplying?
total.numOfCalories=quantity*D.quantity+quantity*D.quantity+quantity*D.quantity;


What are you trying to do there? That's the same as:
 
total.numOfCalories = 3 * (quantity*D.quantity);

which I'm pretty sure is not what you want.

I would think you want something like this:
1
2
total.quantity = quantity + D.quantity;
total.numOfCalories = (quantity * numOfCalories) + (D.quantity * D.numOfCalories);


However, you now have a discrepancy in how you're representing calories. Using the constructor, numOfCalories represents the number of calories of a single donut. But after doing the above calculation, numOfCalories now represents the total number of calories for all donuts. That's not the same thing as your original representation.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.