Reputation: 3
Basically I've been playing a little with C++ and I am doing an exercise from this website called "Graduation". 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. I am putting all the data together in one string like 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.
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
#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
#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!!)
#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.
Upvotes: 0
Views: 46
Reputation: 1798
From quickly looking through your code:
You defined:
string listB[10][100];
And you iterate the first index up to 20 in Colours.h:
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.
If you redefined it somewhere and I missed I am sorry.
Upvotes: 1
Reputation: 11020
In Colours.h, change for (t = 0; t < 20; t++)
to for (t = 0; t < 10; t++)
. You only have 10 subarrays allocated, so you were going outside the bounds of the array.
Upvotes: 1