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

3
  • 2
    Why is "Total calories eaten today = 2631"? It adds up to 2630. Commented Jun 3, 2021 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. Commented Jun 3, 2021 at 16:19
  • @PalLaden the 2631 was a typo, apologies, I have since corrected the error upon your noticing of it. Commented Jun 3, 2021 at 18:20

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @PalLaden, this [int totalCalories = 0;] made all the difference. And for explaining the reasoning, I have a much more conscience understanding.
0

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

totalCalories += caloriesForItem;

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.