简体   繁体   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]

Basically I've been playing a little with C++ and I am doing an exercise from this website called "Graduation". 基本上,我一直在玩C ++,并且正在该网站上进行名为“毕业”的练习。 So far I've been doing good for a beginner but here's my first problem which I have been struggling for the last hours and I can't find a solution online. 到目前为止,我对初学者的表现一直很好,但这是我最近一个小时一直在苦苦挣扎的问题,我无法在线找到解决方案。

I am using 2 strings of arrays, one 80x80 for the map and one 100x20 for all the existing bunnies. 我正在使用2个数组字符串,一个80x80用于地图,一个100x20用于所有现有的兔子。 I am putting all the data together in one string like SEX|COLOR|AGE|COORDINATES|NAME. 我将所有数据放到一个字符串中,例如SEX | COLOR | AGE | COORDINATES | NAME。 So to extract each piece of the grid I always use 2 for functions to run throughout all the array and cut for example the 2nd character so I can know the color of each bunny in the array. 因此,要提取网格的每一块,我总是使用2来使函数在整个数组中运行并剪切例如第二个字符,这样我就可以知道数组中每个兔子的颜色。

It worked fine when I used it to discover the sex of each bunny in the array but I am trying to do the same for the color and it isn't working. 当我用它来发现阵列中每个兔子的性别时,它工作得很好,但是我试图对颜色做同样的事情,但是它不起作用。 My program keeps crashing with the error screen I left in the screenshot I uploaded. 我的程序不断崩溃,并显示了我在上传的屏幕快照中留下的错误屏幕。

Heres the code: 这是代码:

Main.cpp 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;
}

Add.h 地址

#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 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 (Where the problem is happening!!) 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;
    }
}

If it is something really dumb I am sorry! 如果真的很蠢,对不起! Kinda noob but just trying to learn more and more! 金达菜鸟,但只是试图学习更多! :) If you need any more info feel free to ask for! :)如果您需要更多信息,请随时提出要求!

Thank you. 谢谢。

In Colours.h, change for (t = 0; t < 20; t++) to for (t = 0; t < 10; t++) . 在Colours.h中,将for (t = 0; t < 20; t++)更改for (t = 0; t < 10; t++) You only have 10 subarrays allocated, so you were going outside the bounds of the array. 您仅分配了10个子数组,因此您超出了数组的范围。

From quickly looking through your code: 快速浏览您的代码:

You defined: 您定义了:

string listB[10][100];

And you iterate the first index up to 20 in Colours.h: 然后在Colours.h中将第一个索引迭代到20:

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

Which works for: 适用于:

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];

And then you try accessing 然后您尝试访问

listB[10][r]

which is the 11th index and not allocated. 这是第11个索引,未分配。

If you redefined it somewhere and I missed I am sorry. 如果您在某处重新定义它,而我错过了,对不起。

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

相关问题 在Visual Studio项目中使用在vs80(Visual Studio 2005)下编译的C ++静态库在c ++ vs140(Visual Studio 2015)下编译 - Using static libraries compiled C++ under vs80 (Visual Studio 2005) in a Visual Studio project compiling under c++ vs140 (Visual Studio 2015) C ++ Visual Studio Cout返回字符串 - c++ visual studio cout returned string Visual Studio 2012 C ++没有提示 - visual studio 2012 c++ no cout Visual Studio和C ++ / OpenGL输出cout - cout output with visual studio and C++/OpenGL C ++ Visual Studio 2008,delete()操作使程序崩溃 - C++ Visual Studio 2008, delete() operation crashes program 当我尝试比较c ++中的两个字符串时,程序崩溃了? - Program Crashes when I try to compare two strings in c++? C ++-当我使用sprintf()函数时程序崩溃(std :: cout正常工作) - C++ - Program crashes when I use sprintf() function (std::cout works fine) Visual C ++ 2012 cout在运行时崩溃 - Visual C++ 2012 cout crashes during run time 每当我为C ++程序提供大量输入集时,DOS崩溃 - My DOS crashes everytime I gave large input set for my C++ program 尝试读取数组 c++ 时,for 循环崩溃程序 - For loop crashes program while trying to read array c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM