How to convert values from .art file into ASCII art in C++ - c++

Working on a project where I am supposed to load a file based on user input, convert the data in that file into coordinates in the window and then use ASCII characters to draw a picture.
Files are in .art and start with the width and height of the window needed and each subsequent line is the ROW, COLUMN, CHARACTER, and COUNT that needs to be drawn. So basically the position the particular character should be started at and then how many times it should be drawn.
What I am struggling with is how to import that data into something usable so as I can draw the requested image. My initial thought is to import the data as a 4-D array but then I draw a blank as to where I should go from there.
Example lines for a .art file:
50 x 25
2, 15, *, 9
2, 48, *, 9
3, 6, *, 15
UPDATE: Given up on trying to load the files since I just can't wrap my head around that so I've changed to just drawing the art by hand and calling a string but I'm even having issues with that. With my code below, if I select option 1 the output is 001FFA80 rather than outputting the ASCII art that is in the string.
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
int main ()
{
string shapeCupid[]=
{" ",
" .. ",
" $. ,o$$$o. ",
" $. $$$$$$$o. .. ",
" .$. $' $$$$$$ ,o'' ",
" .$' $ '$$$$$,o'.,' .oo ' ",
" .$' $. $$$$' ,, .o'. ",
" .$' '$o. 'O$ .. ooo''',oo ' ",
" .$' .o$' '$$'' ,,o' ",
" .%$,,,,,ooO' ' ,,o'' ",
" .$o. ,o' $o ..oo' ",
" ''O'''''''''',' $'$. .o' "};
string shapeFly = "Fly";
string shapeHeart = "Heart";
string shapeImpossible = "Impossible";
string shapeSeuss = "Seuss";
string shapeWorry = "Worry";
int UserInput;
cout << "What do you want to draw?\n";
cout << "1. Cupid\n2. Fly\n3. Heart\n4. Impossible\n5. Seuss\n6. Worry\nNumber: ";
cin >> UserInput;
if (UserInput == 1){
cout << shapeCupid;}
else if (UserInput == 2){
cout << shapeFly;}
else if (UserInput == 3){
cout << shapeHeart;}
else if (UserInput == 4){
cout << shapeImpossible;}
else if (UserInput == 5){
cout << shapeSeuss;}
else if (UserInput == 6){
cout << shapeWorry;}
else if (UserInput != 1 || 2 || 3 || 4 || 5 || 6)
cout << "Please select proper value.\n";
system("pause");
return 0;
}

