繁体   English   中英

Visual Studio C ++ 每当我尝试从[80] x [80]数组中循环一个cout时,程序就会崩溃

[英]Visual Studio C++ | Program crashes everytime I try to loop a cout from an array of [80]x[80]

基本上,我一直在玩C ++,并且正在该网站上进行名为“毕业”的练习。 到目前为止,我对初学者的表现一直很好,但这是我最近一个小时一直在苦苦挣扎的问题,我无法在线找到解决方案。

我正在使用2个数组字符串,一个80x80用于地图,一个100x20用于所有现有的兔子。 我将所有数据放到一个字符串中,例如SEX | COLOR | AGE | COORDINATES | NAME。 因此,要提取网格的每一块,我总是使用2来使函数在整个数组中运行并剪切例如第二个字符,这样我就可以知道数组中每个兔子的颜色。

当我用它来发现阵列中每个兔子的性别时,它工作得很好,但是我试图对颜色做同样的事情,但是它不起作用。 我的程序不断崩溃,并显示了我在上传的屏幕快照中留下的错误屏幕。

这是代码:

Main.cpp

#include <iostream>
#include "Add.h"
#include "GetNames.h"
#include <string>
#include "PlaceOnMap.h"
#include "Colours.h"

using namespace std;

int start = 1;

int main()
{
    while (start == 1)
    {
        Add(5);
        PlaceOnMap(bunniestotal);
        Colour();
        start = 0;
    }

    system("pause");
    return 0;
}

地址

#pragma once
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include "GetNames.h"

using namespace std;

int colours;
int cB, rB;
int bunnies = 0;
int bunniestotal = 0;
int sex = 0;
int h = 0;

string listB[10][100];

void Add(int x)
{

    srand(time(NULL));

    /***********************************************************************************************/

    while (h < 1) {
        for (int rB = 0; rB < 10; rB++) //DO THE SLOTS FOR THE BUNNIES
        {
            for (int cB = 0; cB < 100; cB++)
            {
                listB[rB][cB] = "-";
            }
        }
        h = 1;
    }

    /***********************************************************************************************/

    while (bunnies < x) //CHOOSE RANDOM SLOTS FOR BUNNIES
    {
        rB = rand() % 10;
        cB = rand() % 100;

        if (listB[rB][cB] == "-")
        {
            listB[rB][cB] = "B";
            bunnies++;
        }
    }

    bunniestotal = bunniestotal + bunnies;
    bunnies = 0;

    /***********************************************************************************************/

    for (int rB = 0; rB < 10; rB++) //SET SEX AND COLOUR
    {
        for (int cB = 0; cB < 100; cB++)
        {
            if (listB[rB][cB] == "B")
            {
                sex = rand() % 2 + 1;
                //cout << sex << endl;
                if (sex == 1)
                {
                    colours = rand() % 4 + 1;
                    switch (colours) 
                    {
                    case 1: listB[rB][cB] = "mR";
                        break;
                    case 2: listB[rB][cB] = "mY";
                        break;
                    case 3: listB[rB][cB] = "mC";
                        break;
                    case 4: listB[rB][cB] = "mB";
                        break;
                    }
                    listB[rB][cB] = listB[rB][cB] + "0";
                    //cut = listB[rB][cB].substr(0, 1);
                    //cout << listB[rB][cB] << "Cut - " << cut << endl;
                    //listB[rB][cB] = listB[rB][cB] + GetNames("M") + " ";
                    //cout << listB[rB][cB] << endl;
                    //listB[rB][cB] = listB[rB][cB] + GetNames("M");
                    //cout << listB[rB][cB] << endl;
                }
                else if (sex == 2)
                {
                    colours = rand() % 4 + 1;
                    switch (colours) 
                    {
                    case 1: listB[rB][cB] = "fR";
                        break;
                    case 2: listB[rB][cB] = "fY";
                        break;
                    case 3: listB[rB][cB] = "fC";
                        break;
                    case 4: listB[rB][cB] = "fB";
                        break;
                    }
                    listB[rB][cB] = listB[rB][cB] + "0";
                    //cut = listB[rB][cB].substr(0, 1);
                    //cout << listB[rB][cB] << "Cut - " << cut << endl;
                    //cout << rB << " " << cB << endl;
                    //listB[rB][cB] = listB[rB][cB]+GetNames("F") + " ";
                    //cout << listB[rB][cB] << endl;
                    //listB[rB][cB] = listB[rB][cB] + GetNames("F");
                    //cout << listB[rB][cB] << endl;
                }
            }
        }
    }
}

PlaceOnMap.h

#pragma once
#include <iostream>
#include "Add.h"
#include <string>
#include <sstream>
#include <Windows.h>
#include "Colours.h"

