I'm writing a simple program to generate a box with a user-defined sidelength and border/fill characters. Everything is working as I want it to, except when it prints the box to the terminal, it produces a strange character I cannot find anywhere. I feel like if I know what it is, I might be able to fix it. My header file is here:
#ifndef Box_h
#define Box_h
class Box
{
private:
int pSidelength;
char pBorder;
char pFill;
public:
Box(int pSidelength, char pBorder = '#', char pFill = '*');
int Sidelength;
char Border;
char Fill;
int Area();
int Perimeter();
int GetSize();
int Grow();
int Shrink();
char SetBorder();
char SetFill();
void Draw();
void Summary();
};
#endif Box_h
My source for the class is:
#include <iostream>
#include "box.h"
#include <iomanip>
using namespace std;
Box::Box(int pSidelength, char pBorder, char pFill)
{
if (pSidelength < 1)
{
Sidelength = 1;
}
else if (pSidelength > 39)
{
Sidelength = 39;
}
else
{
Sidelength = pSidelength;
}
if (pBorder != '#')
{
SetBorder();
}
if (pFill != '*')
{
SetFill();
}
}
int main(void)
{
Box MyBox1(3,'#','*');
Box MyBox2(7, '^', '*');
Box MyBox3(10, '$', '%');
MyBox1.Grow();
MyBox2.Shrink();
MyBox1.Summary();
MyBox2.Summary();
MyBox3.Summary();
return 0;
}
int Box::Shrink()
{
if (Sidelength == 1)
{
Sidelength = Sidelength;
}
else
{
Sidelength = Sidelength - 1;
}
return Sidelength;
}
int Box::Grow()
{
if (Sidelength == 39)
{
Sidelength = Sidelength;
}
else
{
Sidelength = Sidelength + 1;
}
return Sidelength;
}
char Box::SetFill()
{
Fill = pFill;
return Fill;
}
char Box::SetBorder()
{
Border = pBorder;
return Border;
}
int Box::Area()
{
int area = (Sidelength)*(Sidelength);
return area;
}
int Box::Perimeter()
{
int perimeter = 4 * (Sidelength);
return perimeter;
}
int Box::GetSize()
{
int size = Sidelength;
return size;
}
void Box::Draw()
{
int j = 1;
int k = 1;
if (Sidelength == 1 || Sidelength == 2)
{
for (int i = 1; i <= Sidelength; i++)
{
while (j <= Sidelength)
{
cout << setw(2) << Border;
j++;
}
j = 1;
}
cout << endl;
}
else
{
for (int i = 1; i <= Sidelength; i++)
{
if (i == 1 || i == Sidelength)
{
while (k <= Sidelength)
{
cout << setw(2) << Border;
k++;
}
cout << endl;
k = 1;
}
else
{
while (j <= Sidelength)
{
if (j == 1 || j == Sidelength)
{
cout << setw(2) << Border;
}
else
{
cout << setw(2) << Fill;
}
j++;
}
cout << endl;
j = 1;
}
}
cout << endl;
}
}
void Box::Summary()
{
cout << "The Sidelength of the box is: " << Box::GetSize() << endl;
cout << "The Perimeter of the box is: " << Box::Perimeter() << endl;
cout << "The Area of the box is: " << Box::Area() << endl;
Box::Draw();
}
The program has a default character associated with Border/Fill, as specified in the header file. When run, it produces this:
What character is that, and any ideas on why it would be appearing in the first place?
The character is random, and could in theory be different every time you run the program.
It's coming from the pBorder member, which is never set to anything.
You got confused with same name of variables.
Box::Box(int pSidelength, char pBorder, char pFill)
{
if (pSidelength < 1)
{
Sidelength = 1;
}
else if (pSidelength > 39)
{
Sidelength = 39;
}
else
{
Sidelength = pSidelength;
}
if (pBorder != ' ') //Here pBorder has '*' but this is local
// pBorder to this Function
{
SetBorder();
}
if (pFill != ' ')
{
SetFill();
}
}
And When you call SetBorder();
It makes Border as pBorder as that was declared in the class which is still unintialized.
char Box::SetBorder()
{
Border = pBorder; //This pBorder is not initialized
return Border;
}
Solution 1
Dont use Function
if (pBorder != ' ')
{
Border = pBorder;
}
Solution 2
Pass pBorder
if (pBorder != ' ')
{
SetBorder(pBorder);
}
char Box::SetBorder(char pBorder)
{
Border = pBorder; //This pBorder is not initialized
return Border;
}
Related
I am trying to create a c++ unbeatable tic tac toe AI. after watching several videos on the topic i thought I had it all figured out. An error pops up on the screen saying "Expression: vector subscript out of range". I believe the error is coming from the availableMoves() function. however I do not know why.
The game itself works fine. any help would be appreciated.
#include <iostream>
#include <vector>
#include <ctime>
bool in(std::vector<int> v, int element)
{
for (int i = 0; i < v.size(); i++)
{
if (element == v[i])
{
return true;
}
}
return false;
}
class Board
{
private:
char board[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };
public:
void displayBoard()
{
std::cout << "___________________" << std::endl;
for (int i = 0; i < 3; i++)
{
std::cout << "| ";
for (int j = 0; j < 3; j++)
{
std::cout << board[i][j] << " | ";
}
std::cout << std::endl;
}
std::cout << "___________________" << std::endl;
}
std::vector<int> availableMoves()
{
std::vector<int> moves;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != 'X' && board[i][j] != 'O')
{
moves.push_back(i * 3 + j);
}
}
}
return moves;
}
void move(int choice, char mark)
{
int y = choice / 3;
int x = choice - y * 3;
board[y][x] = mark;
}
void revert(int choice)
{
int y = choice / 3;
int x = choice - y * 3;
board[y][x] = (char)choice + 48;
}
int checkWin()
{
for (int i = 0; i < 3; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
if (board[i][0] == 'X')
{
return 1;
}
else if (board[i][0] == 'O')
{
return -1;
}
}
}
for (int i = 0; i < 3; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
if (board[0][i] == 'X')
{
return 1;
}
else if (board[0][i] == 'O')
{
return -1;
}
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
{
if (board[0][0] == 'X')
{
return 1;
}
else if (board[0][0] == 'O')
{
return -1;
}
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
if (board[0][2] == 'X')
{
return 1;
}
else if (board[0][2] == 'O')
{
return -1;
}
}
return 0;
}
int evaluate()
{
return (checkWin() * -1) * (availableMoves().size() + 1);
}
Board& operator=(Board& b)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = b.board[i][j];
}
}
return (*this);
}
};
class TicTacToe
{
private:
Board board;
int turn;
int searches = 0;
public:
TicTacToe()
{
std::srand(time(0));
turn = std::rand() % 2;
}
int minimax(int depth, Board curBoard, bool is_max)
{
searches++;
if (depth == 0 || curBoard.checkWin() != 0)
{
return board.evaluate();
}
if (is_max)
{
int max_eval = -2147483647;
for (int i = 0; i < curBoard.availableMoves().size(); i++)
{
curBoard.move(curBoard.availableMoves()[i], 'O');
depth -= 1;
int eval = minimax(depth, curBoard, false);
curBoard.revert(curBoard.availableMoves()[i]);
if (eval > max_eval)
{
max_eval = eval;
}
}
return max_eval;
}
if (!is_max)
{
int min_eval = 2147483647;
for (int i = 0; i < curBoard.availableMoves().size(); i++)
{
curBoard.move(curBoard.availableMoves()[i], 'X');
depth -= 1;
int eval = minimax(depth, curBoard, true);
curBoard.revert(curBoard.availableMoves()[i]);
if (eval < min_eval)
{
min_eval = eval;
}
}
return min_eval;
}
}
void game()
{
while (board.checkWin() == 0 && board.availableMoves().size() != 0)
{
board.displayBoard();
if (turn % 2 == 0)
{
std::cout << std::endl;
int choice;
std::cout << "Enter Your Move: ";
std::cin >> choice;
choice -= 1;
while (!in(board.availableMoves(), choice))
{
std::cout << "Enter A Valid Move: ";
std::cin >> choice;
}
board.move(choice, 'X');
std::cout << std::endl;
turn++;
}
board.displayBoard();
if (board.checkWin() != 0)
{
break;
}
if (turn % 2 == 1)
{
int ai = minimax(9 - (turn % 2), board, true);
std::cout << searches;
std::cin.get();
turn++;
}
}
if (board.checkWin() == 1)
{
std::cout << "You Won" << std::endl;
}
else if (board.checkWin() == -1)
{
std::cout << "You Lost" << std::endl;
}
else
{
std::cout << "Tie" << std::endl;
}
std::cout << "Would You Like To Play Again Y/N: ";
char playAgain;
std::cin >> playAgain;
if (playAgain == 'Y')
{
Board newBoard;
board = newBoard;
game();
}
}
};
int main()
{
TicTacToe ticTacToe;
ticTacToe.game();
}
Do you know how to debug? If not, you should definitely learn this, it's pretty helpful. But here's some things I found out.
The problem is not in availableMoves(), but in minimax(), more precisely in line 215, where the program calls curBoard. revert(curBoard. availableMoves()[i]).
void revert(int choice)
{
int y = choice / 3;
int x = choice - y * 3;
board[y][x] = (char)choice + 48;
}
for (int i = 0; i < curBoard.availableMoves().size(); i++)
{
curBoard.move(curBoard.availableMoves()[i], 'X');
depth -= 1;
int eval = minimax(depth, curBoard, true);
curBoard.revert(curBoard.availableMoves()[i]);
if (eval < min_eval)
{
min_eval = eval;
}
}
The error happens in the function revert, but I am not sure why. Maybe availableMoves also returns something wrong. Variable i is permanently 0 in the for-loop. So it is possible that there is something wrong at position 0 of the vector moves, which revert cannot handle. Try debugging yourself, maybe you'll find the problem.
My program is exiting without iterating. It automatically goes to "YOU WON". Without the champion function the program runs fine. Its probably some obvious error Im missing. If anyone could please I would greatly appreciate it.
#include <iostream>
#include <string>
#define GRID_SIZE 3
class TicTacToe {
private:
char map[GRID_SIZE][GRID_SIZE];
public:
void champion() {
const char *possiblities[8]{
"123"
"456"
"789"
"147"
"159"
"258"
"369"
"753"
};
for (int i = 0; i < 8; i++) {
bool winner = true;
char previous_pos = '0';
const char *possible_moves = possiblities[i];
for (int index = 0; index < GRID_SIZE; index++) {
char character = possible_moves[i];
int entered_num = character - '0';
int grid_space = entered_num - 1;
int row = index / GRID_SIZE;
int col = index % GRID_SIZE;
char grid_coordinate = map[row][col];
if (previous_pos == '0') {
previous_pos = grid_coordinate;
} else if
(previous_pos == grid_coordinate) {
continue;
} else {
winner = false;
break;
}
}
if (winner = true) {
std::cout << "YOU WON" << std::endl;
exit(0);
break;
}
}
}
void playgame() {
std::string input;
while (true) {
std::cout << "Go player one" << std::endl;
getline(std::cin, input);
if (input != " ") {
char entered = input.c_str()[0];
if (entered >= '1' && entered <= '9') {
int entered_num = entered - '0';
int index = entered_num - 1;
int row = index / 3;
int col = index % 3;
char grid_position = map[row][col];
if (grid_position == 'X' || grid_position == 'O') {
std::cout << "Space taken. Try again" << std::endl;
} else {
map[row][col] = (char) 'X';
break;
}
} else {
std::cout << "Only numbers 1 - 9" << std::endl;
}
} else {
std::cout << "Have to enter something, try again" << std::endl;
}
}
}
void generateGrid() {
int number = 1;
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
map[x][y] = std::to_string(number).c_str()[0];
number += 1;
}
}
}
void tictacToeMap() {
std::cout << std::endl;
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
std::printf(" %c ", map[x][y]);
}
std::cout << std::endl;
}
}
TicTacToe() {
generateGrid();
while (true) {
tictacToeMap();
playgame();
champion();
}
}
};
int main() {
TicTacToe tic;
tic.playgame();
return 0;
}
The problem is here:
if (winner = true) {
std::cout << "YOU WON" << std::endl;
exit(0);
break;
}
You probably meant:
if (winner == true) {
std::cout << "YOU WON" << std::endl;
exit(0);
break;
}
First, do what Max Meijer said, by replacing if(winner = true) with if(winner == true). But the program is still broken. The problem is that in your string array, you are not separating each string with a comma, so when my debugger hits const char *possible_moves, it ends up just assigning the entire array concatenated together. So just separate each string in the possibilities array with a comma, like so:
const char *possiblities[8]{
"123",
"456",
"789",
"147",
"159",
"258",
"369",
"753"
};
I want the program to print "You win" when any of the instances in the champion() function are given. It only shows a winner when "123" is inputted. Whenever three X's are displayed anywhere else the program continues. For instance if three X's are given diagonally the program will still continue. Novice programmer so any criticism is greatly appreciated.
class TicTacToe {
private:
char map[GRID_SIZE][GRID_SIZE];
public:
void computers_turn() {
while (true) {
int choice = (rand() % 9) + 1;
int row = choice / 3;
int col = choice % 3;
char grid_position = map[row][col];
if (grid_position == 'X' || grid_position == 'O') {
std::cout << "Space taken. Try again" << std::endl;
} else {
map[row][col] = (char)'O';
break;
}
}
}
void champion() {
const char* possiblities[8] = {"123", "456", "789", "147",
"258", "369", "159", "753"};
for (int i = 0; i < 8; i++) {
char previous_pos = '0';
bool winner = true;
const char* possible_moves = possiblities[i];
for (int index = 0; index < GRID_SIZE; index++) {
char character = possible_moves[i];
int entered_num = character - '0';
int grid_space = entered_num - '1';
int row = index / GRID_SIZE;
int col = index % GRID_SIZE;
char grid_coordinate = map[row][col];
if (previous_pos == '0') {
previous_pos = grid_coordinate;
} else if (previous_pos == grid_coordinate) {
continue;
} else {
winner = false;
break;
}
}
if (winner) {
puts("You win");
exit(0);
break;
}
}
}
void playgame() {
std::string input;
while (true) {
std::cout << "Go player one" << std::endl;
getline(std::cin, input);
if (input != " ") {
char entered = input.c_str()[0];
if (entered >= '1' && entered <= '9') {
int entered_num = entered - '0';
int index = entered_num - 1;
int row = index / 3;
int col = index % 3;
char grid_position = map[row][col];
if (grid_position == 'X' || grid_position == 'O') {
std::cout << "Space taken. Try again" << std::endl;
} else {
map[row][col] = (char)'X';
break;
}
} else {
std::cout << "Only numbers 1 - 9" << std::endl;
}
} else {
std::cout << "Have to enter something, try again" << std::endl;
}
}
}
void generateGrid() {
int number = 1;
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
map[x][y] = std::to_string(number).c_str()[0];
number += 1;
}
}
}
void tictacToeMap() {
std::cout << std::endl;
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
std::printf(" %c ", map[x][y]);
}
std::cout << std::endl;
}
}
TicTacToe() {
generateGrid();
while (true) {
champion();
tictacToeMap();
playgame();
computers_turn();
}
}
};
int main() {
TicTacToe ticTacToe;
return 0;
}
I want to draw a ASCII-art Christmas tree in C++ using functions with parameters.
This is what I have got so far, which works for the most part however when drawing the actual body of the tree (leaves) it seems to repeat it multiple times.
This effect seems to increase as the height of the tree does as well. So for example if a height of 4 is entered for the tree then the body will be drawn 2 times. if the height is 5, then it is drawn 3 times. 6 is 4 times and so on.
Any help?
#include <iostream>
using namespace std;
const char BLANK = ' ';
const char LEAF = '#';
const char WOOD = '|';
void drawAXmasTree();
void drawFoliage(int);
void drawTrunk(int);
void getValidHeight(int&);
void drawALineOfFoliage(int, int);
int main()
{
cout << "Due on 11 December 2018 \n\n";
drawAXmasTree();
}
void drawAXmasTree()
{
int treeHeight = 0;
getValidHeight(treeHeight); //read in a valid value for the tree height
drawFoliage(treeHeight); //draw tree foliage
drawTrunk(treeHeight); //draw tree trunk
}
void drawFoliage(int trHgt) //draw the foliage
{
int branchLine = 1;
int treeHeight = trHgt;
while (branchLine <= (trHgt - 2))
{
drawALineOfFoliage(treeHeight, branchLine);
branchLine += 1;
}
}
void drawTrunk(int trHgt) //draw the trunk
{
int trunkLine = 1;
int spaces;
while (trunkLine <= 2) // for each line in the truck
{
spaces = 1;
while (spaces <= (trHgt - 2)) //draw the spaces on the left
{
cout << BLANK;
spaces += 1;
}
cout << WOOD; //draw the truck
cout << endl; //go to next line
trunkLine += 1;
}
}
void getValidHeight(int& trHgt)
{
do
{
cout << "Enter the size of the tree(4 - 20): ";
cin >> trHgt;
if (trHgt < 4 || trHgt > 20)
{
cout << "ERROR: Invalid height! ";
}
}
while (trHgt < 4 || trHgt > 20);
}
void drawALineOfFoliage(int trHgt, int brLine)
{
int treeHeight = trHgt;
int branchLine = brLine;
int spaces = trHgt - 2;
for (int i = 0; i < (treeHeight - 2); i++) {
for (int j = spaces; j > 0; j--)
{
cout << BLANK;
}
for (int foliage = 0; foliage <= i * 2; foliage++)
{
cout << LEAF;
}
spaces--;
cout << endl;
}
}
The problem in your code is the following:
In the function drawFoliage you intended to loop and call drawALineOfFoliage for each line. But you actually draw the whole tree every time you call drawALineOfFoliage.
In order to fix you code, just replace drawFoliage for drawALineOfFoliage in the drawAXMasTree function, like this:
void drawAXmasTree()
{
int treeHeight = 0;
getValidHeight(treeHeight); //read in a valid value for the tree height
drawALineOfFoliage(treeHeight);
drawTrunk(treeHeight);
}
Notice you don't need the second argument in drawALineOfFoliage since you don't actually use it.
As for drawFoliage, you can just erase it.
Thanks for the help, I managed to resolve this because like people said it was drawing the whole tree rather than just one line. All I had to do was change the drawALineOfFoliage function to the following;
void drawALineOfFoliage(int trHgt, int brLine)
{
int treeHeight = trHgt;
int branchLine = brLine - 1;
int spaces = trHgt - 2;
for (int i = spaces; i > branchLine; i--)
{
cout << BLANK;
}
for (int foliage = 0; foliage <= branchLine * 2; foliage++)
{
cout << LEAF;
}
spaces--;
cout << endl;
}
Check this out:
#include <iostream>
using namespace std;
const char BLANK = ' ';
const char LEAF = '#';
const char WOOD = '|';
void drawAXmasTree();
void drawFoliage(int);
void drawTrunk(int);
void getValidHeight(int&);
void drawALineOfFoliage(int, int);
int main()
{
cout << "Due on 11 December 2018 \n\n";
drawAXmasTree();
system("pause");
}
void drawAXmasTree()
{
int treeHeight = 0;
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
void drawFoliage(int trHgt)
{
int branchLine = 1;
int treeHeight = trHgt;
while (branchLine <= (trHgt - 2))
{
drawALineOfFoliage(treeHeight, branchLine);
branchLine += 1;
}
}
void drawTrunk(int trHgt)
{
int trunkLine = 1;
int spaces;
for (trunkLine; trunkLine <= 2; trunkLine++)
{
for (spaces = 1; spaces <= (trHgt - 1); spaces++)
{
cout << BLANK;
}
cout << WOOD;
cout << "\n";
}
}
void getValidHeight(int& trHgt)
{
do
{
cout << "Enter the size of the tree(4 - 20): ";
cin >> trHgt;
if (trHgt < 4 || trHgt > 20)
{
cout << "ERROR: Invalid height! ";
}
} while (trHgt < 4 || trHgt > 20);
}
void drawALineOfFoliage(int trHgt, int brLine)
{
int treeHeight = trHgt;
int branchLine = brLine;
int spaces(0);
int tree(0);
for (spaces; spaces < (trHgt - branchLine); spaces++)
{
cout << BLANK;
}
for (tree; tree < (branchLine * 2 - 1); tree++)
{
cout << LEAF;
}
cout << endl;
}
Can someone please help me. I am struggling to find in my code why the last value in column B always gets incremented by one. I have written some code since its an assignment due today. I also cant figure out why the last value in column B is not equal to 196 because in the reset function it sets all the values in the array to 196 . Any suggestion would be appreciated. Thank you in advance
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main() { //main starts
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats) {
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
if (i == 0) {
cout << " " << static_cast<char>(j + 65) << " ";
} else {
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++) {
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats) {
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS))) {
if (seats[(seatChoiceNumber)][(letter - 65)] == 82) {
} else {
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats) {
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats) {
printAllSeats(seats);
anyFreeSeats = false;
}
}
} else {
}
}
I kind of cleaned up the code a bit. It seems you found your answer in the comments, so I just did some indentation. Try and eliminate whitespaces in your code (mind you, the one I am putting here is not perfect either, but you get the point). Clean and easy to read code doesn't only make it better for you, but as you get higher up in the industry and other people begin reading and working on your code, having clean and easy to read code really helps :)
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main()
{
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats)
{
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++)
{
if (i == 0)
{
cout << " " << static_cast<char>(j + 65) << " ";
}
else
{
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++)
{
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats)
{
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS)))
{
if (seats[(seatChoiceNumber)][(letter - 65)] == 82)
{
}
else
{
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats)
{
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats)
{
printAllSeats(seats);
anyFreeSeats = false;
}
}
}
else {
}
}
Note: Some more whitespaces could even come out but I generally like to have spaces after certain statements (personal preference).