-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5_9_7.cpp
More file actions
32 lines (32 loc) · 817 Bytes
/
5_9_7.cpp
File metadata and controls
32 lines (32 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<string>
int main()
{
using namespace std;
struct car
{
string make;
int year;
};
cout << "How many cars do you wish to catalog? ";
int num;
cin >> num;
cin.get(); //如果没有这一行,cin读取数字后,将回车键生成的换行符留在了输入队列中,
//后面的getline看到换行符后,将认为是一个空行 ,并将一个空字符串赋给string对象
car * pcar = new car[num];
for (int i = 0; i < num; i++)
{
cout << "Car #" << i+1 << ":" << endl;
cout << "Please enter the make: ";
getline(cin, pcar[i].make);
cout << "Please enter the year made: ";
cin >> pcar[i].year;
cin.get(); //同上
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < num; i++)
cout << pcar[i].year << " " << pcar[i].make << endl;
delete[] pcar;
system("pause");
return 0;
}