Candy Sale Program

candy-v2a.cpp

Download file: Windows | macOS | Unix

  1// Program    : Candy Sale (v.2, with notes)
  2// Author     : Prof. Krofchok
  3// Date       : Fall 2025
  4// Description: This program implements the "Candy Sale" problem from the "Can
  5//              You Think Like a Computer?" exercise. Modules are used to break
  6//              things down into small pieces.
  7//
  8//              Here is the original step-by-step solution:
  9//                 1. Declare 'pounds' as Integer.
 10//                 2. Declare 'total' as Real.
 11//                 3. Record the amount of truffles in the variable named
 12//                      'pounds'.
 13//                 4. If 'pounds' is more than 5, then go to Step 7;
 14//                      otherwise, go to the next step.
 15//                 5. Set 'total' to 'pounds' times 20.
 16//                 6. Go to Step 10.
 17//                 7. Set 'total' to 100.
 18//                 8. Subtract 5 from 'pounds'.
 19//                 9. Multiply 'pounds' by 12 and add it to 'total'.
 20//                10. End.
 21//
 22//              The corresponding lines of C++ code have been marked. Remember
 23//              that 'main' is in charge, you'll be jumping back-and-forth
 24//              between modules. Start at the beginning of the 'main' module,
 25//              and return back to 'main' upon reaching the end of each other
 26//              module.
 27
 28#include <iostream>  // This line allows us to use modules provided by the C++
 29                     //   language to read data from the keyboard (std::cin)
 30                     //   and display it on the screen (std::cout, std::endl).
 31#include <iomanip>   // This line allows us to display the money value using a
 32                     //   formatting module (std::setprecision) that works in
 33                     //   conjunction with a module from the 'iostream' library
 34                     //   (std::fixed).
 35
 36// The modules other than 'main' listed below are our own creation. Since
 37// they're not a standard part of the C++ language, we must declare them here
 38// so the compiler knows to expect them later in the file. For now, just accept
 39// that the word 'void' is a required part of the syntax for modules other than
 40// 'main'.
 41void input_data(int &pounds);
 42void perform_calculations(int pounds, double &total);
 43void output_results(int pounds, double total);
 44
 45//
 46// This is the main module of the program, and starts running from here. Note
 47// how it delegates its work to other modules, and thus looks like an outline
 48// of the major tasks being performed.
 49//
 50int main()
 51{
 52    // Start here...
 53
 54    // Since 'main' is responsible for transferring data between the various
 55    // modules, it needs separate variables for each number it manages. These
 56    // are declared on the two lines that follow.
 57    int pounds;    // pounds of candy purchased           (Step 1)
 58    double total;  // total price of the candy purchased  (Step 2)
 59
 60    input_data(pounds);  // Jump down to the module...    (Step 3)
 61    perform_calculations(pounds, total);  // Jump...      (Steps 4-9)
 62    output_results(pounds, total);  // Jump...
 63
 64    // You've reached the end! Stop here.                 (Step 10)
 65}
 66
 67//
 68// An input module that gets the pounds of candy purchased from the user. This
 69// module prompts the user for input, and waits until they type a number and
 70// press the 'Enter' key.
 71//
 72// Because this module obtains a number that must be given back to 'main', its
 73// variable is flagged with an ampersand in the first line of the module.
 74//
 75void input_data(int &pounds)
 76{
 77    // The user is prompted with a message, so they know what to input
 78    std::cout << "Enter pounds of truffles purchased: ";
 79
 80    // The number they type is stored in the 'pounds' variable
 81    std::cin >> pounds;  // (Step 3, formally)
 82
 83    // You've reached the end of this module! Go back to 'main'...
 84}
 85
 86//
 87// A processing module that computes the total based on the number of pounds
 88// purchased. Refer to the step-by-step solution for the details.
 89//
 90// This module gets the number of pounds from 'main', then computes the total.
 91// Since the total is a calculated number given back to 'main', its variable
 92// must be flagged with an ampersand in the first line of the module.
 93//
 94void perform_calculations(int pounds, double &total)
 95{
 96    if (pounds <= 5) {        // (Step 4, but reversed)
 97        total = pounds * 20;  // (Step 5)
 98        // Skip the next part and go to the end of the module (this is Step 6,
 99        //   implicitly)
100    }
101    else {
102        total = 100.00;                    // (Step 7)
103        pounds = pounds - 5;               // (Step 8)
104        total = total + (pounds * 12.00);  // (Step 9)
105    }
106
107    // You've reached the end of this module! Go back to 'main'...
108}
109
110//
111// An output module that displays the pounds of truffles purchased and the
112// total. This is a very important step, and is glossed over in the step-
113// by-step version.
114//
115// Since all the numbers here are being supplied by 'main', neither of the
116// variables on the first line of the module are flagged with an ampersand.
117//
118void output_results(int pounds, double total)
119{
120    // Since the total is a money value, let's change the formatting so that
121    // all real-number variables are displayed with two places after the
122    // decimal point
123    std::cout << std::fixed << std::setprecision(2);
124
125    // Display the pounds of candy purchased and the total on the screen
126    std::cout << "The total for "
127              << pounds
128              << " pound(s) of truffles is $"  // <-- note the space at the
129              << total                         //     beginning of this string
130              << std::endl;
131
132    // You've reached the end of this module! Go back to 'main'...
133}