I'm trying to write a program where the user selects one of six shapes to be made with whatever character they desire but I've ran into a bit of the problem with the code.
I've successfully made all of the shapes with arrays and loops but I cannot figure out how to display them correctly...
Help me please?
Thanks in advance.
//
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
//Prototypes
void Menu();
int GetUserChoice();
void GetUserChar(char userchar);
void ClearGrid(char shape[15][15]);
void FillBorder(char shape[15][15],char userchar);
void FillPlus(char shape[15][15], char userchar);
void FillX(char shape[15][15],char userchar);
void FillCheckerBoard(char shape[15][15], char userchar);
void WriteGrid(char shape[15][15]); //arr
void Output();
///////////////////////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
char userchar;
char shape[15][15];
ClearGrid(shape[15][15]);
Menu();
GetUserChoice();
GetUserChar();
WriteGrid(GetUserChoice(), &userchar);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void Menu()
{
cout << "Please select the number of the shape you would like me to create for you.\n";
cout << "--------------------------------------------------------------------------\n";
cout << "1. Box\n""2. X\n""3. Plus\n""4. Bordered X\n""5. Bordered Plus\n""6. Checkerboard\n";
}
///////////////////////////////////////////////////////////////////////////////////////////////////
int GetUserChoice()
{
int choice;
cin >> choice;
while(choice < 0 || choice > 6)
cout << "I'm sorry." << choice << "is not a valid option\nPlease try again.";
return choice;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetUserChar(char &userchar)
{
cout << "Enter the character you wish to create your shape of choice with!\n";
cin >> userchar;
return userchar;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ClearGrid(char shape[15][15])
{
//Make every coordinate in array contain a space
for(int x = 0; x < 15; x++)
for (int y = 0; y < 15; y++)
shape[x][y] = ' ';
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void FillBorder(char shape[15][15], char &userchar) //array and char user picked
{
//fill top row and bottom row
for(int x = 0; x < 15; x = x + 14)
for(int y = 0; y < 15; y++)
shape[x][y] = userchar; //char that user picked
//fill left column and right column (Except the corners)
for(int x = 1; x < 15 - 1; x++)
for(int y = 0; y < 15; y = y + 14)
shape[x][y] = userchar; // char user picks
Output();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FillPlus(char shape[15][15], char &userchar) //array and char user picked
{
//fills middle row (row 7)
for(int y = 0; y < 15; y++)
shape[7][y] = userchar; // char user picked
//fills middle column (col 7)
for (int x = 0; x < 15; x++)
shape[x][7] = userchar; // char user picked
Output();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FillX(char shape[15][15],char &userchar)
{
int z = 0;
for(int i = 0; i<15; i++){
shape[i][z] = userchar;
z++;
}
int y = 0;
for(int i = 14; i>-1; i--){
shape[i][y] = userchar;
y++;
}
Output();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FillCheckerBoard(char shape[15][15],char &userchar)
{
//Every other column with even rows
for(int x = 0; x < 15; x = x + 2)
for(int y = 0; y < 15; y = y + 2)
shape[x][y] = userchar; //char user picked
//Every other column with odd rows
for(int x = 1; x < 15; x = x + 2)
for(int y = 1; y <15; y = y + 2)
shape[x][y] = userchar; //char user picked
Output();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void WriteGrid(char shape[15][15])
{
switch(GetUserChoice())
{
//In the case user selects box
case 1:
FillBorder(userchar);
for(int i = 0; i<15; i++){
cout<<endl;
for(int j = 0; j<15; j++){
cout<<shape[j][i];
}
}
break;
//In the case user selects X
case 2:
FillX(userchar);
break;
//In the case user selects plus
case 3:
FillPlus(userchar);
break;
//In the case user selects bordered x
case 4:
FillBorder(userchar);
FillX(userchar);
break;
//In the case user selects bordered plus
case 5:
FillBorder(userchar);
FillPlus(userchar);
break;
//In the case user selects checkerboard
case 6:
FillCheckerBoard(userchar);
break;
}
}
void Output() {
for(int i = 0; i<15; i++){
cout<<endl;
for(int j = 0; j<15; j++){
cout<<shape[j][i];
}
}
}
I'm not a fan of just giving answers to problems, so I won't give you code but you need to look at two things:
1) You are attempting to use both userchar and shape when you only have passed one of the two variables in certain functions.
2) You aren't passing both characters in your functions to produce the shape, but you call for your function to have both, so when you only pass one of the two arguments, your program looks for a function that only takes one argument.