//Here\'s what I have but my output is all sorts of messed up :( #include <iostr
ID: 3548602 • Letter: #
Question
//Here's what I have but my output is all sorts of messed up :(
#include <iostream>
using namespace std;
int GetUserChoice()
{
int menuChoice;
cout << "Please enter your menu choice: ";
cin >> menuChoice;
if (menuChoice >= 1 && menuChoice <= 7)
return menuChoice;
cout << "Invalid choice." << endl;
GetUserChoice();
}
char GetUserChar()
{
char symbol;
cout << "Enter desired symbol to fill in the Grid: ";
cin >> symbol;
return symbol;
}
void FillBorder(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (r == 0 || r == 14)
Grid[r][c] = userChar;
else
{
if (c == 0 || c == 14)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
}
void FillX(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if ((r + c == 14) || (r == c))
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
void FillPlus(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (c == 7 || r == 7)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
void BorderedFillX(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if ((r + c == 14) || (r == c))
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
if (r%14 == 0 || c%14 == 0)
Grid[r][c] = userChar;
}
void BorderedFillPlus(char Grid[15][15], char userChar)
{
int r,c;
for(r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (c == 7 || r == 7)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
if (r%14 == 0 || c%14 == 0)
Grid[r][c] = userChar;
}
void FillCheckerboard(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (r%2 == 0 && c%2 == 0)
Grid[r][c] = userChar;
else if (r%2 != 0 && c%2 != 0)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
void WriteGrid(char Array[15][15])
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
cout << Array[r][c];
cout << endl;
}
void ClearGrid(char Array[15][15])
{
int r, c;
//Fill the array with white space.
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
Array[r][c] = ' ';
}
int main()
{
int userChoice;
char g[15][15];
char s;
//Display menu.
cout << "--[MAIN MENU]--" << endl;
cout << "1. Box" << endl;
cout << "2. X" << endl;
cout << "3. Plus" << endl;
cout << "4. Bordered X" << endl;
cout << "5. Bordered Plus" << endl;
cout << "6. Checkerboard" << endl;
cout << "7. Exit" << endl;
while(1)
{
//Get user's menu choice.
userChoice = GetUserChoice();
//If the user enters 7 (EFillXit), end the loop.
if (userChoice == 7)
break;
//Get user's desired symbol.
s = GetUserChar();
//Call function ClearGrid.
ClearGrid(g);
switch(userChoice)
{
case 1: FillBorder(g, s);
WriteGrid(g);
break;
case 2: FillX(g, s);
WriteGrid(g);
break;
case 3: FillPlus(g, s);
WriteGrid(g);
break;
case 4: BorderedFillX(g, s);
WriteGrid(g);
break;
case 5: FillCheckerboard(g, s);
WriteGrid(g);
break;
}
}
system("pause");
return 0;
}
Problem: Write a program that uses a two-dimensional character grid (15 rows and 15 columns) and
generates various square patterns. The user can choose 1 of 6 different patterns and will input the
symbol used to fill in the pattern:
The program should contain (at a minimum) the following functions:
- GetUserChoice
Write a program that uses a two-dimensional character grid (15 rows and 15 columns) and generates various square patterns. The user can choose 1 of 6 different patterns and will input the symbol used to fill in the pattern: The program should contain (at a minimum) the following functions:- GetUserChoice - no parameters, returns an integer value representing the choice of the pattern the user wishes to generate. The function should validate the input before returning the value - GetUserChar - character parameter passed by reference - The function should ask the user for the desired symbol to fill in the grid and store it in the variable passed by reference - ClearGrid - array parameter, no return type. The function should write a space into each element of the array FillBorder - array parameter and character parameter (symbol to use in the grid), no return type. The function should fill in the outer borders of the grid with the symbol given FillPlus - array parameter and character parameter (symbol to use in the grid), no return type. The function should fill in the middle row and column of the grid with the symbol given FillX - array parameter and character parameter (symbol to use in the grid), no return type. The function should fill in the outer borders of the grid with the symbol given WriteGrid - array parameter, no return type. This function should write the grid to the screen The remainder of the code can be in the main function. The menu presented to the user should have an option allowing the user to exit the program. The user must be provided a menu to select the pattern. You must validate that the menu selection by the user is valid. You may assume that the user will enter the proper data type for the information. You must verify that what they entered is a valid option from the menu. All output will be to the screen. Helpful Hint: Those wanting to make the ClearGrid function work more effectively may want to take a look at the strcpy function in the cstring libraryExplanation / Answer
#include <iostream>
using namespace std;
int GetUserChoice(){
int menuChoice;
cout << "Please enter your menu choice: ";
cin >> menuChoice;
if (menuChoice >= 1 && menuChoice <= 7)
return menuChoice;
cout << "Invalid choice." << endl;
GetUserChoice();
}
char GetUserChar()
{
char symbol;
cout << "Enter desired symbol to fill in the Grid: ";
cin >> symbol;
return symbol;
}
void FillBorder(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (r == 0 || r == 14)
Grid[r][c] = userChar;
else
{
if (c == 0 || c == 14)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
}
void FillX(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if ((r + c == 14) || (r == c))
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
void FillPlus(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (c == 7 || r == 7)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
void BorderedFillX(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if ((r + c == 14) || (r == c))
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
if (r%14 == 0 || c%14 == 0)
Grid[r][c] = userChar;
}
void BorderedFillPlus(char Grid[15][15], char userChar)
{
int r,c;
for(r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (c == 7 || r == 7)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
if (r%14 == 0 || c%14 == 0)
Grid[r][c] = userChar;
}
void FillCheckerboard(char Grid[15][15], char userChar)
{
int r, c;
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
{
if (r%2 == 0 && c%2 == 0)
Grid[r][c] = userChar;
else if (r%2 != 0 && c%2 != 0)
Grid[r][c] = userChar;
else
Grid[r][c] = ' ';
}
}
void WriteGrid(char Array[15][15]){
int r, c;
for (r = 0; r < 15; r++){
for (c = 0; c < 15; c++){
cout << Array[r][c];
}
cout << endl;
}
}
void ClearGrid(char Array[15][15])
{
int r, c;
//Fill the array with white space.
for (r = 0; r < 15; r++)
for (c = 0; c < 15; c++)
Array[r][c] = ' ';
}
int main(){
int userChoice;
char g[15][15];
char s;
//Display menu.
cout << "--[MAIN MENU]--" << endl;
cout << "1. Box" << endl;
cout << "2. X" << endl;
cout << "3. Plus" << endl;
cout << "4. Bordered X" << endl;
cout << "5. Bordered Plus" << endl;
cout << "6. Checkerboard" << endl;
cout << "7. Exit" << endl;
while(1){
//Get user's menu choice.
userChoice = GetUserChoice();
//If the user enters 7 (EFillXit), end the loop.
if (userChoice == 7)
break;
//Get user's desired symbol.
s = GetUserChar();
//Call function ClearGrid.
ClearGrid(g);
switch(userChoice){
case 1: FillBorder(g, s);
WriteGrid(g);
break;
case 2: FillX(g, s);
WriteGrid(g);
break;
case 3: FillPlus(g, s);
WriteGrid(g);
break;
case 4: BorderedFillX(g, s);
WriteGrid(g);
break;
case 5: BorderedFillPlus(g, s);
WriteGrid(g);
break;
case 6: FillCheckerboard(g, s);
WriteGrid(g);
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.