Hello. I am new to C++ and I am having a hard time getting the correct output. My program takes a 2D array and places characters in it by stepping from spot to spot. I only included parts of the program where I believe there might be something wrong:
void print (char board [size][size])
{
int row, col;
for (row = 0; row < size; row++)
{
cout << "\n" << endl;
for (col = 0; col < size; col++)
cout << setw (4) << board [row][col];
}
}
void doStep (char board [size][size], int step, char ch)
{
//fix function so that it literally steps through
//for eaxmple, y steps to every 3rd spot in array
//and starts in new row when end of column is reached
//until row/column > size
int row;
int col;
cout << "\nEnter a character: ";
cin >> ch;
for (row = 0; row < size; row+=step)
for (col = step; col< size; col+=step)
{
board[row][col]=ch;
}
}
The output is printing columns correctly. However, when it reaches the end of the first row (row[0]), it skips two rows (leaving all ".") before outputting ch again in row[3].
I am not sure which if my doStep, or print function is incorrect...or both?
The output should resemble something like this:
The board is:
...Y..Y..Y
..Y..Y..Y.
.Y..Y..Y..
Y..Y..Y..Y
..Y..Y..Y.
.Y..Y..Y..
Y..Y..Y..Y
..Y..Y..Y.
.Y..Y..Y..
Y..Y..Y..Y
Any help would be greatly appreciated. Please let me know if more of the program is needed to find my error. Thank you.
The first thing that needs fixed is the function definitions. char board [size][size] will not work because size needs to be a constant number e.g. 5 or 10. The actual size of the array needs to be known at compile time.
The code in the functions looks OK for now. I will have to work up a way to test it though.
Hello Handy Andy.
I did declare my function definitions before main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
usingnamespace std;
constint size = 10;
//sets all spots in the board = to char .
void init_board (char board [size][size]);
//prints the array in 2d format
void print (char board [size][size]);
//start @ first element in the array and
//repeatedly steps forward by step
//Places ch at each spot to be stepped to
void doStep (char board [size][size], int step, char ch);
I am wondering if I am not looping correctly in the doStep function? Lets say I ask the user to Enter a step: 4
Enter a character: y
I think you may have misunderstood me the function declaration i.e. fundtion prototype, forward declaration ends with a semi-colon. The function definition does not end with the semi-colon and contains the code of what to do. In the function definition, as I read somewhere, you can not use a variable to define the size of an array it must be a constant like char board[5][10]. Then I tried defining a const variable at the very start after the includes and then used a variable in the function definition it worked.
Now in the doStep function change the first for loop from row += step to row++ and then it will print on every line.