error: expected ‘}’ at end of input -- when there is one [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am getting this error when compiling:
project6.cpp: In function ‘int main()’:
project6.cpp:187: error: expected ‘}’ at end of input
However, there is clearly an end bracket to my int main () function at that line, so I am confused as to why I am getting this error. I also checked all of my other brackets and found none that were not closed. Any help would be much appreciated!
#include <iostream>
using namespace std;
void initRace(char grid[52][72])
{
for(int r = 0; r < 52; r = r + 1)
{ for(int c = 0; c < 72; c = c + 1)
{ grid[r][0] = 'X';
grid[0][c] = 'X';
grid[r][71] = 'X'; // border
grid[51][c] = 'X';
}
for(int c = 65; c <= 70; c = c + 1)
{ grid[51][c] = 'F'; // finish line
}
}
for(int r = 1; r <= 35; r = r + 1)
{ for(int c = 10; c <= 29; c = c + 1)
{ grid[r][c] = 'X'; // first barrier
}
}
for(int r = 16; r <= 50; r = r + 1)
{ for(int c = 40; c <=64; c = c + 1)
{ grid[r][c] = 'X'; //second barrier
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =1; c <=9; c = c + 1)
{ grid[r][c] = ' '; //first block of spaces
}
}
for(int r = 36; r <= 50; r = r + 1)
{ for(int c =10; c <=29; c = c + 1)
{ grid[r][c] = ' '; //second block of spaces
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =30; c <=39; c = c + 1)
{ grid[r][c] = ' '; //third block of spaces
}
}
for(int r = 1; r <= 15; r = r + 1)
{ for(int c =40; c <=64; c = c + 1)
{ grid[r][c] = ' '; //fourth block of spaces
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =65; c <=70; c = c + 1)
{ grid[r][c] = ' '; //fifth block of spaces
}
}
grid[1][1] = 'O';
}
void printRace(char grid[52][72])
{ for (int i = 0 ; i < 52; i = i + 1)
{ for (int j = 0 ; j < 72; j = j + 1)
{ cout << grid[i][j] << " ";
}
cout << endl;
}
}
int main(void)
{ char grid[52][72];
initRace(grid);
int xAcceleration;
int yAcceleration;
int xVelocity = 0;
int yVelocity = 0;
int xPosition = 1;
int yPosition = 1;
for(int i = 1; i < 100; i = i + 1)
{ printRace(grid);
cout << "Horizontal and vertical acceleration (-1,0,1): ";
cin >> xAcceleration;
cin >> yAcceleration;
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
if((yAcceleration != 0) && (yAcceleration != 1) && (yAcceleration != -1))
{ printRace(grid);
if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
}
i = 500;
}
xVelocity = xVelocity + xAcceleration;
yVelocity = yVelocity + yAcceleration;
xPosition = xPosition + xVelocity;
yPosition = yPosition + yVelocity;
if((xPosition >= 10) && (xPosition <=29) && (yPosition >= 1) && (yPosition<= 35))
{ grid[yPosition][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl; // crashed into first barrier
i = 500;
}
if((xPosition >= 40) && (xPosition <= 64) && (yPosition >= 16) && (yPosition <= 50))
{ grid[yPosition][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl; // crashed into second barrier
i = 500;
}
if(xPosition <= 0) //crashed into left border
{ grid[yPosition][0] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if(yPosition <= 0) //crashed into top border
{ grid[0][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if(xPosition >= 71) //crashed into right border
{ grid[yPosition][71] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if((yPosition >= 51) && (xPosition >= 1) && (xPosition <= 39)) //crashed into bottom border
{ grid[51][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if((xPosition >= 65) && (xPosition <= 70) && (yPosition >= 51)) // crossed finish line
{ grid[51][xPosition] = 'O';
printRace(grid);
cout << "Finished after " << i << " seconds" << endl;
i = 500;
}
grid[yPosition][xPosition] = 'O';
}
return 0;
} // THIS IS LINE 187

One of your else blocks does not have a closing brace... Look below:
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
Due to this, the total number of closing braces is not equal to the opening braces, hence the error.

The first else statement in the big for-loop misses a closing brace.

The error is here:
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
The else block has not been closed.

You are missing a closing curly bracket } in the else clause below:
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
Should be:
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
}

Add a closing brace in the section below(at marked line):
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{
cout << "Crashed after " << i << " second" << endl;
}
else
{
cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}//<-- This was missing
}

I think this block is missing a proper ending bracket,
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}

Related

C++ Program leaving my for loop prematurely

I'm working on a little poker application and i've run into the first problem I just can't seem to comprehend.
while (allplayersGood != 1) { //round table till all decided
cout << "TOP OF WHILE LOOP";
for (int i = 0; i < PLAYER_COUNT; i++) { //for loop for decisions from non button or blinds
int player_decision = 1;
char choice;
if ((players[i].playerhand.card1.value != 'F') && (players[i].playerhand.card1.value != 'C')) {
if ((players[i].blind != 1 && players[i].blind != 2) && players[i].button != true) {
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
if (playerTable->currentBet > players[i].currentBet) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type F for Fold, B for Call, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'F') {
player_decision = 0;
players[i].fold();
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'B') {
player_decision = 0;
players[i].call(playerTable);
}
}
if ((playerTable->currentBet == players[i].currentBet) && player_decision != 0) { //big blind after round table
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type C for Check, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'B') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'C') {
if (players[i].check(playerTable) == true) {
player_decision = 0;
}
}
}
}
}
else if (players[i].blind == 1 || players[i].blind == 2) {
if (players[i].blind == 1) {
players[i].chip_amount -= sblind;
playerTable->currentPot += sblind;
players[i].blind = 0;
players[i].currentBet = sblind;
}
if (players[i].blind == 2) {
players[i].chip_amount -= bblind;
playerTable->currentPot += bblind;
players[i].blind = 0;
players[i].currentBet = bblind;
}
}
}
}
for (int i = 0; i < PLAYER_COUNT; i++) { //seperate loop for button and blinds that were ignored in loop above
int player_decision = 1;
char choice;
if (players[i].button == true || players[i].blind == 1) { //button and small blind
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type F for Fold, B for Call, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'F') {
player_decision = 0;
players[i].fold();
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'B') {
player_decision = 0;
players[i].call(playerTable);
}
}
}
cout << i;
if (players[i].blind == 2) { //big blind
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "C for Check, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'C') {
if (players[i].check(playerTable) == true) {
player_decision = 0;
}
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
}
}
}
int playersBetting = 0;
int playersGood = 0;
int playersChecked = 0;
int playersNot = 0;
for (int i = 0; i < PLAYER_COUNT; i++) {
if (players[i].playerhand.card1.value != 'F') {
playersBetting++;
if (players[i].currentBet == playerTable->currentBet) {
playersGood++;
}
}
}
for (int i = 0; i < PLAYER_COUNT; i++) {
if (players[i].playerhand.card1.value != 'F') {
if (players[i].isChecked == true) {
playersChecked++;
}
else {
playersNot++;
}
}
}
cout << playersBetting << playersGood;
if ((playersBetting == playersGood) || (playersNot == 0)) {
cout << "NEXT ROUND STARTED";
}
}
The issue is, during the second for loop with comment "seperate loop for button and blinds that were ignored in loop above" after the first if statement succeeds because players[0] has button equal to true, the player will make the terminal input as a decision, and the program will exit the for loop and go down to the end with the playersBetting and playersGood loops, then return back to the for loop at index 1 correctly.
I'm sorry if this is a little complicated to understand there is a lot of code that I probably didn't put into context very well, if you need any extra information please let me know.
Thank you.
You seem to have different loops inside one another. This is possible, but in that case, you need to use another loop variable (j instead of i), let me show you what happens:
for i ...
for j ...
This causes the following values to be taken for i and j:
i j
1 1
1 2
1 ...
1 n
2 1
2 2
2 ...
2 n
...
n 1
n 2
...
n n
... and here it stops.
If you keep using i in the inner loop, this is what you get:
i (outside loop) i (inside loop)
1 1
2 2 // indeed: changing i inside also changes i outside
... ...
n n
So you jump out of the outside loop, even after just having looped the inside loop one time.
I figured it out, it was unrelated to the actual loop and actually had to do with a value I changed upstream. Thank you for the few who tried to help with such little context haha
Have a good one

