I want to use a string[] to change some of the values of a bool[][] 2D array and "move" in the same array.
So I have all the comands for those instructions in a file this way:
true down left
left false right
right true right
and I copy that word for word to a
string instruc[]
andI want to use this string to "move" in the array[n][m] with the first comand of instruc being aplplied to board[10][10] and then moving in the array acording with the instruction as if it was a board. Here is the code with the failing part.
#include<iostream>
#include<fstream>
int main(){
int N=20;
bool board[N][N];
//All elements of board are false
for(int i=N;i>=0;i--){
for(int a=N;a>=0;a--){
board[i][a]=false;
}
std::cout<<std::endl;
}
// Read the file and gives the number of commands.
std::ifstream w("in.txt");
int words = 0;
std::string word;
while(w >> word){
words++;
}
w.close();
std::cout<<"Number of comands: "<<words<<std::endl;
//Read the file and puts all the commands in the string instruc[]
std::ifstream F;
F.open("in.txt");
std::string instruc[words];
for(int i=0;i<=words;i++){
F >> instruc[i];
}
F.close();
//Shows all the commands in the screen
bool pinta;
for(int c=0;c<=words;c++){
std::cout<<instruc[c]<<"\t";
}
int a=21;
int r=21;
//Use the commands for the array.
//Don't work, this is my Problem.
for(int c=0;c<=words;c++){
instruc[c];
board[a][r];
if(instruc[c]=="izq"){
a=a+1;
}
if(instruc[c]=="dcha"){
a=a-1;
}
if(instruc[c]=="aba"){
r=r-1;
return r;
}
if(instruc[c]=="arr"){
r=r+1;
}
if(instruc[c]=="verd"){
pinta=true;
}
if(instruc[c]=="fals"){
pinta=false;
}
}
//Shows the modified board
//The board is not how it should be
for(a=0;a<=41;a++){
for(r=0;r<=41;r++){
std::cout<<board[a][r]<<"\t";
}
std::cout<<"\n";
}
std::cout<<" The program has ended";
return 0;
}