I am new to Stack overflow and I am wondering if I can get help regarding my C++ code.
I want to fill my 2D array with the users input, for example:
- Please Enter 16 characters: (abcdabcdabcdabcd)
Outputs a 4x4 grid that is filled with user's input in this case its (abcdabcdabcdabcd).
a b c d
a b c d
a b c d
a b c d
Here is my progress up till now:
#include <iostream>
#include <stdlib.h>
using namespace std;
//Constants
const int SM_GRID = 3;
const int LG_GRID = 4;
const char FILL_1 = 'X';
const char FILL_2 = 'O';
const int FORWARD = 1;
const int REVERSE = 2;
const int MAXLEN = 128;
//displays an overview of the program
void displayOverview();
//fills a small array with x's and o's
void fillSmallArray(char grid[][SM_GRID]);
//prompts the user for characters, and fills a 4x4 array with them
//either fills it from top to bottom, left to right (if direction is 1)
//or bottom to top, right to left (if direction is 2)
void fillLgArray(char grid[][LG_GRID], int direction);
//outputs the small array
void outputSmallArray(char grid[][SM_GRID]);
//outputs the large array
void outputLgArray(char grid[][LG_GRID]);
//prompts for a command, and calls the appropriate function based on it
//returns true if command was valid
bool doCommand(char grid[][LG_GRID]);
int main(){
//Overview of the program
displayOverview();
//set up arrays
char myGrid[SM_GRID][SM_GRID];
fillSmallArray (myGrid);
outputSmallArray(myGrid);
//declare a large array
//declare variable: Play or Not?
//As long as user wants to play
//call doCommand
char myGrid1[LG_GRID][LG_GRID];
doCommand (myGrid1);
//fill the small array and output it
//fill the large array and output it, as many times as the user wants
//1 for forward, 2 for reverse, any other character ends the game
return 0;
}
//displays an overview of the program
void displayOverview(){
cout << "Welcome to my grid-o-matic game !! :)\n"
<< "Here is your small grid: \n";
}
//fills a small array with x's and o's
void fillSmallArray(char grid[][SM_GRID]){
//logic: if both row and col are even, or both odd, put in a X
//otherwise put in a O
//loop through the grid put in a X or O as above
for (int row = 0; row < SM_GRID; row++){
for(int col = 0; col < SM_GRID; col++){
if((row %2) == (col%2)){
grid [row][col] = FILL_1;
}else{
grid [row][col] = FILL_2;
}//if else
}//inner for
}//outer for
}//function
//prompts the user for characters, and fills a 4x4 array with them
//either fills it from top to bottom, left to right (if direction is 1)
//or bottom to top, right to left (if direction is 2)
void fillLgArray(char grid[][LG_GRID], int direction){
string userInput;
cout << "Please enter 16 characters!\n";
cin >> userInput;
if(userInput.length() > 16){
userInput = userInput.substr(0, 16);
}
cout << "You entered " << userInput << endl;
for (int row = 0; row < LG_GRID; row++){
for (int col = 0; col < LG_GRID; row++){
grid [row][col] = userInput.at(col+row*LG_GRID);
}
}
}//Function
//outputs the small array
void outputSmallArray(char grid[][SM_GRID]){
for (int row=0;row <SM_GRID;row++){
for(int col=0;col <SM_GRID;col++){
cout << grid[row][col]<<" ";
}
cout << endl;
}
}
//outputs the large array
void outputLgArray(char grid[][LG_GRID]){
for (int row=0;row <LG_GRID;row++){
for(int col=0;col <LG_GRID;col++){
cout << grid[row][col]<<" ";
}
cout << endl;
}
}
//prompts for a command, and calls the appropriate function based on it
//returns true if command was valid
bool doCommand(char grid[][LG_GRID]){
bool valid = true;
char input [MAXLEN];
int command;
cout << "Please enter 1 for FORWARDS or 2 for reverse!\n";
cin.getline(input,MAXLEN);
command = atoi(input);
switch (command){
case FORWARD:
cout << "Going Forwards !!!\n";
fillLgArray(grid,command);
outputLgArray(grid);
break;
case REVERSE:
cout << "Going backwards !!\n";
fillLgArray(grid, command);
outputLgArray(grid);
break;
default:
return false;
}
return valid;
}