Chapter 3 – #9: How Many Calories? – Tony Gaddis – Starting Out With C++

>_ Problem

A bag of cookies holds 30 cookies. The calorie information on the bag claims that there are 10 “servings” in the bag and that a serving equals 300 calories. Write a program that asks the user to input how many cookies he or she actually ate and then reports how many total calories were consumed.

>_ Solution

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    // Constant Variables
    const int COOKIES_PER_BAG       = 30,
              SERVINGS_PER_BAG      = 10,
              CALORIES_PER_SERVING  = 300;

    // Variables
    float cookies_eaten,
          servings_eaten,
          calories_consumed,

          cookies_per_serving = COOKIES_PER_BAG / SERVINGS_PER_BAG;

    // Ask how many cookies eaten
    cout << endl;
    cout << "How many cookies eaten? ";
    cin >> cookies_eaten;

    // Calculate number of servings
    servings_eaten = cookies_eaten / cookies_per_serving;

    // Calculate caloires consumed
    calories_consumed = servings_eaten * CALORIES_PER_SERVING;

    cout << '\n' << setprecision(2) << fixed << right;

    cout << "Number of cookies per bag      = ";
    cout << setw(8) << COOKIES_PER_BAG      << endl;

    cout << "Number of servings per bag     = ";
    cout << setw(8) << SERVINGS_PER_BAG     << endl;

    cout << "Number of calories per serving = ";
    cout << setw(8) << CALORIES_PER_SERVING << endl;

    cout << "Number of cookies per serving  = ";
    cout << setw(8) << cookies_per_serving  << endl;

    cout << "Number of cookies eaten        = ";
    cout << setw(8) << cookies_eaten        << endl;

    cout << "Number of servings eaten       = ";
    cout << setw(8) << servings_eaten       << endl;

    cout << "Number of calories consumed    = ";
    cout << setw(8) << calories_consumed    << endl;

    cout << endl;
    return 0;
}

>_Get The Book!

>_ Video Walk Through

This video includes a step by step process of how I solve programming challenge #9.

>_ Chapter 3 Programming Playlist

>_That’s it!

Thank you for taking interest in what I do! I hope it was helpful for you as much as it helped me along my journey in learning to code!

>_ Also Check Out:

Category: Starting Out With C++ | Chapter 3 | Programming Challenge Solutions

Leave a Reply