-
Write a C++ program that requests and displays information as shown in the following example of output:
What is your first name? Betty Sue What is your last name? Yewe What letter grade do you deserve? B What is your age? 22 Name: Yewe, Betty Sue Grade: C Age: 22
Note that the program should be able to accept first names that comprise more than one word. Also note that the program adjusts the grade downward—that is, up one letter. Assume that the user requests an A, a B, or a C so that you don’t have to worry about the gap between a D and an F.
-
Rewrite Listing 4.4, using the C++
string
class instead ofchar arrays
.Listing 4.4 (instr2.cpp)
// instr2.cpp -- reading more than one word with getline #include <iostream> int main() { using namespace std; const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name:\n"; cin.getline(name, ArSize); // reads through newline cout << "Enter your favorite dessert:\n"; cin.getline(dessert, ArSize); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return 0; }
-
Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use
char arrays
and functions from thecstring
header file. A sample run could look like this:Enter your first name: Flip Enter your last name: Fleming Here’s the information in a single string: Fleming, Flip
-
Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string consisting of the user’s last name followed by a comma, a space, and first name. Use
string
objects and methods from thestring
header file. A sample run could look like this:Enter your first name: Flip Enter your last name: Fleming Here’s the information in a single string: Fleming, Flip
-
The
CandyBar
structure contains three members. The first member holds the brand name of a candy bar. The second member holds the weight (which may have a fractional part) of the candy bar, and the third member holds the number of calories (an integer value) in the candy bar. Write a program that declares such a structure and creates aCandyBar
variable calledsnack
, initializing its members to"Mocha Munch"
,2.3
, and350
, respectively. The initialization should be part of the declaration forsnack
. Finally, the program should display the contents of the snack variable. -
The
CandyBar
structure contains three members, as described in Programming Exercise 5. Write a program that creates an array of threeCandyBar
structures, initializes them to values of your choice, and then displays the contents of each structure. -
William Wingate runs a pizza-analysis service. For each pizza, he needs to record the following information:
- The name of the pizza company, which can consist of more than one word
- The diameter of the pizza
- The weight of the pizza
Devise a structure that can hold this information and write a program that uses a structure variable of that type. The program should ask the user to enter each of the preceding items of information, and then the program should display that information. Use
cin
(or its methods) andcout
. -
Do Programming Exercise 7 but use
new
to allocate a structure instead of declaring a structure variable. Also have the program request the pizza diameter before it requests the pizza company name. -
Do Programming Exercise 6, but instead of declaring an array of three
CandyBar
structures, usenew
to allocate the array dynamically. -
Write a program that requests the user to enter three times for the 40-yd dash (or 40-meter, if you prefer) and then displays the times and the average. Use an
array
object to hold the data. (Use a built-in array ifarray
is not available.)
chapter04
Folders and files
Name | Name | Last commit date | ||
---|---|---|---|---|
parent directory.. | ||||