Xcode 7 'Function Definition is not allowed here' - c++

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;
}

Related

Finding determinate and Multiplication two matrixes with different length in c++

Hey guys I want to make a calculator for matrixes but I have problems.
first, when I want to multiply them it will work when columns and rows are equal in the matrixes but when it's different like n1=5 m1=2 n2=2 m2=6 it goes wrong and have some bugs
and the bigger problem is for calculating the determinate I have no idea what should I do
thanks for answering.
this the code
#include <iostream>
using namespace std;
int main() {
int n1, m1, n2, m2;
int counter = 0;
int ask;
bool matrixCheck;
int determinant = 0;
cout << "pls enter N1" << endl;
cin >> n1;
while (n1 > 100) {
cout << "N most be lower than 100";
cin >> n1;
}
cout << "pls enter M1" << endl;
cin >> m1;
while (m1 > 100) {
cout << "M most be lower than 100";
cin >> m1;
}
cout << "pls enter N2" << endl;
cin >> n2;
while (n2 > 100) {
cout << "N most be lower than 100";
cin >> n2;
}
cout << "pls enter M2" << endl;
cin >> m2;
while (m2 > 100) {
cout << "M most be lower than 100";
cin >> m2;
}
int matrixA[n1][m1];
int matrixB[n2][m2];
cout << "ENTER MATRIX A" << endl;
for (int i = 0; i <= n1 - 1; i++) {
cout << " ";
cout << counter + 1 << "," << i + 1 << "= ";
cin >> matrixA[counter][i];
cout << " ";
if (i == n1 - 1) {
cout << endl;
counter++;
i = -1;
}
if (counter == m1) {
break;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[counter][j] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
counter = 0;
cout << "ENTER MATRIX B" << endl;
for (int k = 0; k <= n2 - 1; k++) {
cout << " ";
cout << counter + 1 << "," << k + 1 << "= ";
cin >> matrixB[counter][k];
cout << " ";
if (k == n2 - 1) {
cout << endl;
counter++;
k = -1;
}
if (counter == m2) {
break;
}
}
counter = 0;
cout << "This is B" << endl;
for (int q = 0; 1 <= n2 - 1; q++) {
cout << matrixB[counter][q] << " ";;
if (q == n2 - 1) {
cout << endl;
counter++;
q = -1;
}
if (counter == m2) {
break;
}
}
cout << "Choose an operation" << endl
<< "1-(+) 2-(-) 3-(A * B) 4-(A / B) 5-(A --> B) 6-(B --> A) 7-(a * A) 8-(|A|) 9-(|B|)" << endl;
if (n1 != n2 || m1 != m2) {
cout
<< "We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different"
<< endl;
matrixCheck = false;
} else if (n1 == n2 && m1 == m2) {
matrixCheck = true;
}
cin >> ask;
while (ask == 1 || ask == 2 || ask == 5 || ask == 6) {
if (matrixCheck) {
break;
}
cout
<< "We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different"
<< endl;
cin >> ask;
}
if (ask == 1 && matrixCheck) {
counter = 0;
for (int n = 0; n <= n1 - 1; n++) {
matrixA[counter][n] = matrixA[counter][n] + matrixB[counter][n];
if (n + 1 == n1) {
n = -1;
if (counter + 1 == m1) {
break;
}
counter++;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
}
if (ask == 2 && matrixCheck) {
counter = 0;
for (int n = 0; n <= n1 - 1; n++) {
matrixA[counter][n] = matrixA[counter][n] - matrixB[counter][n];
if (n + 1 == n1) {
n = -1;
if (counter + 1 == m1) {
break;
}
counter++;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
}
if (ask == 3) {
int matrixResult[n1][m1];
int sum = 0;
// Multiplying two matrices...
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
sum = 0;
for (int k = 0; k < m1; k++)
sum = sum + (matrixA[i][k] * matrixB[k][j]);
matrixResult[i][j] = sum;
}
}
cout << "\nMultiplication Result:\n";
counter = 0;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixResult[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m2) {
break;
}
}
}
}
and this the result
pls enter N1
3
pls enter M1
2
pls enter N2
2
pls enter M2
3
ENTER MATRIX A
1,1=3
1,2=3
1,3=3
2,1=3
2,2=3
2,3=3
This is A
3 3 3
3 3 3
ENTER MATRIX B
1,1=3
1,2=3
2,1=3
2,2=3
3,1=3
3,2=3
This is B
3 3
3 3
3 3
Choose an operation
1-(+) 2-(-) 3-(A * B) 4-(A / B) 5-(A --> B) 6-(B --> A) 7-(a * A) 8-(|A|) 9-(|B|)
We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different
3
Multiplication Result:
18 18 12600660
18 18 12600660
18 12600660 364487398
Process finished with exit code 0
Size of resulting matrix is n1 * m2, not n1*m1.

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

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.

I cannot figure out why my code won't get past the cin on line 60. Help?? c++

This program should be accepting a bet, generating a set of cards for each player, and adding the bet to the winner's pool. This is a class project. I cannot figure out what the problem is on line 60.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct cards {
int value;
string face;
string suit;
bool facebool;
};
struct hand {
cards card[4];
int antiduplicate[52];
};
struct player {
string name;
int sum;
int money;
hand hand;
int bet;
};
struct points {
int player1;
int player2;
};
hand drawhand(int [52]);
points calcvalue(player, player);
int winner(player, player);
int main() {
points store = {};
player house = {"The House"};
player player1 = {};
srand (time(0));
cout << "Enter player 1's name:";
getline(cin,player1.name);
cout << "Enter player 1's money:";
cin >> player1.money;
cout << endl;
house.money = (player1.money + 1);
do{
int deckarray[52] = {0};
//do{
cout << "What do you want to bet?\n";
/* line 60 */ cin >> player1.bet;
cout << "check";
if (player1.bet < player1.money){
}
else if (player1.bet > player1.money){
cout << "\nYou cannot go into debt!!\n";
}
else if (player1.bet == 0){
cout << "\nYou ended the game!";
return 0;
}
//}while (bet > player1.money);
house.hand = drawhand(deckarray);
for (int i = 0; i < 52; i++) {
if (house.hand.antiduplicate[i] == 1)
deckarray[i] = 1;
}
player1.hand = drawhand(deckarray);
for (int i = 0; i < 52; i++) {
if (player1.hand.antiduplicate[i] == 1)
deckarray[i] = 1;
}
cout << "\nCheck check\n";
store = calcvalue(player1, house);
player1.sum = store.player1;
house.sum = store.player2;
cout << player1.name << "'s hand:\n" << player1.hand.card[1].face << " of " <<player1.hand.card[1].suit << endl
<< player1.hand.card[2].face << " of " <<player1.hand.card[2].suit << endl <<player1.hand.card[3].face << " of " <<player1.hand.card[3].suit << endl;
cout << ">> " <<player1.name << " scored " << player1.sum << " points!\n\n";
cout << house.name << "'s hand:\n" << house.hand.card[1].face << " of " <<house.hand.card[1].suit << endl
<< house.hand.card[2].face << " of " <<house.hand.card[2].suit << endl <<house.hand.card[3].face << " of " <<house.hand.card[3].suit << endl;
cout << ">> " << house.name << " scored " << house.sum << " points!\n";
int win;
win = winner(player1, house);
if (win == 1){
cout << "\n" << player1.name << " wins the round!!" << endl;
player1.money = (player1.money + player1.bet); house.money = (house.money - player1.bet);
}
else if (win == -1){
cout << "\n\n" << house.name << " wins the round!!";
house.money = (house.money + player1.bet);
player1.money = (player1.money - player1.bet);
}
else if (win == 0){
cout << "\n\n" << house.name << " wins the round!!";
house.money = (house.money + player1.bet);
player1.money = (player1.money - player1.bet);
}
cout << endl << "House money: " << house.money << endl << "Player money: " << player1.money << endl << endl;
}while (player1.money > 0 && house.money > 0 && player1.bet != 0);
cout << "Game over!";
return 0;
}
hand drawhand(int deckarray[52])
{
string tsuit, tface;
int tvalue;
int suitvalue, facevalue;
bool tfacebool;
hand thand;
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 52; i++) {
if (deckarray[i] == 1)
thand.antiduplicate[i] = 1;
}
int index;
do {
index = rand()%52;
} while (thand.antiduplicate[i] == 1);
thand.antiduplicate[i] = 1;
facevalue = (index%13 + 1);
suitvalue = (index / 13);
switch (suitvalue)
{
case 0: tsuit = "Hearts";
break;
case 1: tsuit = "Diamonds";
break;
case 2: tsuit = "Clubs";
break;
case 3: tsuit = "Spades";
break;
}
switch (facevalue)
{
case 1: tface = "Ace";
tvalue = 1;
break;
case 11: tface = "Jack";
tvalue = 10;
tfacebool = true;
break;
case 12: tface = "Queen";
tvalue = 10;
tfacebool = true;
break;
case 13: tface = "King";
tvalue = 10;
tfacebool = true;
break;
}
if ((facevalue > 1) && (facevalue < 11))
tface = to_string(facevalue);
if (facevalue < 11) tvalue = facevalue;
thand.card[i].suit = tsuit;
thand.card[i].face = tface;
thand.card[i].value = tvalue;
thand.card[i].facebool = tfacebool;
}
return thand;
}
points calcvalue(player player1, player house)
{
points tpoints;
player1.sum = ((player1.hand.card[1].value + player1.hand.card[2].value + player1.hand.card[3].value) % 10);
if (player1.hand.card[1].facebool == true && player1.hand.card[2].facebool == true && player1.hand.card[3].facebool == true)
player1.sum = 10;
house.sum = ((house.hand.card[1].value + house.hand.card[2].value + house.hand.card[3].value) % 10);
if (house.hand.card[1].facebool == true && house.hand.card[2].facebool == true && house.hand.card[3].facebool == true)
house.sum = 10;
tpoints.player1 = player1.sum;
tpoints.player2 = house.sum;
return tpoints;
}
int winner(player player1, player house){
int winorlose;
if (player1.sum > house.sum)
{winorlose = 1;}
else if (player1.sum < house.sum)
{winorlose = -1;;}
else if (player1.sum == house.sum)
{winorlose = 0;}
return winorlose;
}
I see a lot of problems at first glance actually. eg: You've called a hand object 'hand' (thus redefining it), if you're going to call your object names the same as their class/struct, at least differentiate them with capitalization. eg: call your hand struct "Hand" instead of "hand"
cout << "What do you want to bet? ";
cin >> player1.bet; // <==== problem
cout << "check";
if (player1.bet < player1.money){
}
else if (player1.bet > player1.money){
cout << "\nYou cannot go into debt!!\n";
}
else if (player1.bet == 0){
cout << "\nYou ended the game!";
return 0;
}
That being said the issue you are having specifically relating to this section of code probably has something to do with a "\n" staying in the buffer and not getting cleared. Try using cin.ignore(numeric_limits::max(),'\n'); to solve this problem (don't forget to #include limits as well).
Similar problem here: c++ cin input not working?
Unfortunately I'm pressed for time and don't have time for a thorough look. :(

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

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;
}