using namespace std;

string map[80][80];
string cut;
ostringstream join;
ostringstream join1;
int xB, yB;
int hh = 0;
int capslock;
int y = 0;
int z = 0;
int u, o;

void PlaceOnMap(int bunniesn)
{
    HANDLE color = GetStdHandle(STD_OUTPUT_HANDLE);

    //cout << "PlaceOnMap " << listB[rB][cB] << endl;
    //cout << rB << " " << cB << endl;
    //cut = listB[rB][cB].substr(0, 1);
    //cout << cut << endl;

    /***********************************************************/

    while (hh < 1)
    {
        for (int xB = 0; xB < 80; xB++) // CREATE MAP
        {
            for (int yB = 0; yB < 80; yB++)
            {
                map[xB][yB] = "-";
            }
        }
        hh = 1;
    }

    /***********************************************************/

        //cout << bunniesn << endl;
        //cout << rB << endl;
        //cout << cB << endl;

        for (y = 0; y < 10; y++)
        {
            for (z = 0; z < 100; z++)
            {
            nextone:

                if (listB[y][z].length() < 4) //DOESN'T LET BUNNIES THAT ARE ALREADY PLACED ENTER THIS LOOP
                {
                    //cout << y << " " << z << endl;
                    cut = listB[y][z].substr(0, 1); //CUTS THE STRING IN ORDER TO KNOW IF IT IS A FEMALE OR A MALE

                    if (cut == "f" || cut == "m")
                    {
                        if (cut == "f")
                        {
                            capslock = 1;
                        }
                        else if (cut == "m")
                        {
                            capslock = 2;
                        }

                    generate:
                        xB = rand() % 80;
                        yB = rand() % 80;
                        if (map[xB][yB] == "-")
                        {
                            //cout << "Found a slot!" << endl;
                            //Sleep(2000);

                            //join << xB;
                            //join1 << yB;

                            listB[y][z] += to_string(xB);
                            listB[y][z] += to_string(yB);
                            //cout << "Size - " << listB[y][z].length() << endl;

                            if (listB[y][z].length() == 5)
                            {
                                listB[y][z] = listB[y][z] + " ";
                                listB[y][z] = listB[y][z] + " ";
                            }
                            else if (listB[y][z].length() == 6)
                            {
                                listB[y][z] = listB[y][z] + " ";
                            }

                            cout << listB[y][z] << endl;
                            //Sleep(6000);
                            if (capslock == 1)
                            {
                                map[xB][yB] = "f";
                            }
                            else if (capslock == 2)
                            {
                                map[xB][yB] = "m";
                            }
                            z++;
                            x++;
                            //cout << x << endl;
                            //Sleep(3000);
                            if (x < bunniesn)
                            {
                                goto nextone;
                            }
                            else
                            {
                                goto done;
                            }

                        }

                        else
                        {
                            goto generate;
                        }

                    }
                }

            }

        }

done:
    cout << "All done" << endl;
    SetConsoleTextAttribute(color, 15);
}

Colours.h(发生问题的地方!!)

#pragma once
#include <iostream>
#include "PlaceOnMap.h"
#include <string>
#include "Add.h"

using namespace std;

string wordString;
int t, r;

void Colour()
{
    for (t = 0; t < 20; t++)
    {
        for (r = 0; r < 100; r++)
        {
            wordString = listB[t][r].substr(1, 2); //CUTS THE STRING IN ORDER TO KNOW ITS COLOR
            //cout << wordString << " ";
            if (wordString == "R")
            {
                cout << "GOT RED" << endl;
            }
            else if (wordString == "C")
            {
                cout << "GOT CYAN" << endl;
            }
            else if (wordString == "B")
            {
                cout << "GOT BLUE" << endl;
            }
            else if (wordString == "Y")
            {
                cout << "GOT YELLOW" << endl;
            }
        }
        cout << endl;
    }
}

如果真的很蠢,对不起! 金达菜鸟,但只是试图学习更多! :)如果您需要更多信息,请随时提出要求!

谢谢。

在Colours.h中,将for (t = 0; t < 20; t++)更改for (t = 0; t < 10; t++) 您仅分配了10个子数组,因此您超出了数组的范围。

快速浏览您的代码:

您定义了:

string listB[10][100];

然后在Colours.h中将第一个索引迭代到20:

for (t = 0; t < 20; t++)

适用于:

listB[0][r];
listB[1][r];
listB[2][r];
listB[3][r];
listB[4][r];
listB[5][r];
listB[6][r];
listB[7][r];
listB[8][r];
listB[9][r];

然后您尝试访问

listB[10][r]

这是第11个索引,未分配。

如果您在某处重新定义它,而我错过了,对不起。

暂无
暂无

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

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