Compilation error: "expected primary-expression before "else" "
Author |
Message |
Al_
|
Posted: Tue Jun 23, 2009 10:05 pm Post subject: Compilation error: "expected primary-expression before "else" " |
|
|
hi. I decided to take another approach to my tic tac toe game. The code is as follows:
code: | #include<iostream>
#include<string>
void fIntroDrawn();
void fDrawBoard();
void fSetPlayerMark();
void fTwoPlayerGame();
int fCheckGameType();
//Global variables Declared
bool bGameOver = false;
bool bValidMove = false;
bool bExit = false;
char cMark;
char cSquare1 = '1';
char cSquare2 = '2';
char cSquare3 = '3';
char cSquare4 = '4';
char cSquare5 = '5';
char cSquare6 = '6';
char cSquare7 = '7';
char cSquare8 = '8';
char cSquare9 = '9';
std::string sReplay;
std::string sExitVerify;
std::string sWait;
std::string sSquareID;
std::string sLeaveGame;
int iPlayerTurn = 1;
std::string sGameType;
int iGameType;
int main() {
// Intro Drawn
fIntroDrawn();
//Check Game Type
std::cout<<"\n"<<"\n"<<" What type of game would you like to play:"<<"\n"<<"\n"<<
" Note: There is only one option available for this version. "<<"\n"<<"\n"<<" Press 1 for a two player game"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
//Choose Game Type
do{
std::cin>>sGameType;
std::cout<<"\n"<<"\n";
if (sGameType == "1"){
std::cout<<"\n";
bValidMove = true;
iGameType = 1;
}
else if (sGameType == "2"){
std::cout<<"\n";
bValidMove = true;
iGameType = 2;
}
else if (sGameType == "exit" || sGameType == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
}
else {
std::cout<<" Sorry. InInvalid input. Please try again:"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
bValidMove = false;
}
}while (bValidMove == false);
if (iGameType == 1){
fTwoPlayerGame();
}
std::cout<<"\n"<<"\n"<<"\n"<<" Thank Your For Playing My Tic Tac Toe Game"<<"\n"<<"\n"<<" Please type in my name followed by the return key to exit: ";
//Exit Trick Script
if (bExit != true){
std::cin>>sWait;
do{
if ((sWait != "Al") && (sWait != "AL") && (sWait != "aL") && (sWait != "al") && (sWait != "exit") && (sWait != "Exit")){
std::cout<<"\n"<<"\n"<<" :O Thats not my name!!! Im insulted! Try again: ";
std::cin>>sWait;
}
}while ((sWait != "Al") && (sWait != "AL") && (sWait != "aL") && (sWait != "al") && (sWait != "exit") && (sWait != "Exit"));
}
}
//FUNCTIONS
void fIntroDrawn(){
std::cout<<
" ______ _ _ _ _ _ ____ ____ "<<"\n"<<
" | __ \\ | | | | | | | | | | | | |__ |"<<"\n"<<
" | | \\ | | | | | | | | |_| | | {} | / / "<<"\n"<<
" | | | | | | | | | | | _ | / __ \\ / / "<<"\n"<<
" | |__/ | | \\_/ | | |___ | | | | | / \\ | / /__ "<<"\n"<<
" |______/ \\_____/ |_____| |_| |_| /__/ \\__\\ |_____|"<<"\n"<<"\n"<<
" __________________________________________________________"<<"\n"<<
" _____________________________________________________"<<"\n"<<"\n"<<"\n"<<"\n"<<
" <<<PRODUCTIONS PRESENTS>>> "<<"\n"<<"\n"<<
" A GAME BY AL DUMBRAVA:"<<"\n"<<"\n"<<
" Al's Tic Tac Toe v 1.0.2"<<"\n"<<"\n"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<" Press Any Key to Continue: ";
std::cin.get();
std::cout<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n";
std::cout<<"\n"<<
" **************************************************************************"<<"\n"<<
" * *"<<"\n"<<
" * Hi. My Name is Al And Welcome To v 1.0.2 of My Console Based Tic *"<<"\n"<<
" * Tac Toe Game. This Tic Tac Toe Game Is Like Any Other Except: *"<<"\n"<<
" * *"<<"\n"<<
" * 1) Its Console Based :P *"<<"\n"<<
" * 2) Player 1 Is Always 'X' and Player 2 Is Always 'O' and... *"<<"\n"<<
" * 3) At the moment it is only a two player game *"<<"\n"<<
" * *"<<"\n"<<
" * This program is written in C++. Some of the previous bugs have *"<<"\n"<<
" * been eliminated from this verision. You can exit the game at any time *"<<"\n"<<
" * by typing in: \"exit\". *"<<"\n"<<
" * *"<<"\n"<<
" * Press any key to continue to the game and remember: HAVE FUN! :P *"<<"\n"<<
" * *"<<"\n"<<
" ************************************************************************** "<<"\n"<<"\n"<<"\n"<<"\n"<<"\n";
std::cin.get();
}
void fDrawBoard(){
std::cout<<"\n"<<" Board:"<<"\n"<<"\n";
std::cout<<" "<<cSquare7<<"|"<<cSquare8<<"|"<<cSquare9<<"\n"<<" "<<"-+-+-"<<"\n"
<<" "<<cSquare4<<"|"<<cSquare5<<"|"<<cSquare6<<"\n"<<" "<<"-+-+-"<<"\n"
<<" "<<cSquare1<<"|"<<cSquare2<<"|"<<cSquare3<<"\n"<<"\n";
}
void fSetPlayerMark(){
if (iPlayerTurn == 1){
cMark = 'X';
}
else {
cMark = 'O';
}
}
void fTwoPlayerGame(){
//TWO PLAYER GAME
//Drawing the board & setting the player mark
while (bGameOver == false){
fDrawBoard();
fSetPlayerMark();
//Prompt for Player to move
std::cout<<" Player "<<iPlayerTurn<<" please select the number of the square you wish to mark: ";
std::cin>>sSquareID;
//Check if move is valid
if (sSquareID == "1" && cSquare1 == '1'){
bValidMove = true;
cSquare1 = cMark;
}
else if (sSquareID == "2" && cSquare2 == '2'){
bValidMove = true;
cSquare2 = cMark;
}
else if (sSquareID == "3" && cSquare3 == '3'){
bValidMove = true;
cSquare3 = cMark;
}
else if (sSquareID == "4" && cSquare4 == '4'){
bValidMove = true;
cSquare4 = cMark;
}
else if (sSquareID == "5" && cSquare5 == '5'){
bValidMove = true;
cSquare5 = cMark;
}
else if (sSquareID == "6" && cSquare6 == '6'){
bValidMove = true;
cSquare6 = cMark;
}
else if (sSquareID == "7" && cSquare7 == '7'){
bValidMove = true;
cSquare7 = cMark;
}
else if (sSquareID == "8" && cSquare8 == '8'){
bValidMove = true;
cSquare8 = cMark;
}
else if (sSquareID == "9" && cSquare9 == '9'){
bValidMove = true;
cSquare9 = cMark;
}
else if (sSquareID == "exit" || sSquareID == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
}
else {
bValidMove = false;
}
do{
if (bValidMove == false){
std::cout<<"\n"<<"\n"<<" Player "<<iPlayerTurn<<" has made an invalid move. Please move again: ";
std::cin>>sSquareID;
//Check if move is valid
if (sSquareID == "1" && cSquare1 == '1'){
bValidMove = true;
cSquare1 = cMark;
}
else if (sSquareID == "2" && cSquare2 == '2'){
bValidMove = true;
cSquare2 = cMark;
}
else if (sSquareID == "3" && cSquare3 == '3'){
bValidMove = true;
cSquare3 = cMark;
}
else if (sSquareID == "4" && cSquare4 == '4'){
bValidMove = true;
cSquare4 = cMark;
}
else if (sSquareID == "5" && cSquare5 == '5'){
bValidMove = true;
cSquare5 = cMark;
}
else if (sSquareID == "6" && cSquare6 == '6'){
bValidMove = true;
cSquare6 = cMark;
}
else if (sSquareID == "7" && cSquare7 == '7'){
bValidMove = true;
cSquare7 = cMark;
}
else if (sSquareID == "8" && cSquare8 == '8'){
bValidMove = true;
cSquare8 = cMark;
}
else if (sSquareID == "9" && cSquare9 == '9'){
bValidMove = true;
cSquare9 = cMark;
}
else if (sSquareID == "exit" || sSquareID == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
}
else {
bValidMove = false;
}
}
if (bValidMove == true){
std::cout<<"\n"<<"\n"<<"\n";
//Checks for Possible wins
//Horizonatal and Verticle Wins Through Square 1
if (cSquare1 != '1' && cSquare2 == cSquare1 && cSquare3 == cSquare1){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
else if (cSquare1 != '1' && cSquare4 == cSquare1 && cSquare7 == cSquare1){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
//Horizonatal and Verticle Wins Through Square 9
else if (cSquare9 != '9' && cSquare7 == cSquare9 && cSquare8 == cSquare9){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
else if (cSquare9 != '9' && cSquare3 == cSquare9 && cSquare6 == cSquare9){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
//Horizonatal, Verticle, and Diagonal Wins Through Square 5
else if (cSquare5 != '5' && cSquare1 == cSquare5 && cSquare9 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
else if (cSquare5 != '5' && cSquare3 == cSquare5 && cSquare7 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
else if (cSquare5 != '5' && cSquare4 == cSquare5 && cSquare6 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
else if (cSquare5 != '5' && cSquare2 == cSquare5 && cSquare8 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
else if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
cSquare7 != '7' && cSquare1 != '8' && cSquare1 != '8' && cSquare9 != '9'){
std::cout<<" Sorry its a cats game"<<"\n";
bGameOver = true;
}
}
}while(bValidMove == false);
//Alternate Player turn
if (bGameOver == false && iPlayerTurn == 1){
iPlayerTurn = 2;}
else if (bGameOver == false && iPlayerTurn == 2){
iPlayerTurn = 1;}
//Replay
if (bGameOver == true && bExit != true ){
do{
std::cout<<"\n"<<" Would you like to play again? (y/n) ";
std::cin>>sReplay;
if (sReplay == "y" || sReplay == "Y"){
bGameOver = false;
cSquare1 = '1';
cSquare2 = '2';
cSquare3 = '3';
cSquare4 = '4';
cSquare5 = '5';
cSquare6 = '6';
cSquare7 = '7';
cSquare8 = '8';
cSquare9 = '9';}
else if (sReplay == "n" || sReplay == "N"){
do{
std::cout<<"\n"<<" Would you like to return to the main menu? (y/n) ";
std::cin>>sLeaveGame;
if (sLeaveGame == "y" || sLeaveGame == "Y"){
return;}
else if (sLeaveGame == "n" || sLeaveGame == "N"){
do{
std::cout<<"\n"<<" Are you sure you wish to exit? (y/n) ";
std::cin>>sExitVerify;
if (sExitVerify == "n" || sExitVerify == "N"){
bGameOver = false;
sReplay = "m";}
else if (sExitVerify == "y" || sExitVerify == "Y"){
bGameOver = true;}
else if (sExitVerify == "exit" || sExitVerify == "Exit"){
bGameOver = true;
bExit = true;
return;}
else {
std::cout<<"\n"<<" Invalid input."<<"\n";}
}while (sExitVerify != "n" && sExitVerify != "Y" && sExitVerify != "y" && sExitVerify != "N" && sExitVerify != "exit" && sExitVerify != "Exit");
}
else if (sReplay == "exit" || sReplay == "Exit"){
bGameOver = true;
bExit = true;
return;}
else {
std::cout<<"\n"<<" Invalid input."<<"\n";}
}
while(sLeaveGame != "n" && sLeaveGame != "N" && sLeaveGame != "y" && sLeaveGame != "Y" && sLeaveGame != "exit" && sLeaveGame != "Exit");
else if (sReplay == "exit" || sReplay == "Exit"){ ******
bGameOver = true;
bExit = true;
return;
}
else { *******
std::cout<<"\n"<<" Invalid input."<<"\n";
}
}while (sReplay != "n" && sReplay != "N" && sReplay != "y" && sReplay != "Y" && sReplay != "exit" && sReplay != "Exit");
}
}
}
|
I am getting the following error upon compiling: "expected primary-expression before "else" "
This error appears at the lines with the little star things. : )
Note: i intend to create more options for the main menu, for example: to play with an AI. This will be done later on.
Im not sure what to do because im not sure what its asking me for. Thx in advance for any ideas you may have.
~Al
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Silinter
|
Posted: Tue Jun 23, 2009 11:15 pm Post subject: Re: Compilation error: "expected primary-expression before "else" " |
|
|
Alright, after semi-proper indentation, totaling ~10 minutes, the problem was that you didn't put a '}' before the 'else' that gave you the problem.
Also, look up for loops, arrays (specifically vectors, but look up normal C arrays as well), and if/else semantics. For example, if you have a statement that's a single line long, you don't need the '{ }' for if/else/do/while/for loops.
Also, if (something==false) is the same as if (!something). Yet another note, for simplifying answers, make a function that takes in a char/string and returns it lower/upper case. This will help your if statements to a great extent.
Another point, try putting 'using namespace std;' after your includes, so you don't have to keep doing std::cout and std::cin, but can simply do cout and cin.
Another point, std::cout << "\n" << "\n" << "\n" is the same as std::cout << "\n\n\n", but takes less writing and shift pressing.
Here's the code, just added the '}' before that else statement.
code: |
#include<iostream>
#include<string>
void fIntroDrawn();
void fDrawBoard();
void fSetPlayerMark();
void fTwoPlayerGame();
int fCheckGameType();
//Global variables Declared
bool bGameOver = false;
bool bValidMove = false;
bool bExit = false;
char cMark;
char cSquare1 = '1';
char cSquare2 = '2';
char cSquare3 = '3';
char cSquare4 = '4';
char cSquare5 = '5';
char cSquare6 = '6';
char cSquare7 = '7';
char cSquare8 = '8';
char cSquare9 = '9';
std::string sReplay;
std::string sExitVerify;
std::string sWait;
std::string sSquareID;
std::string sLeaveGame;
int iPlayerTurn = 1;
std::string sGameType;
int iGameType;
int main() {
// Intro Drawn
fIntroDrawn();
//Check Game Type
std::cout<<"\n"<<"\n"<<" What type of game would you like to play:"<<"\n"<<"\n"<<
" Note: There is only one option available for this version. "<<"\n"<<"\n"<<" Press 1 for a two player game"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
//Choose Game Type
do{
std::cin>>sGameType;
std::cout<<"\n"<<"\n";
if (sGameType == "1"){
std::cout<<"\n";
bValidMove = true;
iGameType = 1;
} else if (sGameType == "2"){
std::cout<<"\n";
bValidMove = true;
iGameType = 2;
} else if (sGameType == "exit" || sGameType == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
} else {
std::cout<<" Sorry. InInvalid input. Please try again:"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
bValidMove = false;
}
} while (bValidMove == false);
if (iGameType == 1) fTwoPlayerGame();
std::cout<<"\n\n\n Thank Your For Playing My Tic Tac Toe Game\n\n Please type in my name followed by the return key to exit: ";
//Exit Trick Script
if (bExit != true){
std::cin>>sWait;
do {
if ((sWait != "Al") && (sWait != "AL") && (sWait != "aL") && (sWait != "al") && (sWait != "exit") && (sWait != "Exit")){
std::cout<<"\n"<<"\n"<<" :O Thats not my name!!! Im insulted! Try again: ";
std::cin>>sWait;
}
} while ((sWait != "Al") && (sWait != "AL") && (sWait != "aL") && (sWait != "al") && (sWait != "exit") && (sWait != "Exit"));
}
}
//FUNCTIONS
void fIntroDrawn(){
std::cout<<
" ______ _ _ _ _ _ ____ ____ "<<"\n"<<
" | __ \\ | | | | | | | | | | | | |__ |"<<"\n"<<
" | | \\ | | | | | | | | |_| | | {} | / / "<<"\n"<<
" | | | | | | | | | | | _ | / __ \\ / / "<<"\n"<<
" | |__/ | | \\_/ | | |___ | | | | | / \\ | / /__ "<<"\n"<<
" |______/ \\_____/ |_____| |_| |_| /__/ \\__\\ |_____|"<<"\n"<<"\n"<<
" __________________________________________________________"<<"\n"<<
" _____________________________________________________"<<"\n"<<"\n"<<"\n"<<"\n"<<
" <<<PRODUCTIONS PRESENTS>>> "<<"\n"<<"\n"<<
" A GAME BY AL DUMBRAVA:"<<"\n"<<"\n"<<
" Al's Tic Tac Toe v 1.0.2"<<"\n"<<"\n"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<" Press Any Key to Continue: ";
std::cin.get();
std::cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n";
std::cout<<
" **************************************************************************\n"<<
" * *\n"<<
" * Hi. My Name is Al And Welcome To v 1.0.2 of My Console Based Tic *\n"<<
" * Tac Toe Game. This Tic Tac Toe Game Is Like Any Other Except: *\n"<<
" * *\n"<<
" * 1) Its Console Based :P *\n"<<
" * 2) Player 1 Is Always 'X' and Player 2 Is Always 'O' and... *\n"<<
" * 3) At the moment it is only a two player game *\n"<<
" * *\n"<<
" * This program is written in C++. Some of the previous bugs have *\n"<<
" * been eliminated from this verision. You can exit the game at any time *\n"<<
" * by typing in: \"exit\". *\n"<<
" * *\n"<<
" * Press any key to continue to the game and remember: HAVE FUN! :P *\n"<<
" * *\n"<<
" **************************************************************************\n\n\n\n\n";
std::cin.get();
}
void fDrawBoard(){
std::cout<<"\n"<<" Board:"<<"\n"<<"\n";
std::cout<<" "<<cSquare7<<"|"<<cSquare8<<"|"<<cSquare9<<"\n"<<" "<<"-+-+-"<<"\n"
<<" "<<cSquare4<<"|"<<cSquare5<<"|"<<cSquare6<<"\n"<<" "<<"-+-+-"<<"\n"
<<" "<<cSquare1<<"|"<<cSquare2<<"|"<<cSquare3<<"\n"<<"\n";
}
void fSetPlayerMark(){
if (iPlayerTurn == 1){
cMark = 'X';
} else {
cMark = 'O';
}
}
void fTwoPlayerGame() {
//TWO PLAYER GAME
//Drawing the board & setting the player mark
while (bGameOver == false){
fDrawBoard();
fSetPlayerMark();
//Prompt for Player to move
std::cout<<" Player "<<iPlayerTurn<<" please select the number of the square you wish to mark: ";
std::cin >> sSquareID;
//Check if move is valid
if (sSquareID == "1" && cSquare1 == '1'){
bValidMove = true;
cSquare1 = cMark;
} else if (sSquareID == "2" && cSquare2 == '2'){
bValidMove = true;
cSquare2 = cMark;
} else if (sSquareID == "3" && cSquare3 == '3'){
bValidMove = true;
cSquare3 = cMark;
} else if (sSquareID == "4" && cSquare4 == '4'){
bValidMove = true;
cSquare4 = cMark;
} else if (sSquareID == "5" && cSquare5 == '5'){
bValidMove = true;
cSquare5 = cMark;
} else if (sSquareID == "6" && cSquare6 == '6'){
bValidMove = true;
cSquare6 = cMark;
} else if (sSquareID == "7" && cSquare7 == '7'){
bValidMove = true;
cSquare7 = cMark;
} else if (sSquareID == "8" && cSquare8 == '8'){
bValidMove = true;
cSquare8 = cMark;
} else if (sSquareID == "9" && cSquare9 == '9'){
bValidMove = true;
cSquare9 = cMark;
} else if (sSquareID == "exit" || sSquareID == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
} else bValidMove = false;
do {
if (bValidMove == false) {
std::cout<<"\n"<<"\n"<<" Player "<<iPlayerTurn<<" has made an invalid move. Please move again: ";
std::cin>>sSquareID;
//Check if move is valid
if (sSquareID == "1" && cSquare1 == '1'){
bValidMove = true;
cSquare1 = cMark;
} else if (sSquareID == "2" && cSquare2 == '2'){
bValidMove = true;
cSquare2 = cMark;
} else if (sSquareID == "3" && cSquare3 == '3'){
bValidMove = true;
cSquare3 = cMark;
} else if (sSquareID == "4" && cSquare4 == '4'){
bValidMove = true;
cSquare4 = cMark;
} else if (sSquareID == "5" && cSquare5 == '5'){
bValidMove = true;
cSquare5 = cMark;
} else if (sSquareID == "6" && cSquare6 == '6'){
bValidMove = true;
cSquare6 = cMark;
} else if (sSquareID == "7" && cSquare7 == '7'){
bValidMove = true;
cSquare7 = cMark;
} else if (sSquareID == "8" && cSquare8 == '8'){
bValidMove = true;
cSquare8 = cMark;
} else if (sSquareID == "9" && cSquare9 == '9'){
bValidMove = true;
cSquare9 = cMark;
} else if (sSquareID == "exit" || sSquareID == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
} else {
bValidMove = false;
}
}
if (bValidMove == true){
std::cout<<"\n\n\n";
//Checks for Possible wins
//Horizonatal and Verticle Wins Through Square 1
if (cSquare1 != '1' && cSquare2 == cSquare1 && cSquare3 == cSquare1){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare1 != '1' && cSquare4 == cSquare1 && cSquare7 == cSquare1){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
//Horizonatal and Verticle Wins Through Square 9
else if (cSquare9 != '9' && cSquare7 == cSquare9 && cSquare8 == cSquare9){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare9 != '9' && cSquare3 == cSquare9 && cSquare6 == cSquare9){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
//Horizonatal, Verticle, and Diagonal Wins Through Square 5
else if (cSquare5 != '5' && cSquare1 == cSquare5 && cSquare9 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare5 != '5' && cSquare3 == cSquare5 && cSquare7 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare5 != '5' && cSquare4 == cSquare5 && cSquare6 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare5 != '5' && cSquare2 == cSquare5 && cSquare8 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
cSquare7 != '7' && cSquare1 != '8' && cSquare1 != '8' && cSquare9 != '9'){
std::cout<<" Sorry its a cats game"<<"\n";
bGameOver = true;
}
}
} while(bValidMove == false);
//Alternate Player turn
if (bGameOver == false && iPlayerTurn == 1) iPlayerTurn = 2;
else if (bGameOver == false && iPlayerTurn == 2) iPlayerTurn = 1;
//Replay
if (bGameOver == true && bExit != true ){
do{
std::cout<<"\n"<<" Would you like to play again? (y/n) ";
std::cin>>sReplay;
if (sReplay == "y" || sReplay == "Y"){
bGameOver = false;
cSquare1 = '1';
cSquare2 = '2';
cSquare3 = '3';
cSquare4 = '4';
cSquare5 = '5';
cSquare6 = '6';
cSquare7 = '7';
cSquare8 = '8';
cSquare9 = '9';}
else if (sReplay == "n" || sReplay == "N"){
do{
std::cout<<"\n"<<" Would you like to return to the main menu? (y/n) ";
std::cin>>sLeaveGame;
if (sLeaveGame == "y" || sLeaveGame == "Y") return;
else if (sLeaveGame == "n" || sLeaveGame == "N"){
do{
std::cout<<"\n"<<" Are you sure you wish to exit? (y/n) ";
std::cin>>sExitVerify;
if (sExitVerify == "n" || sExitVerify == "N"){
bGameOver = false;
sReplay = "m";}
else if (sExitVerify == "y" || sExitVerify == "Y") bGameOver = true;
else if (sExitVerify == "exit" || sExitVerify == "Exit"){
bGameOver = true;
bExit = true;
return;
} else std::cout<<"\n"<<" Invalid input."<<"\n";
} while (sExitVerify != "n" && sExitVerify != "Y" && sExitVerify != "y" && sExitVerify != "N" && sExitVerify != "exit" && sExitVerify != "Exit");
} else if (sReplay == "exit" || sReplay == "Exit"){
bGameOver = true;
bExit = true;
return;
} else std::cout<<"\n"<<" Invalid input."<<"\n";
} while(sLeaveGame != "n" && sLeaveGame != "N" && sLeaveGame != "y" && sLeaveGame != "Y" && sLeaveGame != "exit" && sLeaveGame != "Exit");
} else if (sReplay == "exit" || sReplay == "Exit"){
bGameOver = true;
bExit = true;
return;
} else std::cout<<"\n"<<" Invalid input."<<"\n";
}while (sReplay != "n" && sReplay != "N" && sReplay != "y" && sReplay != "Y" && sReplay != "exit" && sReplay != "Exit");
}
}
}
|
|
|
|
|
|
|
Al_
|
Posted: Wed Jun 24, 2009 10:02 am Post subject: Re: Compilation error: "expected primary-expression before "else" " |
|
|
First off, Thx for spotting that.
Secondly i have another problem : (
in this segment of code:
code: |
...
int main() {
// Intro Drawn
fIntroDrawn();
do{
//Check Game Type
std::cout<<"\n"<<"\n"<<" What type of game would you like to play:"<<"\n"<<"\n"<<
" Note: There is only one option available for this version. "<<"\n"<<"\n"<<" Press 1 for a two player game"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
//Choose Game Type
do{
std::cin>>sGameType;
std::cout<<"\n"<<"\n";
if (sGameType == "1"){
std::cout<<"\n";
bValidMove = true;
iGameType = 1;}
else if (sGameType == "2"){
std::cout<<"\n";
bValidMove = true;
iGameType = 2;}
else if (sGameType == "exit" || sGameType == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;}
else { //Invalid input
std::cout<<" Sorry. InInvalid input. Please try again:"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
bValidMove = false;}
} while (bValidMove == false);
if (iGameType == 1) {fTwoPlayerGame();}
}while(sExitVerify != "n" && sExitVerify != "Y" && sExitVerify != "y" && sExitVerify != "N" && sExitVerify != "exit" && sExitVerify != "Exit");
std::cout<<"\n\n\n Thank Your For Playing My Tic Tac Toe Game\n\n Please type in my name followed by the return key to exit: ";
//Exit Trick Script
...
|
im thrown into an endless loop for some reason. I start the app. Press enter twice to get past the intro. then i press 1. and the two player game starts (fTwoPlayerGame:void). 1 play a game out. Player 1 wins. Then im prompted to play again. I reply "n". Then it asks me if i wish to return to the main menu. i reply y to this. then the following is typed Quote: what type of game would you like to play: ("\n")("\t") Note: There is only one option available for this version. ("\n")("\t")("\t")Press 1 for a two player game
Then i press 1. Then the endless loop starts. Instead of starting the game, the above quote is repeated, asking me for the game i wish to play. Plz help. Thx again for any help. ~Al
Description: |
|
Download |
Filename: |
Tic Tac Toe v1.0.2.cpp |
Filesize: |
12.45 KB |
Downloaded: |
165 Time(s) |
|
|
|
|
|
|
Silinter
|
Posted: Wed Jun 24, 2009 1:56 pm Post subject: RE:Compilation error: "expected primary-expression before "else" " |
|
|
Well, the version of your game posted above is not the version that you are having the problem with. I can't repeat the problem that you are having, and there is no 'fIntroDrawn()' function in it either; and no double while loop. Post the correct version and I'll see what I can do.
|
|
|
|
|
|
Al_
|
Posted: Wed Jun 24, 2009 3:01 pm Post subject: Re: Compilation error: "expected primary-expression before "else" " |
|
|
opps. my bad : )
srry about that
Description: |
|
Download |
Filename: |
Tic Tac Toe v1.1.0.cpp |
Filesize: |
13.7 KB |
Downloaded: |
682 Time(s) |
|
|
|
|
|
|
Silinter
|
Posted: Wed Jun 24, 2009 3:38 pm Post subject: RE:Compilation error: "expected primary-expression before "else" " |
|
|
Alright the infinite loop is caused when your prompt the play for the variable 'sLeaveGame'. It gets filled with the answer, and the functions exits. However, the prompt loop only exists if 'sExitVerify' variable is either y/Y/n/N/exit/Exit; and since you don't prompt the user for the 'sExitVerify' variable, rather the 'sLeaveGame' variable, it loops forever.
Possible fixes for the problem include extending the while loop exit conditions to include the 'sLeaveGame' variable as well as the 'sExitVerify' variable.
And when you're satisfied, I strongly recommend rewriting it to include for loops and arrays. If you plan to work with C++ longer than just for the current project, you'd need to know it. It also makes your life LOADS easier (that's why I'm suggesting it, not because it's proper coding practice; although it is).
|
|
|
|
|
|
Al_
|
Posted: Wed Jun 24, 2009 3:51 pm Post subject: RE:Compilation error: "expected primary-expression before "else" " |
|
|
Ok. for the while loop: I only want it to exit if sExitVerify is either y/Y/n/N/exit/Exit. What i dont understand is why the function fTwoPlayerGame isnt restarted.
It seems as if the if statement:
code: | if (iGameType == 1) {fTwoPlayerGame();} |
is ignored. shouldn't the two player game restart the two player game if my input to the quote mentioned above is the #1?
|
|
|
|
|
|
Silinter
|
Posted: Wed Jun 24, 2009 4:44 pm Post subject: RE:Compilation error: "expected primary-expression before "else" " |
|
|
It should, and it does.
However, the first while loop in your 'fTwoPlayerGame()' functions is 'while (bGameOver == false)', which works fine the first time around, but is never reset to false the second time around; so it goes around the first time, sets it to true when one player wins, exits, and is never false again, skipping the contents of your function.
It's only reset to false when they want to play again or wish not to, don't want to return to the main menu and wish not to exit.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Al_
|
Posted: Wed Jun 24, 2009 5:56 pm Post subject: RE:Compilation error: "expected primary-expression before "else" " |
|
|
No. that didnt work. its still looping.
|
|
|
|
|
|
Silinter
|
Posted: Wed Jun 24, 2009 6:05 pm Post subject: RE:Compilation error: "expected primary-expression before "else" " |
|
|
Alright, post the latest source and I'll see what I can do. I'll fix it up an comment it so u can see what I changed.
|
|
|
|
|
|
Al_
|
Posted: Wed Jun 24, 2009 9:39 pm Post subject: Re: Compilation error: "expected primary-expression before "else" " |
|
|
ok. here it is:
code: |
#include<iostream>
#include<string>
void fIntroDrawn();
void fDrawBoard();
void fSetPlayerMark();
void fTwoPlayerGame();
//Global variables Declared
bool bGameOver = false;
bool bValidMove = false;
bool bExit = false;
char cMark;
char cSquare1 = '1';
char cSquare2 = '2';
char cSquare3 = '3';
char cSquare4 = '4';
char cSquare5 = '5';
char cSquare6 = '6';
char cSquare7 = '7';
char cSquare8 = '8';
char cSquare9 = '9';
std::string sReplay;
std::string sExitVerify;
std::string sWait;
std::string sSquareID;
std::string sLeaveGame;
int iPlayerTurn = 1;
std::string sGameType;
int iGameType;
int main() {
// Intro Drawn
fIntroDrawn();
do{
//Check Game Type
std::cout<<"\n"<<"\n"<<" What type of game would you like to play:"<<"\n"<<"\n"<<
" Note: There is only one option available for this version. "<<"\n"<<"\n"<<" Press 1 for a two player game"<<"\n"<<"\n"<<" Press 3 to exit game"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
//Choose Game Type
do{
std::cin>>sGameType;
std::cout<<"\n"<<"\n";
if (sGameType == "1"){
std::cout<<"\n";
bValidMove = true;
iGameType = 1;}
else if (sGameType == "2"){
std::cout<<"\n";
bValidMove = true;
iGameType = 2;}
else if (sGameType == "3"){
std::cout<<"\n";
bValidMove = true;
sExitVerify = "y";}
else if (sGameType == "exit" || sGameType == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;}
else { //Invalid input
std::cout<<" Sorry. InInvalid input. Please try again:"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<"\t";
bValidMove = false;}
} while (bValidMove == false);
if (iGameType == 1) {
fTwoPlayerGame();
bool bGameOver = false;}
}while(sExitVerify != "n" && sExitVerify != "Y" && sExitVerify != "y" && sExitVerify != "N" && sExitVerify != "exit" && sExitVerify != "Exit");
std::cout<<"\n\n\n Thank Your For Playing My Tic Tac Toe Game\n\n Please type in my name followed by the return key to exit: ";
//Exit Trick Script
if (bExit != true){
std::cin>>sWait;
do {
if ((sWait != "Al") && (sWait != "AL") && (sWait != "aL") && (sWait != "al") && (sWait != "exit") && (sWait != "Exit")){
std::cout<<"\n"<<"\n"<<" :O Thats not my name!!! Im insulted! Try again: ";
std::cin>>sWait;
}
} while ((sWait != "Al") && (sWait != "AL") && (sWait != "aL") && (sWait != "al") && (sWait != "exit") && (sWait != "Exit"));
}
}
//FUNCTIONS
void fIntroDrawn(){
std::cout<<
" ______ _ _ _ _ _ ____ ____ "<<"\n"<<
" | __ \\ | | | | | | | | | | | | |__ |"<<"\n"<<
" | | \\ | | | | | | | | |_| | | {} | / / "<<"\n"<<
" | | | | | | | | | | | _ | / __ \\ / / "<<"\n"<<
" | |__/ | | \\_/ | | |___ | | | | | / \\ | / /__ "<<"\n"<<
" |______/ \\_____/ |_____| |_| |_| /__/ \\__\\ |_____|"<<"\n"<<"\n"<<
" __________________________________________________________"<<"\n"<<
" _____________________________________________________"<<"\n"<<"\n"<<"\n"<<"\n"<<
" <<<PRODUCTIONS PRESENTS>>> "<<"\n"<<"\n"<<
" A GAME BY AL DUMBRAVA:"<<"\n"<<"\n"<<
" Al's Tic Tac Toe v 1.0.2"<<"\n"<<"\n"<<"\n"<<"\n"<<"\t"<<"\t"<<"\t"<<" Press Any Key to Continue: ";
std::cin.get();
std::cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n";
std::cout<<
" **************************************************************************\n"<<
" * *\n"<<
" * Hi. My Name is Al And Welcome To v 1.0.2 of My Console Based Tic *\n"<<
" * Tac Toe Game. This Tic Tac Toe Game Is Like Any Other Except: *\n"<<
" * *\n"<<
" * 1) Its Console Based :P *\n"<<
" * 2) Player 1 Is Always 'X' and Player 2 Is Always 'O' and... *\n"<<
" * 3) At the moment it is only a two player game *\n"<<
" * *\n"<<
" * This program is written in C++. Some of the previous bugs have *\n"<<
" * been eliminated from this verision. You can exit the game at any time *\n"<<
" * by typing in: \"exit\". *\n"<<
" * *\n"<<
" * Press any key to continue to the game and remember: HAVE FUN! :P *\n"<<
" * *\n"<<
" **************************************************************************\n\n\n\n\n";
std::cin.get();
}
void fDrawBoard(){
std::cout<<"\n"<<" Board:"<<"\n"<<"\n";
std::cout<<" "<<cSquare7<<"|"<<cSquare8<<"|"<<cSquare9<<"\n"<<" "<<"-+-+-"<<"\n"
<<" "<<cSquare4<<"|"<<cSquare5<<"|"<<cSquare6<<"\n"<<" "<<"-+-+-"<<"\n"
<<" "<<cSquare1<<"|"<<cSquare2<<"|"<<cSquare3<<"\n"<<"\n";
}
void fSetPlayerMark(){
if (iPlayerTurn == 1){
cMark = 'X';
} else {
cMark = 'O';
}
}
void fTwoPlayerGame() {
//TWO PLAYER GAME
//Drawing the board & setting the player mark
while (bGameOver == false){
fDrawBoard();
fSetPlayerMark();
//Prompt for Player to move
std::cout<<" Player "<<iPlayerTurn<<" please select the number of the square you wish to mark: ";
std::cin >> sSquareID;
//Check if move is valid
if (sSquareID == "1" && cSquare1 == '1'){
bValidMove = true;
cSquare1 = cMark;}
else if (sSquareID == "2" && cSquare2 == '2'){
bValidMove = true;
cSquare2 = cMark;}
else if (sSquareID == "3" && cSquare3 == '3'){
bValidMove = true;
cSquare3 = cMark;}
else if (sSquareID == "4" && cSquare4 == '4'){
bValidMove = true;
cSquare4 = cMark;}
else if (sSquareID == "5" && cSquare5 == '5'){
bValidMove = true;
cSquare5 = cMark;}
else if (sSquareID == "6" && cSquare6 == '6'){
bValidMove = true;
cSquare6 = cMark;}
else if (sSquareID == "7" && cSquare7 == '7'){
bValidMove = true;
cSquare7 = cMark;}
else if (sSquareID == "8" && cSquare8 == '8'){
bValidMove = true;
cSquare8 = cMark;}
else if (sSquareID == "9" && cSquare9 == '9'){
bValidMove = true;
cSquare9 = cMark;}
else if (sSquareID == "exit" || sSquareID == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
return;}
else bValidMove = false;
do {
if (bValidMove == false) {
std::cout<<"\n"<<"\n"<<" Player "<<iPlayerTurn<<" has made an invalid move. Please move again: ";
std::cin>>sSquareID;
//Check if move is valid
if (sSquareID == "1" && cSquare1 == '1'){
bValidMove = true;
cSquare1 = cMark;}
else if (sSquareID == "2" && cSquare2 == '2'){
bValidMove = true;
cSquare2 = cMark;}
else if (sSquareID == "3" && cSquare3 == '3'){
bValidMove = true;
cSquare3 = cMark;}
else if (sSquareID == "4" && cSquare4 == '4'){
bValidMove = true;
cSquare4 = cMark;}
else if (sSquareID == "5" && cSquare5 == '5'){
bValidMove = true;
cSquare5 = cMark;}
else if (sSquareID == "6" && cSquare6 == '6'){
bValidMove = true;
cSquare6 = cMark;}
else if (sSquareID == "7" && cSquare7 == '7'){
bValidMove = true;
cSquare7 = cMark;}
else if (sSquareID == "8" && cSquare8 == '8'){
bValidMove = true;
cSquare8 = cMark;}
else if (sSquareID == "9" && cSquare9 == '9'){
bValidMove = true;
cSquare9 = cMark;}
else if (sSquareID == "exit" || sSquareID == "Exit"){
bGameOver = true;
bExit = true;
bValidMove = true;
return;}
else bValidMove = false;
}
if (bValidMove == true){
std::cout<<"\n\n\n";
//Checks for Possible wins
//Horizonatal and Verticle Wins Through Square 1
if (cSquare1 != '1' && cSquare2 == cSquare1 && cSquare3 == cSquare1){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare1 != '1' && cSquare4 == cSquare1 && cSquare7 == cSquare1){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
//Horizonatal and Verticle Wins Through Square 9
else if (cSquare9 != '9' && cSquare7 == cSquare9 && cSquare8 == cSquare9){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare9 != '9' && cSquare3 == cSquare9 && cSquare6 == cSquare9){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
}
//Horizonatal, Verticle, and Diagonal Wins Through Square 5
else if (cSquare5 != '5' && cSquare1 == cSquare5 && cSquare9 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare5 != '5' && cSquare3 == cSquare5 && cSquare7 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare5 != '5' && cSquare4 == cSquare5 && cSquare6 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare5 != '5' && cSquare2 == cSquare5 && cSquare8 == cSquare5){
std::cout<<" CONGRATULATIONS PLAYER "<<iPlayerTurn<<"!! YOU ARE THE WINNER!!!!!!"<<"\n";
bGameOver = true;
} else if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
cSquare7 != '7' && cSquare1 != '8' && cSquare1 != '8' && cSquare9 != '9'){
std::cout<<" Sorry its a cats game"<<"\n";
bGameOver = true;
}
}
} while(bValidMove == false);
//Alternate Player turn
if (bGameOver == false && iPlayerTurn == 1) iPlayerTurn = 2;
else if (bGameOver == false && iPlayerTurn == 2) iPlayerTurn = 1;
//Replay
if (bGameOver == true && bExit != true ){
do{
std::cout<<"\n"<<" Would you like to play again? (y/n) ";
std::cin>>sReplay;
if (sReplay == "y" || sReplay == "Y"){
bGameOver = false;
cSquare1 = '1';
cSquare2 = '2';
cSquare3 = '3';
cSquare4 = '4';
cSquare5 = '5';
cSquare6 = '6';
cSquare7 = '7';
cSquare8 = '8';
cSquare9 = '9';}
else if (sReplay == "n" || sReplay == "N"){
do{
std::cout<<"\n"<<" Would you like to return to the main menu? (y/n) ";
std::cin>>sLeaveGame;
if (sLeaveGame == "y" || sLeaveGame == "Y") return;
else if (sLeaveGame == "n" || sLeaveGame == "N"){
do{
std::cout<<"\n"<<" Are you sure you wish to exit? (y/n) ";
std::cin>>sExitVerify;
if (sExitVerify == "n" || sExitVerify == "N"){
bGameOver = false;
sReplay = "m";}
else if (sExitVerify == "y" || sExitVerify == "Y") bGameOver = true;
else if (sExitVerify == "exit" || sExitVerify == "Exit"){
bGameOver = true;
bExit = true;
return;
} else std::cout<<"\n"<<" Invalid input."<<"\n";
} while (sExitVerify != "n" && sExitVerify != "Y" && sExitVerify != "y" && sExitVerify != "N" && sExitVerify != "exit" && sExitVerify != "Exit");
}
else if (sReplay == "exit" || sReplay == "Exit"){
bGameOver = true;
bExit = true;
return;}
else std::cout<<"\n"<<" Invalid input."<<"\n";
} while(sLeaveGame != "n" && sLeaveGame != "N" && sLeaveGame != "y" && sLeaveGame != "Y" && sLeaveGame != "exit" && sLeaveGame != "Exit");}
else if (sReplay == "exit" || sReplay == "Exit"){
bGameOver = true;
bExit = true;
return;}
else std::cout<<"\n"<<" Invalid input."<<"\n";
}while (sReplay != "n" && sReplay != "N" && sReplay != "y" && sReplay != "Y" && sReplay != "exit" && sReplay != "Exit");
}
}
}
|
|
|
|
|
|
|
Silinter
|
Posted: Wed Jun 24, 2009 10:11 pm Post subject: Re: Compilation error: "expected primary-expression before "else" " |
|
|
Alright, here is the relatively fixed file, if you consider it runs as intended 'fixed' (although, as my teacher would refer to it, it's aesthetically unappealing)
I used the following in one case just to make it more readable.
code: | cout << "Hello, I'm a text \
literal being printed on \
multiple lines.\n"; |
Description: |
Comments marked with ---------------- |
|
Download |
Filename: |
Al_ tictactoe v1.1.0.cpp |
Filesize: |
13.9 KB |
Downloaded: |
217 Time(s) |
|
|
|
|
|
|
|
|