/*--- CS 111 - Week 10 Lecture 2 - 2025-10-30 compile using: g++ 111lect10-2.cpp -o 111lect10-2 run using: ./111lect10-2 by: Sharon Tuttle last modified: 2025-10-30 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*=== signature: porridge_pref: double -> string purpose: expects a porridge temperature, and returns: * "mama-approved" if it is less than 50 degrees * "papa-approved" if it is more than 80 degrees * "baby-approved" if in [50, 80] tests: porridge_pref(13) == "mama-approved" porridge_pref(50) == "baby-approved" porridge_pref(60) == "baby-approved" porridge_pref(80) == "baby-approved" porridge_pref(212) == "papa-approved" ===*/ string porridge_pref(double porridge_temp) { if (porridge_temp < 50) { return "mama-approved"; } else if (porridge_temp <= 80) { return "baby-approved"; } else { return "papa-approved"; } } /*=== your client has an odd request to be able to get a "display version" of someone's first and last name that is guaranteed not to exceed MAX_WIDTH characters long ===*/ /*=== signature: display_name: string string -> string purpose: expects a first name and last name, and returns one of the following: * if the length of the first name is 0, return up to the first MAX_WIDTH characters of the last name * else, if the (lengths of both + 1) <= MAX_WIDTH, return the first name, a blank, and the last name * else, if the (length of last name + 3) <= MAX_WIDTH, then return the first initial, a period and a blank, then the last name * otherwise, return a string with just up to the first MAX_WIDTH charactetrs of the last name tests: display_name("", "Shakira") == "Shakira" display_name("", "Silly-Long-Name-789012345") == "Silly-Long-Name-789012345" display_name("", "Goofy-Long-Name-789012345678") == "Goofy-Long-Name-789012345" display_name("Jo", "Williams") == "Jo Williams" CONTINUE COMPLETING TESTS NEXT WEEK display_name("Elizabeth", "Wendell-Johnson") == "Elizabeth Wendell-Johnson" display_name("", "") == "" display_name("", "") == "" display_name("", "") == "" display_name("", "") == "" ===*/ const int MAX_WIDTH = 25; /*=== WE WILL FINISH THIS NEXT WEEK string display_name(string first_name, string last_name) { } ===*/ /*--- test the function above and also try out string class substr methods ---*/ int main() { cout << boolalpha; cout << "*** Testing: porridge_pref ***" << endl; cout << (porridge_pref(13) == "mama-approved") << endl; cout << (porridge_pref(50) == "baby-approved") << endl; cout << (porridge_pref(60) == "baby-approved") << endl; cout << (porridge_pref(80) == "baby-approved") << endl; cout << (porridge_pref(212) == "papa-approved") << endl; // trying out C++ string class substr methods cout << endl; const string DB_INTRO = "CS 325"; // start at position 0, grab 3 characters cout << "[" << DB_INTRO.substr(0, 3) << "]" << endl; // start at position 2, grab the rest of the string cout << "[" << DB_INTRO.substr(2) << "]" << endl; // what if you ask for more characters than are there? // Happily, no error -- just grabs until the end of the // string: cout << "[" << DB_INTRO.substr(1, 27) << "]" << endl; // NOTE that method at returns a char, // but method substr returns a string // (which can be compared to a char* using ==) cout << (DB_INTRO.at(4) == '2') << endl; cout << (DB_INTRO.substr(4, 1) == "2") << endl; return EXIT_SUCCESS; }