Вы находитесь на странице: 1из 15

1.

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? Yew
What letter grade do you deserve? B
What is your age? 22
Name: Yew, 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 downwardthat is,
up one letter.
Assume that the user requests an A, a B, or a C so that you dont have
to worry about the
gap between a D and an F.
My Answer:
1 #include <iostream>
2 int main()
3{
4
5

using namespace std;

6
7

char firstname[20]; // self explanatory variables

char lastname[20];

int age;

1
0

char grade;

1
1

cout << "What is your first name? "; // get the first name

cin.getline(firstname,20);
// use getline to read a line of words, not just
1
one word, for example first & middle, "James S"
2
1
3

cout << "What is your last name? "; // get the last name

1
cin.getline(lastname,20); // use getline for the whole line, so you can get
4 lastname & suffix at the same input line
1
5

1
6
1
7
1
8
1
9

cout << "What letter grade do you deserve? ";

cin >> grade; // get the grade, cin recognizes int input as char too, so you
can
type char input for int variable
2
0
2
1
2
2

cout << "What is your age ? ";


(cin >> age).get();

// get the age

// .get() is used to remove extra end of line char

2
grade = grade + 1; // or grade++, however, we don't know about ++
3 operator until Chapter 5.
2
cout << "Name: " << lastname << ", " << firstname << endl;
4 first & Last

// display

2
cout << "Grade: " << char (grade) << endl; // increase the grade and
5 display the character value of corresponding int number (ASCII number)
2
6
2
7

cout << "Age: " << age << endl; // disp the age

cin.get();

return 0;
2
8}
2
9
3
0
3
1

+++ Nice, simple & straightforward program.


My only comment on the code is with the use of getline. Doesnt it terminate reading the line
input when it comes to whitespace? Mine was made more complicated with the use of the
new keyword, the use of pointers and included a struct (written with VS 2008).
Your comments on my code would be appreciated.
1 // q1.cpp
2

#include <iostream>

PAGE 197

3
4
5
6
7 struct student
8 {
char first_name[20];
9
char last_name[20];
10
int age;
11
char grade;
12};
13
14int main()
{
15
16
using namespace std;
17
int newgrade = 0;
18
19
student *ps = new student;
// allocate memory using "NEW"
cout << "What is your first name? ";
20
cin.get(ps->first_name, (sizeof(ps->first_name)));
21
cin.get();
22
cout << "What is your last name? ";
23
cin.get(ps->last_name, (sizeof(ps->last_name)));
cin.get();
24
cout << "What letter grade do you deserve? ";
25
(cin >> ps->grade).get();
26
newgrade = int(ps->grade) +1;
27
cout << "What is your age? ";
28
(cin >> ps->age).get();
cout << "Name: " << ps->last_name << ", " << ps->first_name << endl;
29
cout << "Grade: " << char(newgrade) << endl;
30
// char(newgrade) converts from ASCII number to a char ... eg 66 = B
31
cout << "Age: " << ps->age << endl;
32
cin.get();
return 0;
33
34}
35
36
37
Reply

admin says:

August 13, 2011 at 11:23 pm


Hi Ryuu,
getline reads the whole line input until end of line character \n, and then saves the
input except for end of line char \n.

This way you can use getline to read First & Lastname in one input on one line, as
requested in this exercise, for example, cin.getline(firstname,20) can read this Alex
S and cin.getline(lastname,20) to read Ignatkov Sr.
Also, I have updated this solution slightly, eliminating extra newgrade variable.
Reply

Bazil says:

September 5, 2011 at 4:50 pm


Hi. Nice solution.
You wrote in string 32
cout << "Grade: " << char(newgrade) << endl;
but in Chapter 4 (and Chapters 1.2 and 3) author don't says about increment operation. Ad it
means, that we don't know about this operation. If you can please write another solution
about grade without increment.
Reply

Bazil says:
September 5, 2011 at 4:54 pm
Admin. Sorry.
You wrote in string 25
cout << "Grade: " << char (++grade) << endl;
Reply

admin says:
September 11, 2011 at 10:24 am
Youre right, we dont know about ++ operator until Chapter 5. Ive updated the
program to use regular increment expression for grade.

2. Rewrite Listing 4.4, using the C++ string class instead of char arrays.
My Answer:

#include <iostream>

#include <string>

int main()

using namespace std;

string name;

string dessert;

8
9

cout << "Enter your name:\n";

10 getline(cin,name); // reads through newline


11 cout << "Enter your favorite dessert:\n";
12 getline(cin,dessert);
13 cout << "I have some delicious " << dessert;
14 cout << " for you, " << name << ".\n";
15
16 cin.get();
17 return 0;
18 }
3. 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 users last name
followed by a comma, a space, and first name. Use char arrays and
functions from the
cstring header file. A sample run could look like this:
Enter your first name: Flip
Enter your last name: Fleming
Heres the information in a single string: Fleming, Flip
My Answer:
1

#include <iostream>

#include <string>

#include <cstring>

4
5

int main()

using namespace std;

8
9

char firstname[20];

10

char lastname[20];

11

char thirdname[80]={'x'};

12
13
14

cout << "What is your first name? ";

15

cin.getline(firstname,20);

16
17

cout << "What is your last name? ";

18

cin.getline(lastname,20);

19
20

strcpy(thirdname,lastname);

21

strcat(thirdname,", ");

22

strcat(thirdname,firstname);

23
24

cout << "Name: " << thirdname << endl;

25
26
27

cin.get();

28

return 0;

29

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 users last name followed by a comma, a space,
and first name. Use string objects and methods from the
string header file. A sample run could look like this:
Enter your first name: Flip
Enter your last name: Fleming
Heres the information in a single string: Fleming, Flip
1

#include <iostream>

