Calculating inputs using While and For Loop

I need a help on this program it needs to calculate multiple number inputs using While Loop firstly and try the it with For Loop again.

#include <iostream>
using namespace std;

int main()
{
int numberOfItems;
int count;
int caloriesForItem;
int totalCalories;

cout <<"How many items did you eat? ";
cin >> numberOfItems;
cout <<endl;
cout <<"Enter the number of calories in each of the ";
cout << numberOfItems << " items eaten: " <<endl;

cout << "Enter Calories for Items: ";
cin >> (caloriesForItem, count);

//while (numberOfItems = 0)
//{
//cout << "Nothing Eaten" <<endl;
//}
while (caloriesForItem <= 0)
totalCalories = caloriesForItem * numberOfItems;
cout <<"Total calories eaten today = " << totalCalories <<endl;
cout<<endl;

return 0;
}
Last edited on
even here i still struggle to get the correct output
Here's your code with code tags and a couple of comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
int main()
{
	int numberOfItems;
	int count;
	int caloriesForItem;
	int totalCalories; //You need to initialize this

	cout <<"How many items did you eat? ";
	cin >> numberOfItems;
	cout <<endl;
	cout <<"Enter the number of calories in each of the ";
	cout << numberOfItems << " items eaten: " <<endl;

	cout << "Enter Calories for Items: ";
	cin >> (caloriesForItem, count); //You set caloriesForItem and count in a funny manner here

	while (caloriesForItem <= 0) //You have a while loop but you'll never exit it.
		totalCalories = caloriesForItem * numberOfItems;

	cout <<"Total calories eaten today = " << totalCalories <<endl;
	cout<<endl;

	return 0;
}


And this is what I expect you were tryign to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int main()
{
	int numberOfItems;
	int count;
	int caloriesForItem;
	int totalCalories = 0; //You need to initialize totalCalories

	cout <<"How many items did you eat? ";
	cin >> numberOfItems;
	cout <<endl;
	cout <<"Enter the number of calories in each of the ";
	cout << numberOfItems << " items eaten: " <<endl;

	cout << "Enter Calories for Items: ";
	for (count = 0; count < numberOfItems; count++)
	{
		cin >> caloriesForItem; //We need to cin with a loop.
		totalCalories += caloriesForItem; //Add the calories to the total calories.
	}

	cout <<"Total calories eaten today = " << totalCalories <<endl;
	cout<<endl;

	return 0;
}
thanks Stewbond
Topic archived. No new replies allowed.