Candy Sale Program
candy-v2.cpp
Download file: Windows | macOS | Unix
1// Program : Candy Sale (v.2)
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#include <iomanip>
9#include <iostream>
10
11void input_data(int £s);
12void perform_calculations(int pounds, double &total);
13void output_results(int pounds, double total);
14
15int main()
16{
17 int pounds; // pounds of candy purchased
18 double total; // total price of the candy purchased
19
20 input_data(pounds);
21 perform_calculations(pounds, total);
22 output_results(pounds, total);
23}
24
25//
26// An input module that gets the pounds of candy purchased from the user.
27//
28void input_data(int £s)
29{
30 std::cout << "Enter pounds of truffles purchased: ";
31 std::cin >> pounds;
32}
33
34//
35// A processing module that computes the total based on the number of pounds
36// purchased. Refer to the original exercise for details.
37//
38void perform_calculations(int pounds, double &total)
39{
40 if (pounds <= 5) {
41 total = pounds * 20;
42 }
43 else {
44 total = 100.00;
45 pounds = pounds - 5;
46 total = total + (pounds * 12.00);
47 }
48}
49
50//
51// An output module that displays the pounds of truffles purchased and the
52// total.
53//
54void output_results(int pounds, double total)
55{
56 std::cout << std::fixed << std::setprecision(2);
57 std::cout << "The total for "
58 << pounds
59 << " pound(s) of truffles is $"
60 << total
61 << std::endl;
62}