我想知道是否有人可以看看为什么我在这里遇到运行时错误。
Project1.exe 中 0x6FBDEBC2 (msvcr110d.dll) 的第一次机会异常:0xC0000005:访问冲突写入位置 0x7CB67DEB。Project1.exe 中 0x6FBDEBC2 (msvcr110d.dll) 处的未处理异常:0xC0000005:访问冲突写入位置 0x7CB67DEB。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
struct person {
string name;
int age;
int weight;
string nickname;
} ;
person people[1];
for(int i = 0; i < sizeof(people); i++){
string s[4];
for(int x = 0; x < 4; x++){
cout << "Please enter person " << i << "'s " << (x == 0 ? "name" : x == 1 ? "age" : x == 2 ? "weight" : x == 3 ? "nickname" : "unknown") << "." << endl;
getline(cin, s[x]);
}
people[i].name = s[0];
stringstream(s[1]) >> people[i].age;
stringstream(s[2]) >> people[i].weight;
people[i].nickname = s[3];
}
for(int i = 0; i < sizeof(people); i++)
cout << "Person " << i << ": name = " << people[i].name << ", age = " << people[i].age << ", weight = " << people[i].weight << ", nickname = " << people[i].nickname << endl;
cout << "Please press enter to continue.";
fflush(stdin);
cin.clear();
cin.get();
}
在第二个 for 循环之前它运行良好,它似乎运行错误。