From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter3/bag_demo.cxx | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 chapter3/bag_demo.cxx (limited to 'chapter3/bag_demo.cxx') diff --git a/chapter3/bag_demo.cxx b/chapter3/bag_demo.cxx new file mode 100644 index 0000000..3ee7900 --- /dev/null +++ b/chapter3/bag_demo.cxx @@ -0,0 +1,62 @@ +// FILE: bag_demo.cxx +// This is a small demonstration program showing how the bag class is used. +#include // Provides cout and cin +#include // Provides EXIT_SUCCESS +#include "bag1.h" // With value_type defined as an int +using namespace std; +using namespace main_savitch_3; + +// PROTOTYPES for functions used by this demonstration program: +void get_ages(bag& ages); +// Postcondition: The user has been prompted to type in the ages of family +// members. These ages have been read and placed in the ages bag, stopping +// when the bag is full or when the user types a negative number. + +void check_ages(bag& ages); +// Postcondition: The user has been prompted to type in the ages of family +// members once again. Each age is removed from the ages bag when it is typed, +// stopping when the bag is empty. + + +int main( ) +{ + bag ages; + + get_ages(ages); + check_ages(ages); + cout << "May your family live long and prosper." << endl; + return EXIT_SUCCESS; +} + + +void get_ages(bag& ages) +{ + int user_input; + + cout << "Type the ages in your family." << endl; + cout << "Type a negative number when you are done:" << endl; + cin >> user_input; + while (user_input >= 0) + { + if (ages.size( ) < ages.CAPACITY) + ages.insert(user_input); + else + cout << "I have run out of room and can’t add that age." << endl; + cin >> user_input; + } +} + +void check_ages(bag& ages) +{ + int user_input; + + cout << "Type those ages again. Press return after each age:" << endl; + while (ages.size( ) > 0) + { + cin >> user_input; + if (ages.erase_one(user_input)) + cout << "Yes, I've got that age and will remove it." << endl; + else + cout << "No, that age does not occur!" << endl; + } +} -- cgit v1.2.3