ASAP, please

Hi, i am trying to make a program that writes in a square board(which is an array bool[][]) given the instructions saved in a string[]. Any ideas?
How could it be done? Please I need some help here.

Here is what I got, the board before the instructions.
1
2
3
4
5
6
7
8
9
 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;
}

And what I want is this to use the string intruc:
 
std::string intruc={"up","up","down","right","right","chalk_up","chalk_down","left","left"}

So I need code that can read intruc(iteration) and relate it with board[][] so that if intruc[c]=="up" then boar[n][m]=board[n+1][m]. I tried this but it doesnt work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//drawn is a previously declared bool
for(int c=0;c<=words;c++){
    instruc[c];
    board[a][r];
    if(instruc[c]=="left"){
       a=a+1;
    }
    if(instruc[c]=="right"){
        a=a-1;
    }
    if(instruc[c]=="down"){
        r=r-1;
        return r;
    }
    if(instruc[c]=="up"){
        r=r+1;
    }
    if(instruc[c]=="true"){
        drawn=true;
    }
    if(instruc[c]=="false"){
        drawn=false;
    }
}

What are you trying to do with lines 3 and 4? Because, right now, those statements do nothing.

What are a and r supposed to represent? Your code will be easier for you and us to understand if you use meaningful variable names.

Why does your function return if the instruction is "down", but not if there are other instructions?

Last edited on
Sorry, I have been trying and changing this for too long. The return r is a mistake, and I change the names of a and r.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(int c=0;c<=words;c++){
    if(instruc[c]=="left"){
       row=row+1;
    }
    if(instruc[c]=="right"){
        row=row-1;
    }
    if(instruc[c]=="down"){
        column=column-1;
    }
    if(instruc[c]=="up"){
        column=column+1;
    }
    if(instruc[c]=="true"){
        drawn=true;
    }
    if(instruc[c]=="false"){
        drawn=false;
    }
}

You are right lines 3 and 4 did nothing, but still the board[][] isnt the way i want it.

Here is all the code I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<fstream>
int main(){
int N=20;
bool board[N][N];
//All elements of board are false
for(int row=N;row>=0;row--){
    for(int column=N;column>=0;column--){
        board[row][column]=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 n=0;n<=words;n++){
            F >> instruc[n];
    }
F.close();
//Shows all the commands in the screen
bool drawn;
for(int n=0;n<=words;n++){
    std::cout<<instruc[n]<<"\t";
}
int row=21;
int column=21;
//Use the commands for the array.
//Don't work, this is my Problem.
for(int c=0;c<=words;c++){
    if(instruc[c]=="left"){
       row=row+1;
    }
    if(instruc[c]=="right"){
        row=row-1;
    }
    if(instruc[c]=="down"){
        column=column-1;
    }
    if(instruc[c]=="up"){
        column=column+1;
    }
    if(instruc[c]=="true"){
        drawn=true;
    }
    if(instruc[c]=="false"){
        drawn=false;
    }
}
//Shows the modified board
//The board is not how it should be
for(row=0;row<=41;row++){
for(column=0;column<=41;column++){
    std::cout<<board[row][column]<<"\t";
}
std::cout<<"\n";
}
std::cout<<" The program has ended";

return 0;
}
Last edited on
You don't ever do anything to actually change the values stored in board. You initialise the values in lines 7 - 12, but you never then change them anywhere.
You are right. Thank you for your help. Im going to put this problem aside for a while and look on manuals and the forum for more basic stuff.
Topic archived. No new replies allowed.