10 CPP Programs

Exercise P1.1. Write a program that prints the message, “Hello, my name is Hal!”
Then, on a new line, the program should print the message “What would you like
me to do?” Then it’s the user’s turn to type in an input. You haven’t yet learned how
to do it—just use the following lines of code:
string user_input;
getline(cin, user_input);
Finally, the program should ignore the user input and print the message “I am sorry,
I cannot do that.”
This program uses the string data type. To access this feature, you must place the
before the main function.

Here is a typical program run. The user input is printed in boldface.
Hello, my name is Hal!
What would you like me to do?
Clean up my room
I am sorry, I cannot do that.
When running the program, remember to hit the Enter key after typing the last
word of the input line.

P1.3. Write a program that computes the sum of the first ten positive integers,
1 + 2 + … + 10. Hint: Write a program of the form
int main()
cout << return 0; } Exercise P2.3. Write a program that prompts the user for two integers and then prints • The sum • The difference • The product • The average • The distance (absolute value of the difference) • The maximum (the larger of the two) • The minimum (the smaller of the two) Hint: The max and min functions are defined in the algorithm header. Exercise P2.30. Your boss, Juliet Jones, is getting married and decides to change her name. Complete the following program so that you can type in the new name for the boss: int main() { Employee boss("XXXXX, XXXXXt", 45000.00); // Your code goes here; leave the code above and below unchanged cout << "Name: " << boss.get_name() << "\n"; cout << "Salary: " << boss.get_salary() << "\n"; return 0; } The problem is that there is no set_name member function for the Employee class. Hint: Make a new object of type Employee with the new name and the same salary. Then assign the new object to boss. Exercise P3.3. Write a program that reads in three floating-point numbers and prints the largest of the three inputs. For example: Please enter three numbers: 4 9 2.5 The largest number is XXXXX Exercise P3.16. Write a program that first asks the user to type in today’s exchange rate between U.S. dollars and Japanese yen, then reads U.S. dollar values and converts each to Japanese yen. Use 0 as the sentinel value to denote the end of dollar inputs. Then the program reads a sequence of yen amounts and converts them to dollars. The second sequence is terminated by the end of the input file. Exercise P4.2. Write a procedure void sort2(int& a, int& b) that swaps the values of a and b if a is greater than b and otherwise leaves a and b unchanged. For example, int u = 2; int v = 3; int w = 4; int x = 1; sort2(u, v); // u is still 2, v is still 3 sort2(w, x); // w is now 1, x is now 4 Exercise P4.8. Write a function double get_double(string prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. (In other words, write a console version of cwin.get_double.) Here is a typical usage: salary = get_double("Please enter your salary:"); perc_raise = get_double("What percentage raise would you like?"); If there is an input error, abort the program by calling exit(1). (You will see in Chapter 5 how to improve this behavior.) Exercise P4.15. Consider the following buggy function: Employee read_employee() { cout << "Please enter the name: "; string name; getline(cin, name); cout <> salary;
Employee r(name, salary);
return r;
When you call this function once, it works fine. When you call it again in the same
program, it won’t return the second employee record correctly. Write a test harness
that verifies the problem. Then step through the function. Inspect the contents of
the string name and the Employee object r after the second call. What values do you

Exercise P5.1. Implement all member functions of the following class:
class Person
Person(string pname, int page);
void get_name() const;
void get_age() const;
string name;
int age; // 0 if unknown

New Download