回复 7楼 叶纤
嗯对!就是这个!A[a]改成A[i]。自己太粗心了,没注意到还有就是你这个手机端叫啥
下面是自己写的最终版:
#include<iostream>
#include<string>
using namespace std;
struct car { string name; int year; };
int main() {
cout << "How many cars do you wish to catalog?";
int a;
cin >> a;
cin.get();
car* A = new car[a];
for (int i = 0; i < a; i++) { //这是个循环(for语句),当i<a时,执行大括号里的语句;然后i自增1继续判定i<a
cout << "Car #" << i + 1 << ":" << endl;
cout << "Pleaseenter the make:";
getline(cin, A[i].name);
cout << "Please enter the year made:";
cin >> A[i].year;
cin.get(); //cin.get()是为了丢弃上面cin>>A[i].year输入后产生的换行符,否则下次输入时读到换行符会跳过输入
};
cout << "Here is your collection:" << endl;
for(int j=0;j<a;j++)
cout << A[j].year << " " << A[j].name << endl;
delete[] A;
return 0;
}
另外,9楼版主大佬写的那个应该看看(虽然好多看不懂),尤其是delete[]这个,确实很多时候会忘
[此贴子已经被作者于2020-1-17 10:13编辑过]