c++ coordinate grid map marking coordinates

ifstream myfile;
myfile.open("config.txt");
if (myfile.fail())
{
cerr << "Error opening config file" << endl;
myfile.close();
}
int line_no = 0;
while (line_no != 3 && getline(myfile, line3)) {
++line_no;
}
while (line_no != 7 && getline(myfile, line7)) {
++line_no;
}
while (line_no != 10 && getline(myfile, line10)) {
++line_no;
}
while (line_no != 14 && getline(myfile, line14)) {
++line_no;
}
while (line_no != 18 && getline(myfile, line18)) {
++line_no;
}
cout << line3 << endl;
cout << line7 << endl;
cout << line10 << endl;
cout << line14 << endl;
cout << line18 << endl;
gridXIdxA = stoi(ExtractString(line3, "=", "-"));
gridXIdxB = stoi(ExtractString(line3, "-", "\n"));
gridYIdxA = stoi(ExtractString(line7, "=", "-"));
gridYIdxB = stoi(ExtractString(line7, "-", "\n"));
cout << gridXIdxA << endl;
cout << gridXIdxB << endl;
cout << gridYIdxA << endl;
cout << gridYIdxB << endl;
int y = gridYIdxB + 1;
y > -1;
mapBoundaryX = gridXIdxB + 6; // dynamic array to print out boundary of city map
mapBoundaryY = gridYIdxB + 4;
int** dMapBoundaryArray;
dMapBoundaryArray = new int*[mapBoundaryX]();
for (int i = 0; i < mapBoundaryX; i++)
{
dMapBoundaryArray[i] = new int[mapBoundaryY];
}
for (int i = 0; i < mapBoundaryX; i++)
{
cout << endl;
for (int j = 0; j < mapBoundaryY; j++)
{
dMapBoundaryArray[i][j] = i;
if (i == 0 && j > 0 && j < gridXIdxB+4)
{
cout << "# "; // top
}
if (i == 0 && j == 0)
{
cout << " ";
}
if (i == (gridYIdxB+2) && j > 0 && j < gridXIdxB+4)
{
cout << "# "; // bottom
}
if (i == (gridYIdxB + 2) && j == 0)
{
cout << " ";
}
if (i>0 && i<12 && j==1)
{
cout << "#"; // left
}
else if (i == 6 && j == 3)
{
cout << " ";
cout << 3;
}
else if (i == 6 && j == 4)
{
cout << " ";
cout << 3;
}
else if (i == 7 && j == 4)
{
cout << " ";
cout << 3;
}
else if (i > 0 && i < 12 && j == 13) //right
{
cout << setw(24)<<right << "#";
}
if (i == 13 && j > -1 && j < 2) //x axis
{
cout << " ";
}
if (i == 13 && j > 1 && j < 13)
{
x = x++;
cout <<" " << x; //x axis
}
if (j == 0 && i <= gridYIdxB+1 && i >= 1) // y axis
{
y = --y;
cout << y; //y axis
}
}
}
Hi, have some questions that i need help with, beginner to c++ currently so definitely appreciate if stuff can be explained in simple terms. I can manage to create the edges of the coordinate map but I also need to pinpoint certain coordinates within the map and mark them for eg. with a '3'. Is there anyway to pinpoint the coords without moving the column of '#' on the right?
Output
before marking coords
after marking coords
You may already have noticed that C++ alone has no facilities to set a character at a certain position on the screen. curses is a rather wide-spread library that helps with that. However, if you want to stay with your own code you can still get some inspiration from how curses handles output. The contents of the screen are buffered in memory and only when you request it all of it is updated. You can do similar in your own code. Store the contents you want to print on the screen in a std::vector<std::vector<char> or a std::vector<std::string>. Modify the contents as desired and when printing you do not have to bother about alignment and formatting anymore, it is just a simple loop:
for (const auto& line : screen) {
for (const auto& character : line) std::cout << character;
std::cout << "\n";
}

