gpt4 book ai didi

c++ - Visual Studio C++ |每次我尝试从 [80]x[80] 数组循环 cout 时程序崩溃

转载 作者:行者123 更新时间:2023-11-28 01:41:33 25 4
gpt4 key购买 nike

基本上我一直在玩 C++,我正在做一个来自 this website 的练习。叫做“毕业”。到目前为止,我一直在为初学者做的很好,但这是我过去几个小时一直在努力解决的第一个问题,我无法在网上找到解决方案。

我使用了 2 个数组字符串,一个 80x80 用于 map ,一个 100x20 用于所有现有的兔子。我将所有数据放在一个字符串中,例如 SEX|COLOR|AGE|COORDINATES|NAME。因此,为了提取网格的每一部分,我总是使用 2 作为函数来遍历整个数组并剪切例如第二个字符,这样我就可以知道数组中每个兔子的颜色。

当我用它来发现数组中每只兔子的性别时,它工作正常,但我试图对颜色做同样的事情,但它不起作用。我的程序不断崩溃,并显示我在 screenshot 中留下的错误屏幕我上传了。

代码如下:

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

添加.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

#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 个子数组,因此超出了数组的范围。

关于c++ - Visual Studio C++ |每次我尝试从 [80]x[80] 数组循环 cout 时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46922508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com