How to count up values from a while statement?

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; //loop counter for the loop
    int caloriesForItem;
    int totalCalories;
    cout << "How many items did you eat today? ";
    cin >> numberOfItems;
    cout << "Enter the number of calories in each of the "
        << numberOfItems << " items eaten: " << endl;
//My Code
     int i = 1;
    while (i <= numberOfItems)
    {
        cout << "Total calories for item " << i << " : " ; i++;
        cin >> caloriesForItem;
    }

totalCalories = //sum of  the values inputed;

//My Code
        cout << "Total calories eaten today = " << totalCalories;
return 0;
}



here is the code , i need to be able to sum the values inputted by user. Can anyone help ?
Last edited on
Isn't 'totalCalories' the "sum of the values inputed"?
What if you add every new item to 'totalCalories'?


PS. What if number of items is 0; what is 'totalCalories' then?
Start with
int totalCalories = 0;

Inside the loop, so
totalCalories += caloriesForItem;


> int count; //loop counter for the loop
No it isn't. You don't use it.

> int i = 1;
> while (i <= numberOfItems)
A for loop is much better when you know in advance how many times you want to iterate.
Your badly placed i++ in your code is a bug waiting to happen.

for ( int i = 1 ; i <= numberOfItems ; i++ )
wraps up the whole loop into a single easy to recognise code.

@salem c ,I understand what you getting at with the for loop.

I have to do both for and while loop , they both are done , i just needed to know how to calculate the totals on the total calories, i'll use your above message, thank you!


p.s. Im new at this , so just bare with me :P

I got the code and then within the //My Code blocks , i was needed to write a loop for the total of items enter and then to have the sum of the total calories for each item

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; //loop counter for the loop
    int caloriesForItem;
    int totalCalories;
    cout << "How many items did you eat today? ";
    cin >> numberOfItems;
    cout << "Enter the number of calories in each of the "
        << numberOfItems << " items eaten: " << endl;
//My Code
     int i = 1;
    while (i <= numberOfItems)
    {
        cout << "Total calories for item " << i << " : "; i++;
        cin >> caloriesForItem;
        totalCalories += caloriesForItem;
    }



//My Code
        cout << "Total calories eaten today = " << totalCalories;
return 0;
}


so i changed it to this, its working but only 1 problem.

I have to input 7 items , it works
then
items are
120
60
150
600
1200
300
200
I enter them and get the value of 2630.
but im requested to have 2631? Its their error right?
Last edited on
@salem c

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; //loop counter for the loop
    int caloriesForItem;
    int totalCalories;
    cout << "How many items did you eat today? ";
    cin >> numberOfItems;
    cout << "Enter the number of calories in each of the "
        << numberOfItems << " items eaten: " << endl;
//My Code
    for (int i=1; i <= numberOfItems; i++)
    {
        cout << "Total calories for item " << i << " : " ;
        cin >> caloriesForItem;
        totalCalories += caloriesForItem;
    }




//My Code
        cout << "Total calories eaten today = " << totalCalories;
return 0;
}


here is my for loop
Last edited on
What value do you think totalCalories will have before the first time totalCalories += caloriesForItem; (line 18) is executed?

What value do you think it will have immediately after the first time that line is executed?

(Hint: You've completely ignored something important @salem c told you.)
@MikeyBoy , the value that it had was 16 everytime and I couldn't figure out why, i did try
int totalCalories=0; before , but i still couldn't figure out exactly how to count them up together
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
I have to input 7 items , it works
then
items are
120
60
150
600
1200
300
200
I enter them and get the value of 2630.
but im requested to have 2631? Its their error right? 

C'mon - this is basic junior maths. You don't need a program or even a calculator.
7 values all with 0 in the units, how do you expect to wind up with a result having a 1 in the units.

Either your values are not what you're supposed to type in, or someone's expectation of the result is wrong.

You should always initialise variable when they are defined. By default they are not initialised and could have any value.

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 caloriesForItem {};
	int totalCalories {};

	cout << "How many items did you eat today? ";
	cin >> numberOfItems;

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

	//My Code
	for (int i = 1; i <= numberOfItems; ++i)
	{
		cout << "Total calories for item " << i << " : ";
		cin >> caloriesForItem;
		totalCalories += caloriesForItem;
	}

	//My Code
	cout << "Total calories eaten today = " << totalCalories << '\n';
	return 0;
}

The input can fail. Therefore, it should be tested and one has to decide how failure is handled. For example:
1
2
3
4
5
6
7
8
9
int count {};
while ( count < numberOfItems &&
        (cout << "Total calories for item " << (count+1) << " : ") &&
        cin >> caloriesForItem )
{
  ++count;
  totalCalories += caloriesForItem;
}
cout << "Total calories for " << count << " items eaten today = " << totalCalories << '\n';
Topic archived. No new replies allowed.