Xcode 7 'Function Definition is not allowed here'

My code is below. I have tried all different options. I am still learning C++ through classes but have reached an impasse. Xcode says error are in the lines there are three straight * marks. Ex: ***code* Any advice would be great and looking for ways to understand arrays better.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int board [7] [7];
void initBoard (int array1[7][7]) {
srand(time(0));
string position[7][7];
int moves = rand() % 8 + 5;
int r = 0;
int c = 0;
for (c = 0; c < 7; c++) {
if (c == 0 || c == 6) {
for (r = 0; r < 7; r++) {
position[r][c] = " ";
}
}
else {
for (r = 0; r < 7; r++) {
if (r == 0 || r == 6) {
position[r][c] = " ";
}
else {
position[r][c] = "_ ";
}
}
}
}
***void plantBombs (int array2[7][7]) {***
r = 0;
c = 0;
int k = (rand() % 5) + 1;
int d = ((rand() % 111) % 5) + 1;
int numBombs = rand() % 4 + 3;
int bombR[numBombs];
int bombC[numBombs];
int i = 0;
while (i < numBombs){
bombR[i] = (rand() % 71) % 5 + 1;
bombC[i] = (rand() % 123) % 5 + 1;
while (bombR[i] == k && bombC[i] == d) {
bombR[i] = (rand() % 97) % 5 + 1;
bombC[i] = (rand() % 79) % 5 + 1;
}
while ((i > 0) && (bombR[i-1] == bombR[i]) && (bombC[i-1] == bombC[i])){
bombR[i] = (rand() % 213) % 5 + 1;
bombC[i] = (rand() % 857) % 5 + 1;
}
position[bombR[i]][bombC[i]] = "* ";
i = i + 1;
}
}
***void placeYou (int array2[7][7]){***
position[k][d] = "U ";
}
***void movePlayer (int arrary3[7][7]){***
while (moves != 0) {
cout << "SURVIVE " << moves << " MORE MOVES!" << endl;
for (c = 0; c < 7; c++) {
for (r = 0; r < 7; r++) {
cout << position[r][c];
}
cout << endl;
}
cout << "Enter direction (N/S/E/W): ";
string direction;
cin >> direction;
//if (direction == "N")
if (direction == "N"){
if (position[k][d-1] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k][d-1] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k][d-1] = "U ";
d = d - 1;
}
}
if (direction == "E"){
if (position[k+1][d] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k+1][d] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k+1][d] = "U ";
k = k + 1;
}
}
if (direction == "S"){
if (position[k][d+1] == " ") {
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k][d+1] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k][d+1] = "U ";
d = d + 1;
}
}
if (direction == "W"){
if (position[k-1][d] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k-1][d] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k-1][d] = "U ";
k = k - 1;
}
moves = moves - 1;
}
if (moves == 0){
cout << "***YOU WIN***";
}
else {
cout << "";
}
}
}
}
int main()
{
initBoard(board);
***plantBombs(board);
placeYou(board);
movePlayer(board);***
return 0;
}

