1

How do you use a while loop only to add multiple values with a given point when to exit the loop and display the tallied amounts.

Note the following example. Test your program by entering 7 for the number of items and the following values for the calories: 7 - 120 60 150 600 1200 300 200

If your logic is correct, the following will be displayed: Total calories eaten today = 2630

Below is what I have written, what I require is understanding the calculation for the total calories.

#include <iostream>

using namespace std;

int main()
{
    int numberOfItems;
    int count = 1; //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;

    while (count <= numberOfItems) // count cannot be more than the number of items
    {
        cout << "Enter calorie: ";
        cin >> caloriesForItem;
        totalCalories = ; //?
        ++count;
    }
    cout << "Total calories  eaten today  = " << totalCalories;

    return 0;
}

How do I store a value, then add on that value, repeatedly until the program reaches a point to exit as per the count value

G Dube
  • 127
  • 7
  • 2
    Why is "**Total calories eaten today = 2631**"? It adds up to `2630`. – Kitswas Jun 03 '21 at 15:30
  • 1
    You could move the declaration of `caloriesForItem` to inside the `while` loop, as the first statement. This is a practice you may want to build up: declaring variables closest to their first usage. Also, that variable is only used within the `while` loop. – Thomas Matthews Jun 03 '21 at 16:19
  • @PalLaden the 2631 was a typo, apologies, I have since corrected the error upon your noticing of it. – G Dube Jun 03 '21 at 18:20

3 Answers3

2

Logic Explained

Sourcecode

#include <iostream>

using namespace std;

int main()
{
    int numberOfItems;
    int count = 1; //loop counter for the loop
    int caloriesForItem;
    long totalCalories = 0;
    cout << "How many items did you eat today? ";
    cin >> numberOfItems;
    cout << "Enter the number of calories in each of the "
         << numberOfItems << " items eaten: " << endl;

    while (count <= numberOfItems) // count cannot be more than the number of items
    {
        cout << "Enter calorie: ";
        cin >> caloriesForItem;
        totalCalories = totalCalories + caloriesForItem;
        ++count;
    }
    cout << "Total calories eaten today  = " << totalCalories;

    return 0;
}
Kitswas
  • 1,134
  • 1
  • 13
  • 30
  • Thank you @PalLaden, this [int totalCalories = 0;] made all the difference. And for explaining the reasoning, I have a much more conscience understanding. – G Dube Jun 03 '21 at 18:17
0

Also you can add them with += operator. But the result will be the same.

totalCalories += caloriesForItem;
Tinchetsu
  • 116
  • 1
  • 3
0

You should increase the number of total calories in every loop. You can easily do that using the addition assignment operator (+=). It should look like this :

totalCalories += caloriesForItem;