Multiple Inputs

For my intro class I have to write a code that allows the user to input multiple integers. For example, I ask how many items that person has eaten that day (in this example lets just say 5 items), I then ask how many calories are in each item. So using 5 items in this example, how would I allow the user to input 5 different amounts of calories. Just made this account and am still getting the hang of using this website, sorry if this question has been asked before.
What will you use the data for? All purposes do not require storing the data more than brief moment.
#include <iostream>
using namespace std;

int getItems();

int getCalories (int itemsPar);

float calcAvg (int itemsPar, int caloriesPar);

int main()
{
int items, calories;
float avgCals;

items = getItems(); //5
calories = getCalories(items);
avgCals = calcAvg(items, calories);

return 0;
}

int getItems()
{
int items;
cout << "How many items did you eat today? ";
cin >> items;

return items;
}

int getCalories(int itemsPar)
{
int calories, i;

cout << "Enter the number of calories in each of the\n"
<< itemsPar << " items eaten:\n ";

for ( i = 1; i <= itemsPar; i++)
{
cin >> calories;

}

return calories;
}

float calcAvg(int itemsPar, int caloriesPar)
{
int sum = 0;
sum += caloriesPar;

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



return sum;
}

This is what I have so far and It's outputting the correct answer. If I eat 5 items and those
5 items have 200, 300, 560, 45, and 129 calories the sum of them should be 1234 calories.
All I get is 129 calories.
Let me know what you think of the program that I made for you. In regards to your program, the "int sum=sum+caloriesPar;" this is after you 0 sum out first.


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
#include <iostream>
using namespace std;
int main() 
{
int Calorie, Counter=-10, Totalfat=0;
cout<<"*********How fat are you??********"<<endl<<"Please enter the number of meals you've eaten today..."<<endl;
while (Counter<0 or Counter>10)
	{
	cin>>Counter;
	}
cout<<"Please enter the number of calories you ate for each meal..."<<endl;

for(int A=Counter; A>0; A--)
	{
		cout<<"meal "<<((Counter-A)+1)<<" :";
		cin>>Calorie;
		cout<<endl;
		Totalfat=Totalfat+Calorie;
	}
        cout<<"Over "<<Counter<<" Meals, you've' consumed "<<Totalfat<<" Calories"<<endl;
	if (Totalfat>1500)
	{
		cout<<"You really need to get off this program, start working out and eating healthy!!!!!!!!!!"<<endl<<"************************YOU FAT*********************";
	}
}
Last edited on
Topic archived. No new replies allowed.