do-while loop, infinity loop

I have 2 things that aren't working with this code. The first thing is that in my second do-while loop in main, when I input 'g' or 'G' and I put in coordinates and select if it is left or right it loops continuously.
My second thing that doesn't work right is that when I shoot my laser, a baffle should turn it and make it go a different direction. This works but it doesn't turn the laser until the box after the baffle is located. I am so stuck with this, any help would be great, and anything that you also see that I should fix would be greatly appreciated.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
struct velocity
{
int X = 0, Y = 0;
};
struct position
{
int X = 0, Y = 0;
void addVelocity(velocity V)
{
X += V.X;
Y += V.Y;
}
};
int gameBoard[10][10];
int guessBoard[10][10];
int random(int);
void laser(int&);
int boxNum(position, velocity);
position calcExitPos(velocity&, int&);
position moveLaser(position&, velocity V);
velocity checkPos(position, velocity&);
bool checkWall(position, velocity);
int main()
{
char level, action;
int num = 0;
int score = 0, guess = 0, shots = 0, LorR = 0;
int X = 0, Y = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
gameBoard[i][j] = 0;
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
guessBoard[i][j] = 0;
}
}
cout << "Welcome to the Baffle Game." << endl;
cout << " 10 11 12 13 14 15 16 17 18 19" << endl;
cout << "9|___|___|___|___|___|___|___|___|___|___|20" << endl;
cout << "8|___|___|___|___|___|___|___|___|___|___|21" << endl;
cout << "7|___|___|___|___|___|___|___|___|___|___|22" << endl;
cout << "6|___|___|___|___|___|___|___|___|___|___|23" << endl;
cout << "5|___|___|___|___|___|___|___|___|___|___|24" << endl;
cout << "4|___|___|___|___|___|___|___|___|___|___|25" << endl;
cout << "3|___|___|___|___|___|___|___|___|___|___|26" << endl;
cout << "2|___|___|___|___|___|___|___|___|___|___|27" << endl;
cout << "1|___|___|___|___|___|___|___|___|___|___|28" << endl;
cout << "0|___|___|___|___|___|___|___|___|___|___|29" << endl;
cout << " 39 38 37 36 35 34 33 32 31 30" << endl << endl;
cout << "This is the game board, you will shoot lasers and try to find all the baffles. " << endl << endl;
//cout << "For beginner there are 4 baffles, for intermediate there are 7 baffles and for advanced there are 10 baffles." << endl;
//cout << "Please type B for beginner, I for intermediate, and A for advanced" << endl;
do
{
cout << "For beginner there are 4 baffles, for intermediate there are 7 baffles" << endl;
cout << "and for advanced there are 10 baffles." << endl;
cout << "Please type B for beginner, I for intermediate, and A for advanced" << endl;
cin >> level;
} while (level != 'b' && level != 'B' && level != 'i' && level != 'I' && level != 'a' && level != 'A');
if (level == 'B' || level == 'b')
{
//Baffles equals 4
num = random(4); // function returns to num
}
else if(level == 'I' || level == 'i')
{
//Baffles equals 7
num = random(7);
}
else
{
//Baffles equals 10
num = random(10);
}
cout << "We will begin the game now." << endl;
cout << "Here are some commands that you will need to know to play." << endl;
cout << "L: Laser shot, then pick a number on the board to shoot the laser from." << endl;
cout << "G: Guess the location of one baffle with coordinance and L or R to choose a left or right baffle." << endl;
cout << "S: When you want me to print the score that you have." << endl;
cout << "P: Print the board showing the baffles that you have already found." << endl;
cout << "Q: Quit the game at any time" << endl;
cout << "C: This shows you the board with all the baffles and which direction they are." << endl;
do
{
cout << "Please type in your command." << endl;
cin >> action;
if (action == 'L' || action == 'l')
{
laser(shots);
}
else if (action == 'G' || action == 'g')
{
cout << "We will guess where the baffle is." << endl;
cout << "Please enter the X coordinate." << endl;
cin >> X;
cout << "Please enter the Y coordinate." << endl;
cin >> Y;
//cout << gameBoard[X][Y] << endl;
cout << "Please enter L and R for Left and Right Baffles." << endl;
cin >> LorR;
//cout << gameBoard[X][Y];
if (gameBoard[X][Y] == 1)//Right
{
//cout << "1" << endl;
if (LorR == 'R' || LorR == 'r')
{
cout << "Good job you got it correct." << endl;
guessBoard[X][Y] = 'R';
//points++
}
else
cout << "Wrong!" << endl;
}
else if (gameBoard[X][Y] == 2)//Left
{
//cout << "2" << endl;
if (LorR == 'L' || LorR == 'l')
{
cout << "Good job you got it correct." << endl;
guessBoard[X][Y] = 'L';
//points++
}
else
cout << "Wrong!" << endl;
}
else if (gameBoard[X][Y] == 0)
{
cout << "Your are wrong!" << endl;
}
}
else if (action == 'S' || action == 's')
{
cout << "Number of shots: " << shots << endl;
cout << "Number of guesses: " << guess << endl;
cout << "Current score: " << endl;//Get score
}
else if (action == 'P' || action == 'p')
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << setw(2) << guessBoard[i][j] << " ";
}
cout << endl;
}
}
else if (action == 'C' || action == 'c')
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << setw(2) << gameBoard[i][j] << " ";
}
cout << endl;
}
}
else if (action == 'q' || action == 'Q')
{
}
else if (action != 'q' && action != 'Q')
{
cout << "*** Illegal command - Try again ***" << endl;
}
cout << "loop" << endl;
}
while (action == 'q' || action == 'Q' || action == 'L' || action == 'l' || action == 'G' || action == 'g' ||
action == 'S' || action == 's' || action == 'P' || action == 'p' || action == 'C' || action == 'c');
}
//This function finds a random number for the find a random index
// in the array.
int random(int randnum)
{
unsigned seed = time(0); //get the system time.
srand(seed); //seed the random number generator.
int x, y, randomNum = 0;
int count;
for (count = 0; count < randnum; count++)
{
do
{
x = (rand() % (9 - 0 + 1)) + 0;
y = (rand() % (9 - 0 + 1)) + 0;
} while (gameBoard[x][y] != 0);
cout << "(" << x << "," << y << ")" << endl;
randomNum = rand();
gameBoard[x][y] = (randomNum % 2) + 1;
cout << gameBoard[x][y] << endl;
//2 = Left baffle
//1 = right baffle
}
return gameBoard[x][y];
}
void laser(int &shots) //Shoots a laser where user asks it to
{
int shoot;
cout << "Please input what number you want to shoot the laser from." << endl;
cin >> shoot;
shots++;
velocity V;
position P = calcExitPos(V,shoot);
//checkPos(P, V);
//cout << "1" << endl;
do
{
checkPos(P, V);
moveLaser(P, V);
//cout << "1" << endl;
} while (checkWall(P, V) == false);
cout << "Laser shot #" << shots << " exited the box at " << boxNum(P, V) << endl;
}
int boxNum(position P, velocity V) //Figures out what number the laser exits
{
if (P.X == 0 && V.X == -1)
{
return 9 - P.Y;
}
else if (P.Y == 0 && V.Y == -1)
{
return 10 + P.X;
}
else if (P.X == 9 && V.X == 1)
{
return P.Y + 20;
}
else if (P.Y == 9 && V.Y == 1)
{
return 39 - P.X;
}
}
position calcExitPos(velocity &V, int &box)//Figures out where the laser shoots from by the number the user inputs
{
position P1;
if (box >= 0 && box <= 9)
{
V.X = 1;
V.Y = 0;
P1.X = 0;
P1.Y = (9 - box);
}
else if (box >= 10 && box <= 19)
{
V.X = 0;
V.Y = 1;
P1.X = (box - 10);
P1.Y = 0;
}
else if (box >= 20 && box <= 29)
{
V.X = -1;
V.Y = 0;
P1.X = 9;
P1.Y = (box - 20);
}
else if (box >= 30 && box <= 39)
{
V.X = 0;
V.Y = -1;
P1.X = (39 - box);
P1.Y = 9;
}
return P1;
}
position moveLaser(position &P, velocity V)
{
int baffletype = gameBoard[P.X][P.Y];
if (baffletype == 1)//Right
{
if (V.X == 1)
{
V.Y = -1;
V.X = 0;
P.Y--;
}
else if (V.X == -1)
{
V.Y = 1;
V.X = 0;
P.Y++;
}
else if (V.Y == 1)
{
V.X = -1;
V.Y = 0;
P.X--;
}
else if (V.Y == -1)
{
V.X = 1;
V.Y = 0;
P.X++;
}
}
else if (baffletype == 2)//Left
{
if (V.X == 1)
{
V.Y = 1;
V.X = 0;
P.Y++;
}
else if (V.X == -1)
{
V.Y = -1;
V.X = 0;
P.Y--;
}
else if (V.Y == 1)
{
V.X = 1;
V.Y = 0;
P.X++;
}
else if (V.Y == -1)
{
V.X = -1;
V.Y = 0;
P.X--;
}
}
else if (baffletype == 0)
{
if (V.X == 1)
{
V.Y = 0;
V.X = 1;
P.X++;
}
else if (V.X == -1)
{
V.Y = 0;
V.X = -1;
P.X--;
}
else if (V.Y == 1)
{
V.X = 0;
V.Y = 1;
P.Y++;
}
else if (V.Y == -1)
{
V.X = 0;
V.Y = -1;
P.Y--;
}
}
cout << "(" << P.X << ","<< P.Y << ")" << endl;
return P;
}
velocity checkPos(position P, velocity &V) //Directs the laser when the laser hits a baffle
{
int baffletype = gameBoard[P.X][P.Y];
if (baffletype == 1)//Right
{
if (V.X == 1)
{
V.Y = -1;
V.X = 0;
}
else if (V.X == -1)
{
V.Y = 1;
V.X = 0;
}
else if (V.Y == 1)
{
V.X = -1;
V.Y = 0;
}
else if (V.Y == -1)
{
V.X = 1;
V.Y = 0;
}
}
else if (baffletype == 2)//Left
{
if (V.X == 1)
{
V.Y = 1;
V.X = 0;
}
else if (V.X == -1)
{
V.Y = -1;
V.X = 0;
}
else if (V.Y == 1)
{
V.X = 1;
V.Y = 0;
}
else if (V.Y == -1)
{
V.X = -1;
V.Y = 0;
}
}
else if (baffletype == 0)
{
if (V.X == 1)
{
V.Y = 0;
V.X = 1;
P.X++;
}
else if (V.X == -1)
{
V.Y = 0;
V.X = -1;
P.X--;
}
else if (V.Y == 1)
{
V.X = 0;
V.Y = 1;
P.Y++;
}
else if (V.Y == -1)
{
V.X = 0;
V.Y = -1;
P.Y--;
}
}
//cout << "( " << P.X << ", " << P.Y << ")" << endl;
return V;
}
bool checkWall(position P, velocity V) //Checks to see if the laser hits the wall
{
//cout << "2" << endl;
P.addVelocity(V);
return (P.X > 9 || P.X < 0 || P.Y > 9 || P.Y < 0); //Checks to see if the next position is out of bounds
}
I don't have a complete solution for you but a couple of things.
You should make your while loop test simpler.
How about this (pseudocode):
bool done = false;
while (!done) {
// Process action loop
cin >> action;
action = toupper(action); // Make everything upper case
processTheAction(action);
done = areWeDoneYet(action);
}
Break up the complex "are we done yet" task and other things into functions. (Divide and conquer)
bool areWeDoneYet(action) {
switch (action) {
case A:
case B:
... (cases where we are not done)
return false;
case E:
case F:
... (cases where we are done)
return true;
}
}
Make your "event loop" simple. Have it call a complex function
"ProcessAction" That way you can tell if you are busy processing when you have an issue, or if your having an issue with the event loop.
void processTheAction( action ) {
switch (action) {
case A:
case a: // Optional method if you don't do topper on Action
// Code for action 'A' goes here
// If a *lot* of code make a function like "ProcessActionA()"
break;
case B:
case b: // not needed if you do topper
// Code for action 'B' goes here
break;
default:
printf("I'm hosed cause I forgot to manage action [%c]\n",
action);
}
}
Don't know if this helps, but it might help you to break down the problem.