Here's a quick and dirty version that might help. I didn't add a ton of error checking, but it works for the 2 examples I used. If you do plan to embed the data as string literals you'll want to be sure to escape any special characters like backslash and double quotes. The Boop example has both.
For the embedded string literal data the first line must be padded to be as long as the longest line since it is used to write the width. An enhancement would be to iterate through all lines to get the max width and use that.
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using StringVec = std::vector<std::string>;
StringVec shapeCupid =
{
" ",
" .. ",
" $. ,o$$$o. ",
" $. $$$$$$$o. .. ",
" .$. $' $$$$$$ ,o'' ",
" .$' $ '$$$$$,o'.,' .oo ' ",
" .$' $. $$$$' ,, .o'. ",
" .$' '$o. 'O$ .. ooo''',oo ' ",
" .$' .o$' '$$'' ,,o' ",
" .%$,,,,,ooO' ' ,,o'' ",
" .$o. ,o' $o ..oo' ",
" ''O'''''''''',' $'$. .o' "
};
//Borrowed from http://www.chris.com/ascii/index.php?art=cartoons/betty%20boop
StringVec boop =
{
" _(,__ __), ",
" (_,d888888888b,d888888888b",
" d888888888888/888888888888b_)",
" (_8888888P'\"\"'`Y8Y`'\"\"'\"Y88888b",
" Y8888P.-' ` '-.Y8888b_)",
" ,_Y88P (_(_( )_)_) d88Y_,",
" Y88b, (o ) (o ) d8888P",
" `Y888 '-' '-' `88Y`",
" ,d/O\\ c /O\\b,",
" \\_/'.,______w______,.'\\_/",
" .-` `-.",
" / , d88b d88b_ \\",
" / / 88888bd88888`\\ \\",
" / / \\ Y88888888Y \\ \\",
" \\ \\ \\ 88888888 / /",
" `\\ `. \\d8888888b, /\\\\/",
" `.//.d8888888888b; |",
" |/d888888888888b/",
" d8888888888888888b",
" ,_d88p\"\"q88888p\"\"q888b,",
" `\"\"'`\\ \"`| /`'\"\"`",
" `. |===/",
" > | |",
" / | |",
" | | |",
" | Y /",
" \\ / /",
" jgs | /| /",
" / / / |",
" /=/ |=/"
};
void Write(const std::string& filename, StringVec& data)
{
if(data.empty())
{
return;
}
std::ofstream out(filename);
if(out)
{
out << data[0].size() << " x " << data.size() << "\n";
for(size_t row = 0; row < data.size(); ++row)
{
const std::string& line = data[row];
size_t col_start = 0;
size_t col_end = 0;
char col_char = line[0];
for(size_t col = 0; col < line.size(); ++col)
{
//If we hit a new character write the previous run to the file
if(col_char != line[col])
{
//but only if it wasn't a run of spaces.
if(col_char != ' ')
{
out << row << ", " << col_start << ", " << col_char << ", " << col_end - col_start + 1 << "\n";
}
col_char = line[col];
col_start = col;
col_end = col;
}
col_end = col;
}
//write the last run
if(col_char != ' ')
{
out << row << ", " << col_start << ", " << col_char << ", " << col_end - col_start + 1 << "\n";
}
}
}
}
StringVec Read(const std::string& filename)
{
StringVec data;
std::ifstream in(filename);
if(in)
{
char dummy;
size_t width;
size_t height;
if(in >> width >> dummy >> height)
{
data.resize(height, std::string(width, ' '));
}
if(!data.empty())
{
size_t row;
size_t col;
char ch;
size_t len;
while(in >> row >> dummy >> col >> dummy >> ch >> dummy >> len)
{
for(size_t i = col; i < col + len; ++i)
{
data[row][i] = ch;
}
}
}
}
return data;
}
void Print(const StringVec& data)
{
for(const std::string& s : data)
{
std::cout << s << "\n";
}
std::cout << "\n";
}
int main()
{
Write("cupid.art", shapeCupid);
Print(Read("cupid.art"));
Write("boop.art", boop);
Print(Read("boop.art"));
return 0;
}

Related

How to memoize a recursive problem to avoid re-calculating subproblems?

