简体   繁体   English

C ++,数组-Windows错误当机,但没有编译器错误

[英]C++, Array - Windows Error Crash but no Compiler Error

Ok so here's my code. 好的,这是我的代码。 I'm getting no errors in the compiler, however when I run this program it lets me input 2 names then crashes with a Window's Error. 我在编译器中没有收到任何错误,但是当我运行该程序时,它让我输入了2个名称,然后因窗口错误而崩溃。 What in the heck am I doing wrong?! 我到底在做什么错?!

#include <iostream>

using namespace std;

//declaration of variables

int i; // Loop Counter
int x; // Number of Family Members

int main()
{

string FamilyName[x]; // Array of Names


cout << "Enter Number of Family Members" <<endl;
cin >> x;

for (i = 0 ; i < x ; i++){
        cout << "Enter Family Member's Name: " <<endl;
        cin >> FamilyName[i];
}
for (i = 0 ; i < x ; i++){
    cout << FamilyName[i] <<endl;
}

return 0;
}

You can solve the problem in two ways, allocate the array to a large size(x should be initialized...and it should be a constant value) and then the code becomes: 您可以通过两种方式解决该问题,将数组分配为大尺寸(x应该被初始化...并且它应该是一个常数),然后代码变为:

#include <iostream>
#include <string>
using namespace std;

//declaration of variables

int i; // Loop Counter
const int x = 500; // MAX Number of Family Members

int main()
{

string FamilyName[x]; // Array of Names

int count;
cout << "Enter Number of Family Members" <<endl;
cin >> count;

for (i = 0 ; i < count ; i++){
        cout << "Enter Family Member's Name: " <<endl;
        cin >> FamilyName[i];
}
for (i = 0 ; i < count ; i++){
    cout << FamilyName[i] <<endl;
}

return 0;
}

Or use dynamic memory allocation to read the number of family member first and then do the allocation: 或使用动态内存分配先读取家庭成员的数量,然后进行分配:

int main()
{

int count;
cout << "Enter Number of Family Members" <<endl;
cin >> count;

auto FamilyName = new string[count];

for (i = 0 ; i < count ; i++){
        cout << "Enter Family Member's Name: " <<endl;
        cin >> FamilyName[i];
}
for (i = 0 ; i < count ; i++){
    cout << FamilyName[i] <<endl;

    delete[] FamilyName;
}

Hope this helps 希望这可以帮助

You are not allocating any memory to your string array, on this line string FamilyName[x]; // Array of Names 您不会在此行上为字符串数组分配任何内存, string FamilyName[x]; // Array of Names string FamilyName[x]; // Array of Names x is not defined. string FamilyName[x]; // Array of Names未定义string FamilyName[x]; // Array of Names x It also may be a good idea to set a max array size or have some way of pushing on to the end of the array. 设置最大数组大小或采用某种方式推送到数组末尾也是一个好主意。

The size of your array should be constant. 数组的大小应为常数。

Maybe use a vector of strings since vectors can grow dynamically. 也许使用字符串向量,因为向量可以动态增长。

string name;
vector<string> FamilyName;
for(int i = 0; i < x; i++)
{
    cin >> name;
    FamilyName.push_back(name);
}

Either that or give your array a constant size larger than any family size you might encounter. 要么给您的数组一个恒定的大小,要么使其大于您可能遇到的任何家庭大小。

string FamilyName[100];

if you wouldn't ever get a family with over 100 members. 如果您永远不会拥有一个成员超过100人的家庭。

You need to either allocate reasonable memory for FamilyName or use STL: 您需要为FamilyName分配合理的内存或使用STL:

string[] FamilyName;
//after get x
FamilyName = new string[x];

or 要么

vector<string> FamilyName;
cin << temp_string;
FamilyName.push_back(temp_string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM