# Help, my code isn't working. It won't run no matter how many times I refresh my screen!

asked Sep 2, 2021
//Before, it was saying there was a problem on line 157. But now nothing is happening.

//The program:

#include <cstdlib> // random number tools including srand() and rand()
#include <ctime> // Gets the computer's time
#include <iostream>
using namespace std;
int main()
{
// Variables
int u1 = 0;
int c1 = 0;
int counter = 0;
int board[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
// introduce the game, explain the rules
//Beginning of main While loop for game
while (counter <= 9) {
//Display board:
for (int i=0; i<9; i++) {
if (i == 2 || i == 5){
cout << endl;
}
cout << board[i];
}
//User input:
cout << "Please enter a spot on the board: \n";
cin >> u1;
while (board[u1] == 1 && board[u1] == 2) {
cout << "Please enter an non-taken spot on the board\n";
cin >> u1;
}//End of minor while loop 1
//Conditions for user input:

if (u1 == 1){
board[0] = 1;
} else if (u1 == 2) {
board[1] = 1;
} else if (u1 == 3) {
board[2] = 1;
}else if (u1 == 4) {
board[3] = 1;
}else if (u1 == 5) {
board[4] = 1;
}else if (u1 == 6) {
board[5] = 1;
}else if (u1 == 7) {
board[6] = 1;
}else if (u1 == 8) {
board[7] = 1;
}else if (u1 == 9) {
board[8] = 1;
} else {
cout << "That is not a valid space. The computer goes first.\n";
}

//Computer input:
//Random number generator:
srand( static_cast<unsigned int>(time(0)));
c1 = rand() % 10;
while (board[c1] == 1 && board[c1] == 2) {
c1 = rand() % 10;
} //End of minor while loop 3
board[c1] = 2;
//Conditions for computer input:
if (c1 == 1){
board[0] = 1;
} else if (c1 == 2) {
board[1] = 1;
} else if (c1 == 3) {
board[2] = 1;
}else if (c1 == 4) {
board[3] = 1;
}else if (c1 == 5) {
board[4] = 1;
}else if (c1 == 6) {
board[5] = 1;
}else if (c1 == 7) {
board[6] = 1;
}else if (c1 == 8) {
board[7] = 1;
}else if (c1 == 9) {
board[8] = 1;
}
// Conditions for when the game is won/lost:
//Diagonal negative slope:
if (board[0] == board[4] && board[8] == board[4] && board[8] == board[0]) {
// -User wins
if (board[0] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[0] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}

}
//Diagonal positive slope:
else if (board[2] == board[4] && board[6] == board[4] && board[6] == board[2]) {
// -User wins
if (board[2] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[2] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}
}

//straight down(right):
else if (board[2] == board[5] && board[2] == board[8] && board[5] == board[8]) {
// -User wins
if (board[2] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[2] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}

}
}
//straight down(middle):
if (board[1] == board[4] && board[7] == board[1] && board[7] == board[4]) {
// -User wins
if (board[1] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[1] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}

}
}
//straight down(Left):
if (board[0] == board[3] && board[6] == board[0] && board[6] == board[3]) {
// -User wins
if (board[0] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[0] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}

}
}
//straight across(top):
if (board[0] == board[1] && board[2] == board[1] && board[2] == board[0]) {
// -User wins
if (board[0] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[0] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}

}
}
//straight across(middle):
if (board[3] == board[4] && board[5] == board[3] && board[5] == board[4]) {
// -User wins
if (board[3] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[3] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}

}
}
//straight across(bottom):
if (board[6] == board[7] && board[8] == board[6] && board[8] == board[7]) {
// -User wins
if (board[6] == 1) {
cout << "You win! Nice job!\n";
counter = 9;
}
// -Computer wins
else if (board[6] == 2) {
cout << "You lost! Too bad!\n";
counter = 9;
} else {

}

}
}
counter++

}//End of main while loop
return 0;
}//End of function main

+1 vote
answered Sep 20, 2021 by (53,530 points)

You should have used an else instead of } above lines

• //straight down(middle):
• //straight down(Left):
• //straight across(top):
• //straight across(middle):
• //straight across(bottom):
commented Sep 20, 2021 by (110 points)
edited Sep 21, 2021 by Ibrahim Abdul-Malik
Thank you! @Peter Minarik It's working now :D