#include <string>

#include <cstring>

4
5

int main()

using namespace std;

8
9

string firstname;

10

string lastname;

11

string thirdname;

12
13

cout << "What is your first name? ";

14

getline(cin,firstname);

15
16

cout << "What is your last name? ";

17

getline(cin,lastname);

18
19

thirdname=lastname+ ", " + firstname;

20
21

cout << "Name: " << thirdname << endl;

22
23

cin.get();

24

return 0;

25

5. 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 a
CandyBar variable called snack, initializing its members to Mocha
Munch, 2.3, and
350, respectively. The initialization should be part of the declaration for
snack. Finally,
the program should display the contents of the snack variable.
My Answer:

1
2
3
4
5 #include <iostream>
6 #include <string>
7
8 struct CandyBar
9{
1
0

std::string brand;

1
1

int calories;

float weight;

1 }snack = {
2
"Mocha Munch",
1
3

2.3,

1
4 };
1
5

350

int main()

1
{
6
using namespace std;
1
7
1 cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight <<
8 "\nCalories: " << snack.calories << endl;
1
9

cin.get();
2
0 return 0;
2}
1
2
2
2
3

6. The CandyBar structure contains three members, as described in


Programming Exercise
5. Write a program that creates an array of three CandyBar structures,
initializes them to
values of your choice, and then displays the contents of each structure.
My Answer:
1 #include <iostream>
2 #include <string>
3
4 struct CandyBar
5{
6

std::string brand;

double weight;

int calories;

9 };
1
0

int main()

1
{
1
using namespace std;
1
2
1 CandyBar snack[3] =
3
{
1
{"Snickers",3.4,450},
4
1
5

{"Mars",5.5,300},
{"Twix",9.3,600}

1 };
6
1
7 cout << "Brand: " << snack[0].brand << " Weight: " << snack[0].weight << "
Calories: " << snack[0].calories << endl;
1
8 cout << "Brand: " << snack[1].brand << " Weight: " << snack[1].weight << "
Calories: " << snack[1].calories << endl;
1
9 cout << "Brand: " << snack[2].brand << " Weight: " << snack[2].weight << "
Calories: " << snack[2].calories << endl;
2
0

2
1
2
2
2
3
2 cin.get();
4
return 0;
2
5}
2
6
2
7
2
8
7. 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) and cout.
My Answer:
1 #include <iostream>
2 #include <string>
3
4 struct pizza
5{
6

std::string name;

double diameter;

double weight;

9 };
1
0

int main()
1
{
1
using namespace std;
1
2
1 pizza pizzainput;
3
1
4 cout << "Enter pizza name: ";
1 getline(cin,pizzainput.name);
5
1 cout << "Enter diameter of pizza: ";
6
cin >> pizzainput.diameter;
1
7
1 cout << "Enter weight of pizza: ";
8
(cin >> pizzainput.weight).get();
1
9
2
0 cout << "Name: " << pizzainput.name << " Diameter: " <<
pizzainput.diameter << " Calories: " << pizzainput.weight << endl;
2
1
2 cin.get();
2
return 0;
2
3}
2
4
2
5
2
6
2
7
2
8

2
9
3
0
3
1
8. 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.
My Answer:
1 #include <iostream>
2 #include <string>
3
4 struct pizza
5{
6

std::string name;

double diameter;

double weight;

9 };
1
0

int main()
1
{
1
using namespace std;
1
2
1 pizza *pizzainput = new pizza;
3
1
4 cout << "Enter diameter of pizza: ";
1 (cin >> pizzainput->diameter).get();
5
1 cout << "Enter pizza name: ";
6
getline(cin,pizzainput->name);
1
7

1
8
1
9
2
0
2
1
2
2 cout << "Enter weight of pizza: ";
2 (cin >> pizzainput->weight).get();
3
2
4
2 cout << "Name: " << pizzainput->name << " Diameter: " << pizzainput5 >diameter << " Calories: " << pizzainput->weight << endl;
2
6 delete pizzainput;
2
7

cin.get();
2
8 return 0;
2}
9
3
0
3
1
3
2
3
3
9. Do Programming Exercise 6, but, instead of declaring an array of
three CandyBar structures,
use new to allocate the array dynamically.
My Answer:
1 #include <iostream>
2 #include <string>

3
4 struct CandyBar
5{
6

std::string brand;

double weight;

int calories;

9 };
1
0

int main()
1
{
1
using namespace std;
1
2
1 CandyBar *snack = new CandyBar[3];
3
1
4 snack[0].brand = "Snickers"; snack[0].weight = 3.4; snack[0].calories = 450;
1 snack[1].brand = "Mars"; snack[1].weight = 5.5; snack[1].calories = 300;
5 snack[2].brand = "Twix"; snack[2].weight = 9.3; snack[2].calories = 600;
1
6

cout << "Brand: " << snack[0].brand << " Weight: " << snack[0].weight << "
1 Calories: " << snack[0].calories << endl;
7
cout << "Brand: " << snack[1].brand << " Weight: " << snack[1].weight << "
1 Calories: " << snack[1].calories << endl;
8
cout << "Brand: " << snack[2].brand << " Weight: " << snack[2].weight << "
1 Calories: " << snack[2].calories << endl;
9
delete [] snack;
2
0
2 cin.get();
1
return 0;
2
}
2
2
3
2
4

2
5
2
6
2
7
2
8

Priyank Bansal says:


July 18, 2012 at 7:58 am
we are not using * before snacks in entering and fetching data, WHY????
Reply

Aleksander says:

July 6, 2013 at 2:14 pm


Hi there
Because pointer to the array snacks is equivalent to array implementation in c+
+ and you can use such pointers as you use arrays with some restrictions

Вам также может понравиться