How do I exit a loop in C++ without using break?

I'm writing a code to swap integers in an array and I want to know how I can exit a loop without using a break statement and keeping my logic consistent. Here is my code below:
int swapped = 0;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
break;
}
}
if (swapped) {
break;
}
Use goto [I'll be bashed because of this].
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value "
<< arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
goto done;
}
}
done:
something;
for (int i = 1; i < arraycount && !swapped; ++i)
{
for (int j = 1; j < arrays[i][0] + 1 && !swapped; ++j)
{
if(arrays[i][j] % 2 != 0)
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with " << "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
}
}
}
this will do the same thing you have in inner loop.
Using a Break statement does not necessarily make your codes logic inconsistent and breaks are often useful to improve the readability of your code. But in answer to your question this can be achieved by utilizing while loops and logical boolean operators. A modified version of your code is below, I have tried to modify it as little as possible so you can still see your code within the example. There are a few logical errors in your code that I have left in the example below that you might want to look into. In particular the line below will print "is odd" when in fact the number would be even. If you where wanting to check if the number arrays[0][first] is odd then the following if statement would be needed if (arrays[0][first] % 2 != 0) instead of if (arrays[0][first] % 2 == 0).
Logical Error
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
This is the code without using breaks.
bool swapped = true;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
int i = 1;
while ( (i < arraycount) && swapped)
{
int j = 1;
bool if_odd = true;
while ((j < arrays[i][0] + 1) && if_odd)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = false;
if_odd = false;
}
j++;
}
i++;
}
}