#include <iostream>
#include <string>
using namespace std;
struct person
{
string name;
int numberofpies;
string flavour;
};
int main()
{
for (int i=1; i<10; i++)
{
cout << "press <1> to add a person or press <2> to get results" << endl;
int input;
cin >> input;
person newperson[i];
string names, flavours;
int numbersofpies;
if (input==1)
{
cout << "please enter your name " << endl;
cin >> names;
cout << "enter the number of pies you ate" << endl;
cin >> numbersofpies;
cout << "enter the flavour" << endl;
cin >> flavours;
newperson[i].numberofpies=numbersofpies;
newperson[i].flavour=flavours;
newperson[i].name=names;
}
else if(input == 2)
{
int x=1;
while (x>i)
{
cout << "name : " << newperson[x].name << endl;
cout << "number of pies : " << newperson[x].numberofpies<< endl;
cout << "flavour: " << newperson[x].flavour << endl;
}goto point;
}
}point:
return 0;
}
我的问题是,该代码正常编译和运行完全,直到第一循环结束,然后将其实验和尝试不同的解决方案,我意识到这个问题是在过去的三年线后死机等等'if语句'for循环一次迭代后坠毁
newperson[i].numberofpies=numbersofpies;
newperson[i].flavour=flavours;
newperson[i].name=names;
因为删除它们后问题就消失了。然而,这个程序显然不会做它应该做的事情,所以我想我的问题在这些方面有什么问题,如果他们不是问题是什么?我该如何解决它? 另外,我不介意其他方法,如果它有我可以学习的想法,但我最重要的是了解问题,以了解没有得到程序运行的兴趣。
评论你对数组的书再次章节。 – LogicStuff
认真这就是所有你不得不说的你不能告诉我我的错误? – fahad97azawi
检查数组索引边界限制。另外,可变长度数组不是标准的C++,而是一些编译器提供的扩展,它最好避免。 – doug