2016-07-30 186 views
-2
#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; 

因为删除它们后问题就消失了。然而,这个程序显然不会做它应该做的事情,所以我想我的问题在这些方面有什么问题,如果他们不是问题是什么?我该如何解决它? 另外,我不介意其他方法,如果它有我可以学习的想法,但我最重要的是了解问题,以了解没有得到程序运行的兴趣。

+3

评论你对数组的书再次章节。 – LogicStuff

+1

认真这就是所有你不得不说的你不能告诉我我的错误? – fahad97azawi

+1

检查数组索引边界限制。另外,可变长度数组不是标准的C++,而是一些编译器提供的扩展,它最好避免。 – doug

回答

2

person newperson[i];正在声明可变长度数组,而且VLA是非标准供应商特定的编译器扩展。不要使用它们。如果您需要可变长度阵列,请改为使用std::vector

在这种情况下,您的代码未定义行为,因为你的循环变量i总是出你分配VLA的界限的,所以当你尝试设置阵列的成员,newperson[i]正触及到周围的记忆在阵列之外。这就是为什么你的代码崩溃。

数组索引是基于0的,但是您的循环变量是基于1的。因此,在第一次迭代中,您将分配一个包含1个元素的数组,然后访问第二个元素。在第二次迭代中,您将分配一个包含2个元素的数组,然后访问第三个元素。等等

2

当你宣布你的阵列这样

person newperson[i]; 

它使大小i的新数组,这意味着指数从0(包括)到i-1,包容,允许在newperson阵列。这是一个问题,因为索引newperson[i]是非法的。

此外,newperson的大小直到运行时才知道,这意味着它是一个可变长度数组; C++标准不允许这样做,所以你正在使用一个流行的扩展。

修复这个问题很简单 - 移动申报外循环,并使其

person newperson[10]; 

您需要修改这个代码的一些其他问题:

  • ifor循环和xwhile循环需要从零开始,而不是一个。
  • goto应该避免;在这种情况下,break就足够了。
  • i达到10时,无法输出。这可能没有问题,但您可能需要强制的某些输出以提醒最终用户这种情况。例如,输入2可能会退出循环,并且while循环可能正好在for循环之后。这将优雅地处理goto/break问题,并且不会有重复的代码。
+0

让你的循环从0开始,而不是从1开始。 – pie3636

+0

@ pie3636是的,OP似乎在两个地方('i'和'x')错过了这个部分。 – dasblinkenlight

1

让我们看一下第一次迭代:

i=1,所以definion:

person newperson[i]; 

定义长度的数组1.

现在,当你分配:

  newperson[i].numberofpies=numbersofpies; 

您访问数组的第二项(因为newperson[0]是第一项,而newperson[1]是被调用的),这并不存在。

什么你真的想要做的就是定义:

person newperson[10]; 

循环,并从i=0迭代。

0
#include <iostream> 
#include <string> 

using namespace std; 

struct person 
{ 
    string name; 
    int numberofpies; 
    string flavour; 
}; 

int main() 
{ 
const int Pcount = 10; 
int input; 
cin >> input; 
person newperson[Pcount]; 
string names, flavours; 
int numbersofpies; 

for (int i=1; i<Pcount; i++) 
    { 
    cout << "press <1> to add a person or press <2> to get results" << endl; 
     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; 

      } 
      break; 
     } 


    } 
     return 0;  
} 

试着像这样至少做到这一点。

-1

你被错误地分配内存对于一个人阵 让我们来声明一个常量保持元件的数量要存储在阵列中

const int NUM_PERSON =10; 

#define NUM_PERSON 10; 

进入循环前添加此 这是为10个人类型的对象分配足够的内存

person newperson[NUM_PERSON] 

现在你可以访问它的元素通过使用语法

newperson[i].numberofpies 
newperson[i].flavour 
newperson[i].name 
+0

对一个bug_的_off没有帮助。 –

0
  1. 采取person newperson[i];外面的for循环并将其更改为person newperson[10];
  2. 变化while (x>i)while (x<=i)
  3. 的结束前添加x++; while循环。

下面是编辑的代码:

#include <iostream> 
#include <string> 

using namespace std; 

struct person 
{ 
    string name; 
    int numberofpies; 
    string flavour; 
}; 

int main() 
{ 
    person newperson[10]; 
    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; 
    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; 
       x++; 
      }goto point; 
     } 


    }point: 
     return 0;  
}