I am trying to solve this problem:
https://www.geeksforgeeks.org/count-possible-decodings-given-digit-sequence/
Example:
Input:
Input str = "121"
Total decoding:: 3 :: ABA AU LA
I am able to code this problem through recursion. But the code fails to process a bigger input sequence (for e.g., i/p str = 11111111111111111111111111111111111111111)
This is happening because I am calculating sub-problems again-and-again.
Can anyone help me by letting me know how to memoize below sample code?
PS - I know there are other ways to solve this problem. But I don't want to do that. I want to memoize this solution only. It will help me to build my concept. Please help.
Here is the code:
#include "iostream"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
namespace solution3
{
void solve(string str, string& out, vector<string>& v)
{
if (str.size() == 0)
{
v.push_back(out);
return;
}
//we have 2 choices:
//ch#1: take 1st char of str
//ch#2: take 1st and 2nd chars of str
if (str.size() >= 1)//ch#1: take 1st char of str
{
string out1 = out;
string str1 = str;
int num1 = stoi(str.substr(0, 1)); // converting string at index 0 to integer
if (num1) // we will not consider if the string at index 0 is zero.
{
out1.push_back(('#' + num1)); //<-- It will conevrt 1 into A; 2 into B; and so on.
str1 = str1.erase(0, 1);//erase the index 0 from str1.
solve(str1, out1, v);
}
}
if (str.size() >= 2)//ch#2: take 1st and 2nd chars of str
{
string out2 = out;
string str2 = str;
int num2 = stoi(str.substr(0, 2)); // converting string at index 0 and 1 to integer
// checking if num2 is a valid number for decoding.
// num2 should be - NON-ZERO, 1st char is not ZERO, is within the range of 1 and 26.
if (num2 && str[0] != '0' && num2 > 0 && num2 <= 26)
{
out2.push_back(('#' + num2));
//Erase 1st two chars from str
str2 = str2.erase(0, 1);//erase the index 0 from str1.
str2 = str2.erase(0, 1);//erase the index 0 from str1.
solve(str2, out2, v);
}
}
}
void alphacode(string str)
{
string out;
vector<string> v; //<-- To store all the Decodings
solve(str, out, v);
cout << "Total decoding:: " << v.size() << ":: ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
}
int main()
{
string str = "25114";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "3333333333";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "202";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "2010";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111111111111111111111111"; //<-- takes too much time! How to solve this?
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
return 0;
}
You can memoize each substring that you are currently working with, which you're forming after deleting one or two characters, depending on the case. Something like this:
#include "iostream"
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
map<string, vector<string>> dp;
namespace solution3
{
void solve(string str, string& out, vector<string>& v)
{
if (str.size() == 0)
{
v.push_back(out);
return;
}
//we have 2 choices:
//ch#1: take 1st char of str
//ch#2: take 1st and 2nd chars of str
if(dp.find(str) != dp.end()) {
vector<string> current = dp[str];
for(string s: current) {
v.push_back(s);
}
return;
}
if (str.size() >= 1)//ch#1: take 1st char of str
{
string out1 = out;
string str1 = str;
int num1 = stoi(str.substr(0, 1)); // converting string at index 0 to integer
if (num1) // we will not consider if the string at index 0 is zero.
{
out1.push_back(('#' + num1)); //<-- It will conevrt 1 into A; 2 into B; and so on.
str1 = str1.erase(0, 1);//erase the index 0 from str1.
solve(str1, out1, v);
}
}
if (str.size() >= 2)//ch#2: take 1st and 2nd chars of str
{
string out2 = out;
string str2 = str;
int num2 = stoi(str.substr(0, 2)); // converting string at index 0 and 1 to integer
// checking if num2 is a valid number for decoding.
// num2 should be - NON-ZERO, 1st char is not ZERO, is within the range of 1 and 26.
if (num2 && str[0] != '0' && num2 > 0 && num2 <= 26)
{
out2.push_back(('#' + num2));
//Erase 1st two chars from str
str2 = str2.erase(0, 1);//erase the index 0 from str1.
str2 = str2.erase(0, 1);//erase the index 0 from str1.
solve(str2, out2, v);
}
}
dp[str] = v;
}
void alphacode(string str)
{
string out;
vector<string> v; //<-- To store all the Decodings
solve(str, out, v);
cout << "Total decoding:: " << v.size() << ":: ";
// for (int i = 0; i < v.size(); i++)
// cout << v[i] << " ";
cout << endl;
}
}
int main()
{
string str = "25114";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "3333333333";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "202";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "2010";
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
cout << "----------------" << endl;
str = "1111111111111111111111111111111"; //<-- takes too much time! How to solve this?
cout << "IpStr:: " << str << endl;
solution3::alphacode(str);
return 0;
}

creating a c++ program that displays hexadecimal-formatted data from a bmp file

I'm trying to create a program that displays output of a bmp file in the form of hexadecimal. So far I get the output, but I need it to be organized a certain way.
The way it needs to be organized is with the address of the bmp file to be on the left column and then 16 bytes of data in hex across each row in the order they appear in the file. While leaving an extra space between every 8 bytes. So far, I got the hexadecimal to show up, I just need help with organizing it.
What I have:
What I'm trying to make it look like:
Here is my code:
#include <iostream> // cout
#include <fstream> // ifstream
#include <iomanip> // setfill, setw
#include <stdlib.h>
using namespace std; // Use this to avoid repeated "std::cout", etc.
int main(int argc, char *argv[]) // argv[1] is the first command-line argument
[enter image description here][1]{
// Open the provided file for reading of binary data
ifstream is("C:\\Users\\Test\\Documents\\SmallTest.bmp", ifstream::binary);
if (is) // if file was opened correctly . . .
{
is.seekg(0, is.end); // Move to the end of the file
int length = is.tellg(); // Find the current position, which is file length
is.seekg(0, is.beg); // Move to the beginning of the file
char * buffer = new char[length]; // Explicit allocation of memory.
cout << "Reading " << length << " characters... ";
is.read(buffer, length); // read data as a block or group (not individually)
if (is)
cout << "all characters read successfully.\n";
else
cout << "error: only " << is.gcount() << " could be read.\n";
is.close();
// Now buffer contains the entire file. The buffer can be printed as if it
// is a _string_, but by definition that kind of print will stop at the first
// occurrence of a zero character, which is the string-ending mark.
cout << "buffer is:\n" << buffer << "\n"; // Print buffer
for (int i = 0; i < 100; i++) // upper range limit is typically length
{
cout << setfill('0') << setw(4) << hex << i << " ";
cout << setfill('0') << setw(2) << hex << (0xff & (int)buffer[i]) << " ";
}
delete[] buffer; // Explicit freeing or de-allocation of memory.
}
else // There was some error opening file. Show message.
{
cout << "\n\n\tUnable to open file " << argv[1] << "\n";
}
return 0;
}
You could do it something like this:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <cctype>
std::ostream& fullLine(std::ostream& out, const std::vector<uint8_t>& v, size_t offset)
{
//save stream state so we can restore it after all the hex/setw/setfill nonsense.
std::ios oldState(0);
oldState.copyfmt(out);
out << std::hex << std::setfill('0') << std::setw(8) << offset << " ";
for (size_t i = 0; i < 16; ++i)
{
if (i == 8) out << " ";
out << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint32_t>(v[i + offset]) << " ";
}
out << " ";
//restore stream state to print normal text
out.copyfmt(oldState);
for (size_t i = 0; i < 16; ++i)
{
out << (std::isprint(v[i + offset]) ? static_cast<char>(v[i + offset]) : '.');
}
out << "\n";
return out;
}
int main()
{
std::vector<uint8_t> data;
std::ifstream f("test.txt", std::ios::binary);
if (f)
{
f.seekg(0, f.end);
data.resize(static_cast<size_t>(f.tellg()));
f.seekg(0, f.beg);
f.read((char*)data.data(), data.size());
const size_t numFullLines = data.size() / 16;
const size_t lastLineLength = data.size() % 16;
for (size_t i = 0; i < numFullLines; ++i)
{
if (!fullLine(std::cout, data, i * 16))
{
std::cerr << "Error during output!\n";
return -1;
}
}
}
return 0;
}
There's probably a fancy way to do it, but I usually go for brute force when I'm looking for particular output using iostreams.
How to handle the partial last line is up to you. :)
Use the % operator to break the line after every 16th count:
cout << hex;
for(int i = 0; i < 100; i++)
{
if(i && (i % 16) == 0)
cout << "\n";
cout << setfill('0') << setw(2) << (buffer[i] & 0xFF) << " ";
}
I need it to be organized a certain way.
In another answer, I submitted this form of dumpByteHex()... perhaps it can help you achieve what you want. (see also https://stackoverflow.com/a/46083427/2785528)
// C++ support function
std::string dumpByteHex (char* startAddr, // reinterpret_cast explicitly
size_t len, // allows to char* from T*
std::string label = "",
int indent = 0)
{
std::stringstream ss;
if(len == 0) {
std::cerr << "\n dumpByteHex() err: data length is 0? " << std::endl << std::dec;
assert(len != 0);
}
// Output description
ss << label << std::flush;
unsigned char* kar = reinterpret_cast<unsigned char*>(startAddr); // signed to unsigned
std::string echo; // holds input chars until eoln
size_t indx;
size_t wSpaceAdded = false;
for (indx = 0; indx < len; indx++)
{
if((indx % 16) == 0)
{
if(indx != 0) // echo is empty the first time through for loop
{
ss << " " << echo << std::endl;
echo.erase();
}
// fields are typically < 8 bytes, so skip when small
if(len > 7) {
if (indent) { ss << std::setw(indent) << " "; }
ss << std::setfill('0') << std::setw(4) << std::hex
<< indx << " " << std::flush;
} // normally show index
}
// hex code
ss << " " << std::setfill('0') << std::setw(2) << std::hex
<< static_cast<int>(kar[indx]) << std::flush;
if((indx % 16) == 7) { ss << " "; wSpaceAdded = true; } // white space for readability
// defer the echo-of-input, capture to echo
if (std::isprint(kar[indx])) { echo += kar[indx]; }
else { echo += '.'; }
}
// finish last line when < 17 characters
if (((indx % 16) != 0) && wSpaceAdded) { ss << " "; indx++; } // when white space added
while ((indx % 16) != 0) { ss << " "; indx++; } // finish line
// the last echo
ss << " " << echo << '\n';
return ss.str();
} // void dumpByteHex()
Output format:
0000 11 22 33 44 55 66 00 00 00 00 77 88 99 aa ."3DUf....w...

c++ Script that finds questions in string

I am new at coding , been coding for about a week and i am trying to do a script that finds the "?" and the "." in the script , then outputs their position in the script and i use those value to print the question to a text file.
Except it does not really work.
If you put the value in like this, it works.
myfile << test.substr( 18, 20 )
But like this it does not work it just print the whole script from the value of dot[0] until the end of the script.
myfile << test.substr( dot[0], interrogation[0] )
The way that i use to find the "?" position in the string is also not very accurate.
Where there is the .
if(x > 0){
I had a while loop but i replaced it for debugging reasons .
This is the whole code.
If you can help me i appreciate it.
Thanks.
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main (){
std::vector< int > interrogation ;
std::vector< int > dot;
string look = "?";
string look_again = ".";
string test = "ver. o que e isto? nao sei. ola? adeus. fghfghfhfghf";
string::size_type pos = test.find(look);
string::size_type sop = test.find(look_again);
string::size_type exc = test.find(look_again_again);
while (pos != std::string::npos)
{
int a = pos ;
int b = sop;
cout << " . found at : " << sop << std::endl;
cout << " ? found at : " << pos << std::endl;
interrogation.push_back(a);
dot.push_back(b);
string fragment = test.substr (0 , pos ); // works
//cout << fragment << endl ;
string fragment2 = test.substr (0 , sop ); // works
//cout << fragment2 << endl ;
pos = test.find(look, pos + 1);
sop = test.find(look_again, sop + 1);
}
int x = 1;
if(x > 0){
int a = 1;
int q = dot[a];
int w = interrogation[a];
// to save text
// to save text
string save = "saved_question.txt" ;
ofstream myfile;
myfile.open (save.c_str(), ios::app);
myfile << test.substr( 18, 20 ) + "\n" ;
myfile.close();
cout << "Question saved in text file" << endl;
}
}
The code is not finished yet but i got it working with help.
Thanks
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main (){
std::vector< int > interrogation ;
std::vector< int > dot;
//std::vector< int > exclamation;
string look = "?";
string look_again = ".";
string look_again_again = "!";
string test = " ver.o que e isto? nao sei. ola? adeus.";
string::size_type pos = test.find(look);
string::size_type sop = test.find(look_again);
while (pos != std::string::npos)
{
int a = pos ;
int b = sop;
cout << " . found at : " << sop << std::endl;
cout << " ? found at : " << pos << std::endl;
// cout << " ! found at : " << exc << std::endl;
interrogation.push_back(a);
dot.push_back(b);
//exclamation.push_back(c);
string fragment = test.substr (0 , pos ); // works
//cout << fragment << endl ;
string fragment2 = test.substr (0 , sop) ; // works
//cout << fragment2 << endl ;
string fragment3 = test.substr (dot.back() + 1, interrogation.back() - dot.back()); // works
cout << fragment3 << endl ;
pos = test.find(look, pos + 1);
sop = test.find(look_again, sop + 1);
}
}
You can do something like this:
void function(string str) {
string sentence = "";
for(int i=0; i < str.length(); i++) {
if(str[i] == '.' || str[i] == '!')
sentence = ""; // the sentence is not a question so clear the sentence
else if(str[i] == '?') {
sentence.push_back(str[i]);
cout << sentence << endl; // output the question - just replace cout with your ofstream
}
else
sentence.push_back(str[i]);
}
}
I think I've seen this question before though..

Binary Search Tree output in tree structure

I used the code from this post, How to display binary search tree in console properly?. It complies fine on my IDE, but it doesnt print out anything so it basically shows nothing. I want it to print out in the tree structure like the post mentioned I posted here. I think I have the code typed wrong for 3rd argument for the printLevel function in int main(). Would char* x = " " work fine? It does give warning message, but I'm not sure if I do it right.
string printLevel(Node *pRoot, int level, string gap)
{
if (level == 1)
{
if (pRoot == 0)
{
cout << ".. printLevel - " << pRoot << ": " << gap << "-" << gap << "\n";
return gap + "-" + gap;
}
stringstream out;
out << pRoot->data;
//cout << ".. printLevel - " << pRoot << ": " << gap << pRoot->data << gap << "\n";
return gap + out.str() + gap;
}
else if(level > 1)
{
string left = printLevel(pRoot ? pRoot->pLeft : 0, level - 1, gap);
string right = printLevel(pRoot ? pRoot->pRight : 0, level - 1, gap);
//cout << ".. printLevel - " << pRoot << ": '" << left << "', '" << right << "'\n";
return left + " " + right;
}
else return
"";
}
void printLevelOrder(Node* pRoot, int depth)
{
for (int i = 1; i <= depth; i++)
{
string gap = "";
for (int j = 0; j < pow(2, depth - i) - 1; j++)
{
gap += " ";
}
string levelNodes = printLevel(pRoot, i, gap);
cout << levelNodes <<endl;
}
}
in the main
Node *pRoot = NULL;
int inputValue = 0;
char* gap = " ";
// Loop to read in input values
cout << "To build a BST enter positive integer values, followed by -1 \n";
while (inputValue != -1)
{
cin >> inputValue;
if( inputValue != -1)
{
insertIntoTree( pRoot, inputValue);
}
}
cout << endl;
// Display the tree
printLevel(pRoot, inputValue, gap);
printLevelOrder(pRoot, inputValue);

console based game crashes for no reason

i am trying to make the beginnings of a very basic game i am trying to have it just make the border of the game witch will be x's and they player is H but when i build and run it the program will immediately crash please help
#include <iostream>
using namespace std;
int cords[2];
string map[6][6] =
{
{"X","X","X","X","X","X"},
{"X"," "," "," "," ","X"},
{"X"," "," "," "," ","X"},
{"X"," ","H"," "," ","X"},
{"X"," "," "," "," ","X"},
{"X","X","X","X","X","X"}
};
string input;
bool running = true;
void tick(){
if(input == "w"){
cords[1]++;
}
if(input == "s"){
cords[1] = cords[1] - 1;
}
if(input == "a"){
cords[0] = cords[0] - 1;
}
if(input == "d"){
cords[0]++;
}
}
void render(){
cout << map[1][1] << map[1][2] << map[1][3] << map[1][4] << map[1][5] << map[1][6] <<endl
<< map[2][1] << map[2][2] << map[2][3] << map[2][4] << map[2][5] << map[2][6] <<endl
<< map[3][1] << map[3][2] << map[3][3] << map[3][4] << map[3][5] << map[3][6] <<endl
<< map[4][1] << map[4][2] << map[4][3] << map[4][4] << map[4][5] << map[4][6] <<endl
<< map[5][1] << map[5][2] << map[5][3] << map[5][4] << map[5][5] << map[5][6] <<endl
<< map[6][1] << map[6][2] << map[6][3] << map[6][4] << map[6][5] << map[6][6] <<endl;
}
void run(){
int ticks;
int frames;
tick();
render();
}
int main(){
while(running == true){
run();
cin>> input;
}
}
Both map indeces must be between 0 and 5: map[6][3] is incorrect, for example
If you had written a loop to print out your 2d array, you may have avoided the error:
const int matSize = 6;
for (int i = 0; i < matSize; ++i)
{
for (int j = 0; j < matSize; ++j )
cout << m[i][j];
cout << "\n";
}
You should change your board to use string or char, not arrays of single character strings:
std::string map[6] =
{
"XXXXXX",
"X X",
"X X",
"X H X",
"X X",
"XXXXXX",
};
char map_as_char_array[6][6] =
{
{'X','X','X','X','X','X'},
{'X',' ',' ',' ',' ','X'},
{'X',' ',' ',' ',' ','X'},
{'X',' ','H',' ',' ','X'},
{'X',' ',' ',' ',' ','X'},
{'X','X','X','X','X','X'}
};
The string data type is overkill for single letters.
Remember, the string type can be accessed as a single dimension array of characters.