C++编译异常?
#include<iostream>#include<string>usingnamespacestd;structcar{stringname;intyear;};in...
#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++) {
cout << "Car #" << i + 1 << ":" << endl;
cout << "Pleaseenter the make:";
getline(cin, A[a].name);
cout << "Please enter the year made:";
cin >> A[a].year;
cin.get();
};
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;
}
测试的时候输入第一个INT值a之后就提示:
发了异常: 写入访问权限冲突。
_Left 是 0x51EE1DC。
这个该怎么解决,是我的语句有问题么? 展开
#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++) {
cout << "Car #" << i + 1 << ":" << endl;
cout << "Pleaseenter the make:";
getline(cin, A[a].name);
cout << "Please enter the year made:";
cin >> A[a].year;
cin.get();
};
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;
}
测试的时候输入第一个INT值a之后就提示:
发了异常: 写入访问权限冲突。
_Left 是 0x51EE1DC。
这个该怎么解决,是我的语句有问题么? 展开
1个回答
2020-01-16
展开全部
for (int i = 0; i <= a; i++) //这里改为i<a
{
cout << "Car #" << i + 1 << ":" << endl;
cout << "Pleaseenter the make:";
getline(cin, A[a].name); //这里改为 A[i].name
cout << "Please enter the year made:";
cin >> A[a].year; //这里改为A[i].year
cin.get();
}; //这个分号没用
cout << "Here is your collection:" << endl;
for(int j=0;j<=a;j++) //这里改为j<a
cout << A[j].year << " " << A[j].name << endl;
delete[] A;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询