/*======== Fall 2024 - CS 111 Week 12 Lab Exercise date: 2024-11-15 ========*/ /*--- USING pair-programming * COPY and PASTE the contents of this file into a lab12.cpp file within the CS50 IDE * ADD the parts asked for below (one student saying what to type, the other student typing it into the CS50 IDE) * each time you want to compile: in a CS50 terminal that is open to the folder CONTAINING this .cpp file, ("Open in Integrated Terminal"), type: g++ lab12.cpp -o lab12 * IF it compiles with no errors: to run: in that same CS50 terminal that is open to the folder CONTAINING this .cpp file, type: ./lab12 * (BUT, because the interactive input in today's lab makes redirected output problematic, you do NOT need to submit a .txt file THIS time!) * Download a copy of your resulting lab12.cpp by right-clicking on its names in the file explorer on the left of the CS50 IDE, and use Gmail to MAIL a copy of this files to BOTH of you. * And, EACH of you should SUBMIT this file lab12.cpp to Canvas ---*/ /*--- by: PUT BOTH of YOUR NAMES HERE last modified: 2024-11-15 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*--- COPIED from CS 111 - Week 10 Lecture 1 ---*/ /*=== signature: name_length: string string -> int purpose: expects a first name and a last name, and returns the total length of the full name (first and last combined) tests: name_length("Jonathan", "Vex") == 11 name_length("Holly", "Dawn") == 9 ===*/ int name_length(string first_name, string last_name) { return first_name.length() + last_name.length(); } /*--- COPIED from CS 111 - Week 10 Lecture 2 ---*/ /*=== signature: porridge_state: double -> string purpose: expects a porridge temperature (in Fahrenheit), and returns a string, "mama-approved", "papa-approved", or "baby-approved" based on the following: Mama Bear's pref: < 50 degrees Papa Bear's pref: > 80 degrees Baby Bear's pref: in [50, 80] tests: porridge_state(34) == "mama-approved" porridge_state(50) == "baby-approved" porridge_state(65) == "baby-approved" porridge_state(80) == "baby-approved" porridge_state(99) == "papa-approved" ====*/ string porridge_state(double porridge_temp) { if (porridge_temp < 50) { return "mama-approved"; } else if (porridge_temp <= 80) { return "baby-approved"; } else { return "papa-approved"; } } //===== // desired maximum total length of a first+last name // before we would prefer a first initial be used const int NAME_LENGTH_MAX = 30; /*=== signature: pretty_name_2: string string -> string purpose: expects a first name and a last name, and if the combined length of both is less than NAME_LENGTH_MAX, return a string with the last name, a comma and blank, and the first name, OTHERWISE, it returns a string with the last name, a comma and blank, and the first letter in the first name (their initial) followed by a period tests: pretty_name_2("Jo", "Williams") == "Williams, Jo" pretty_name_2("Elizabetha-Lisa", "Wendell-Johnson") == "Wendell-Johnson, E." pretty_name_2("Alexander", "Graham-Bell-Johnson") == "Graham-Bell-Johnson, A." ===*/ string pretty_name_2(string first_name, string last_name) { if (name_length(first_name, last_name) < NAME_LENGTH_MAX) { return last_name + ", " + first_name; } else { return last_name + ", " + first_name.at(0) + '.'; } } /*--- ADAPTED from CS 111 - Week 10 Lecture 1 ---*/ /*=== signature: describe_grade: char -> string purpose: expects a letter grade, and returns the desired description for that grade, as follows: A or a - Excellent B or b - Very Good C or c - Acceptable T or t - Try Again BUT if given an unrecognized grade, it returns "Unrecognized Grade" tests: describe_grade('A') == "Excellent" describe_grade('B') == "Very Good" describe_grade('C') == "Acceptable" describe_grade('T') == "Try Again" describe_grade('a') == "Excellent" describe_grade('b') == "Very Good" describe_grade('c') == "Acceptable" describe_grade('t') == "Try Again" describe_grade('F') == "Unrecognized Grade" ===*/ string describe_grade(char grade) { string desired_descr = ""; switch (grade) { case 'A': case 'a': desired_descr = "Excellent"; break; case 'B': case 'b': desired_descr = "Very Good"; break; case 'C': case 'c': desired_descr = "Acceptable"; break; case 'T': case 't': desired_descr = "Try Again"; break; default: desired_descr = "Unrecognized Grade"; } return desired_descr; } /*--- interactive front-end making use of the functions above ---*/ int main() { cout << boolalpha; cout << endl; /*--- Declare TWO local variables appropriate for holding a person's FIRST name and LAST name: ---*/ /*--- Use cout to ask the user to enter a desired FIRST name, then use cin with >> and your local variable for a person's FIRST name to assign to that variable what the user enters: ---*/ /*--- Use cout to ask the user to enter a desired LAST name, then use cin with >> and your local variable for a person's LAST name to assign to that variable what the user enters: ---*/ cout << endl; cout << "The length of this person's name is: "; /*--- Use cout to print to the screen the value of the expression calling name_length with your now-filled local variables for a person's FIRST and LAST names: ---*/ cout << "The pretty_name_2 version of this person's name is: "; /*--- Use cout to print to the screen the value of the expression calling pretty_name_2 with your now-filled local variables for a person's FIRST and LAST names: ---*/ cout << endl; /*--- Declare ONE local variable appropriate for holding a person's preferred porridge temperature: ---*/ /*--- Use cout to ask the user to enter that person's preferred porridge temperature, then use cin with >> and your local variable for a person's preferred porridge temperature to assign to that variable what the user enters: ---*/ cout << endl; cout << "Fun fact: this person's porridge temperature has the recommendation: " << endl << " "; /*--- Use cout to print to the screen the value of the expression calling porridge_state with your now-filled local variable for a person's preferred porridge temperature: ---*/ cout << endl; /*--- Declare ONE local variable appropriate for holding a person's grade to be described: ---*/ /*--- Use cout to ask the user to enter that person's grade to be described, then use cin with >> and your local variable for a person's grade to be described to assign to that variable what the user enters: ---*/ cout << endl; cout << "The description for this person's grade is: "; /*--- Use cout to print to the screen the value of the expression calling describe_grade with your now-filled local variable for a person's grade to be described: ---*/ cout << endl; return EXIT_SUCCESS; }