Drunk Walker Arrays

Hi, I was given an assignment with arrays. The purpose of the program is to have a "drunk walker" walking around the array[10][10] randomly leaving a letter in each element that has been visited.

I am having several errors:

line 23 expected unqualified-id before "public"

line 23 expected `,' or `;' before "public"

In function `int north(int, int)':

line 140 invalid conversion from `char*' to `char'

the same error that accured in north() repeated in the other functions

Could you guys give me tips to help me solve my problem?

The assignment was due today at 12:00. It is now 12:20. I am now just doing this to learn.
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
 #include <iostream>
     #include <cstdlib>    
     using namespace std; 
    
 // prototypes
  int north(int,int);
  int east(int,int);
  int south(int,int);
  int west(int,int);
       
     char drunk[10][10]; 
     int row;
     int col;  
     int isFinished;
     char* alphaIndex;
     char alpha[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
     public alpha[]=alphaIndex;
   int main()
   {    

        int  canMove=0;
        int  randNum;
        int cantMove= 0 ; 
        
        for(int v=0;v<10;v++)
           for(int dd=0;dd<10;dd++)
              drunk[v][dd] = '.';
              
        for(int v=0;v<10;v++)
        {
           cout << "\n";
           for(int dd=0;dd<10;dd++)
              cout << drunk[v][dd] << " "; 
        }
        

      
    
      srand ( time(0));
      row=rand()% 10;
      col=rand()% 10;
      randNum=rand()%4;
      isFinished=0;
      
     while(isFinished!=1) 
     {   
          switch(randNum)
          {
               case 0: cout<<"north";
               
                   if(north(row, col) != 5)
                   {;}
                   else if(east(row, col) != 5)
                   {;}
                   else if(south(row, col) != 5)
                   {;}
                   else if(west(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
                   break;
               case 1:cout<<"east";

                   if(east(row, col) != 5)
                   {;}
                   else if(south(row, col) != 5)
                   {;}
                   else if(west(row, col) != 5)
                   {;}
                   else if(north(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
                  
                   break;
               case 2:cout<<"south";
                   
                   if(south(row, col) != 5)
                   {;}
                   else if(west(row, col) != 5)
                   {;}
                   else if(north(row, col) != 5)
                   {;}
                   else if(east(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
                  
                   break;
               case 3:cout<<"west";
                   
                   if(west(row, col) != 5)
                   {;}
                   else if(north(row, col) != 5)
                   {;}
                   if(east(row, col) != 5)
                   {;}
                   else if(south(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
         }//end switch        
   }// end while
     cout<<"    \n";
     cout<<"    \n";
     cout<<"    \n";
     cout<<"Walk Complete\n";   
     drunk[row][col]=row;
     cout<< drunk[row][col]<<"  ";
     
     system("pause");
  }//end main


 int north(int row,int col)
 {  cout<<"in north";
 
   if(row<0 || (row+1)>9 || col<0 || col>9 || alpha[27]==26)// bounds check
     {                              
     return 5;
     }
     else
     {   
          if(drunk[row+1][col]=='.')
          {
             drunk[++row][col]=alphaIndex++;
             return 0;         
           }// end nested if
           else
           {
            return 5;
           }
      }// end if  
}//end North Function             
     
     int south(int row,int col)
  { cout<<"in south";
       
  
     
     if(row<0||row>9||col<0||col>9)// bounds check
     {
           return 5;
     }
     else
      {   
     
          if(drunk[row-1][col]!='.')
          {
            if(alpha[27]==26)
            {
              return 5;
            }                  
         
            drunk[--row][col]=alphaIndex++;
            return 0;
          }// end nested if
      }// end if  
  }//end South Function             
     
 int east(int row,int col)
 { cout<<"in east";
  
   
     
     if(row<0||row>9||col<0||col>9)// bounds check
     {
       return 5;
     }     
     else
     {  
          if(drunk[row][col+1]!='.')
          {
             if(alpha[27]==26)
             {
                return 5;
             }                  
           drunk[row][++col]=alphaIndex++;
           return 0;
         }// end nested if
      }// end if  
}//end East Function             
     
 int west(int row,int col)
 { cout<<"in west";
     
     if(row<0||row>9||col<0||col>9)// bounds check
     {
        return 5; 
     }      
     else
     {     

          if(drunk[row][col-1]!='.')
          {
            if(alpha[27]==26)
            {
               return 5;
            }                       
            drunk[row][--col]=*alphaIndex++;
            return 0;
          }// end nested if
      }// end if  
 }//end West Function              


Last edited on
public alpha[]=alphaIndex;
public is not a data type.

alphaIndex is never initialized.

I'm confused. You correctly dereferenced alphaIndex here:
drunk[row][--col]=*alphaIndex++;
But not here:
drunk[row][++col]=alphaIndex++;
Last edited on
Helios, thanks for pointing out that I had not dereferenced alphaIndex.I had some help from a teacher and did not know to do that. alphaIndex was declared as a pointer, not as a variable.

I have worked out all but two errors with this. The first says that there is a ',' or a ';' expected after '=' on line 23. The second is on the same line. I says "expected constructor, destructor, or type conversion before '=' token" .

I have obviously not done something that I should have. What should I have done to correct the syntax?
Post the erroneous code.
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
   
       #include <iostream>
     #include <cstdlib>    
     using namespace std; 
    
 // prototypes
  int north(int,int);
  int east(int,int);
  int south(int,int);
  int west(int,int);
       
     char drunk[10][10]; 
     int row;
     int col;  
     int isFinished;
     char* alphaIndex ;
     char alpha[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
     alpha[]=alphaIndex;
   int main()
   {    

        int  canMove=0;
        int  randNum;
        int cantMove= 0 ; 
        
        for(int v=0;v<10;v++)
           for(int dd=0;dd<10;dd++)
              drunk[v][dd] = '.';
              
        for(int v=0;v<10;v++)
        {
           cout << "\n";
           for(int dd=0;dd<10;dd++)
              cout << drunk[v][dd] << " "; 
        }
        

      
    
      srand ( time(0));
      row=rand()% 10;
      col=rand()% 10;
      randNum=rand()%4;
      isFinished=0;
      
     while(isFinished!=1) 
     {   
          switch(randNum)
          {
               case 0: cout<<"north";
               
                   if(north(row, col) != 5)
                   {;}
                   else if(east(row, col) != 5)
                   {;}
                   else if(south(row, col) != 5)
                   {;}
                   else if(west(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
                   break;
               case 1:cout<<"east";

                   if(east(row, col) != 5)
                   {;}
                   else if(south(row, col) != 5)
                   {;}
                   else if(west(row, col) != 5)
                   {;}
                   else if(north(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
                  
                   break;
               case 2:cout<<"south";
                   
                   if(south(row, col) != 5)
                   {;}
                   else if(west(row, col) != 5)
                   {;}
                   else if(north(row, col) != 5)
                   {;}
                   else if(east(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
                  
                   break;
               case 3:cout<<"west";
                   
                   if(west(row, col) != 5)
                   {;}
                   else if(north(row, col) != 5)
                   {;}
                   if(east(row, col) != 5)
                   {;}
                   else if(south(row, col) != 5)
                   {;}
                   else
                   {
                       isFinished = 1;
                   }
         }//end switch        
   }// end while
     cout<<"    \n";
     cout<<"    \n";
     cout<<"    \n";
     cout<<"Walk Complete\n";   
     drunk[row][col]=row;
     cout<< drunk[row][col]<<"  ";
     
     system("pause");
  }//end main


 int north(int row,int col)
 {  cout<<"in north";
 
   if(row<0 || (row+1)>9 || col<0 || col>9 || alpha[27]==26)// bounds check
     {                              
     return 5;
     }
     else
     {   
          if(drunk[row+1][col]=='.')
          {
             drunk[++row][col]=*alphaIndex++;
             return 0;         
           }// end nested if
           else
           {
            return 5;
           }
      }// end if  
}//end North Function             
     
     int south(int row,int col)
  { cout<<"in south";
       
  
     
     if(row<0||row>9||col<0||col>9)// bounds check
     {
           return 5;
     }
     else
      {   
     
          if(drunk[row-1][col]!='.')
          {
            if(alpha[27]==26)
            {
              return 5;
            }                  
         
            drunk[--row][col]=*alphaIndex++;
            return 0;
          }// end nested if
      }// end if  
  }//end South Function             
     
 int east(int row,int col)
 { cout<<"in east";
  
   
     
     if(row<0||row>9||col<0||col>9)// bounds check
     {
       return 5;
     }     
     else
     {  
          if(drunk[row][col+1]!='.')
          {
             if(alpha[27]==26)
             {
                return 5;
             }                  
           drunk[row][++col]=*alphaIndex++;
           return 0;
         }// end nested if
      }// end if  
}//end East Function             
     
 int west(int row,int col)
 { cout<<"in west";
     
     if(row<0||row>9||col<0||col>9)// bounds check
     {
        return 5; 
     }      
     else
     {     

          if(drunk[row][col-1]!='.')
          {
            if(alpha[27]==26)
            {
               return 5;
            }                       
            drunk[row][--col]=*alphaIndex++;
            return 0;
          }// end nested if
      }// end if  
 }//end West Function              



      
Last edited on
Line 17:
error: expected constructor, destructor, or type conversion before '=' token

What are you trying to do here?
If I comment that line, it compiles without errors.
The idea of the program is to have a "drunk walker" walking from element to element inside the array leaving a letter where it has been. Line 17 is a declaration of the character array that holds the letters that will be left behind by the walker.
Ok, Some thing's to point out for you.

Line 12: char drunk[10][10]; You need to init the array, or it's going to have crap in it. You should use char drunk[10][10] = {0} to set all elements to null.

Lines 26-35: No longer needed because you've init'd the array to null now.

You've overall made the program over-complicated. I have re-factored your code slightly. Take a look at this and try to understand what I am doing. Then use it, or adapt yours to be similar.

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
#include <iostream>
#include <cstdlib>
using namespace std;

// prototypes
void moveNorth();

#define MAX_ROW 10
#define MAX_COL 10

char drunk[MAX_COL][MAX_ROW]  = {0};
int row             = 0;
int col             = 0;
int charIndex       = 0;
char alpha[27]      ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main() {
  srand ( time(0));
  row=rand()% 10;
  col=rand()% 10;
  
  while (true) {
    int direction = rand() % 4;

    switch(direction) {
    case 0: // Move North
      cout << "Moving North" << endl;
      moveNorth();
      break;
    default:
      cout << "Code not complete" << endl;
    }

    drunk[col][row] = alpha[charIndex];
    charIndex++;
    
    if (charIndex == 26)
      break;
  }

  cout << "Walk Finished" << endl;
  system("pause");
}//end main

void moveNorth() {
  if (col >= MAX_COL) // Can't Move North.
    return;
  col++;
}

Last edited on
Topic archived. No new replies allowed.