Академический Документы
Профессиональный Документы
Культура Документы
6
7
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
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
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
#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:
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:
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()
string name;
string dessert;
8
9
#include <iostream>
#include <string>
#include <cstring>
4
5
int main()
8
9
char firstname[20];
10
char lastname[20];
11
char thirdname[80]={'x'};
12
13
14
15
cin.getline(firstname,20);
16
17
18
cin.getline(lastname,20);
19
20
strcpy(thirdname,lastname);
21
strcat(thirdname,", ");
22
strcat(thirdname,firstname);
23
24
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()
8
9
string firstname;
10
string lastname;
11
string thirdname;
12
13
14
getline(cin,firstname);
15
16
17
getline(cin,lastname);
18
19
20
21
22
23
cin.get();
24
return 0;
25
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
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
Aleksander says: