Am I doing something wrong?

Started by
4 comments, last by Cornstalks 11 years, 7 months ago
So I'm starting to learn me some programming, and I decided to start with C++ as its the most prominent, Ive been watching some tutorials and reading up on some books i found, did a program from a tutorial from Xoax.net (http://www.youtube.c...38&feature=plcp) and I attempted to copy his game (Lesson 9 Tic Tac Toe) to see if I have an understanding of what Im doing (lol) I understand the program, but when i generate this code it comes up w/ an error msg: "The program cannot find the file specified." I've tried looking into it but it seems to be an error w/ Visual C++ Basic (2010) not being able to create a .exe file for the program, I've tried to reinstall multiple times but to no avail, any ideas? Or is it because of an error in my code? I've compared it to the Xoax code and I didnt see anything different...

Heres my code for a tic tac toe game in the console:

[source]#include <iostream>
int main() {
char cSquare1('1'); // Why not int? Char for letters/int for #'s?
char cSquare2('2');
char cSquare3('3');
char cSquare4('4');
char cSquare5('5');
char cSquare6('6');
char cSquare7('7');
char cSquare8('8');
char cSquare9('9');
int iPlayerTurn(1);
bool bGameOver(true);


//Main Game Loop
do {
// Board
std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << std::endl;
//Player Mark 1 or 2
char cPlayerMark;
if (iPlayerTurn == 1) {
cPlayerMark = 'X';
} else {
cPlayerMark = 'O';
}
//Player Move
std::cout << "Player " << iPlayerTurn << "'s Turn" << std::endl;
bool bValidMove;
//needs valid move, if no valid move then loop until valid
do {
char cNextMove;
std::cin >> cNextMove;
bValidMove = true;
if (cNextMove == '1' && cSquare1 == '1') {
cSquare1 = cPlayerMark;
} else if (cNextMove == '2' && cSquare2 == '2') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '3' && cSquare3 == '3') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '4' && cSquare4 == '4') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '5' && cSquare5 == '5') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '6' && cSquare6 == '6') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '7' && cSquare7 == '7') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '8' && cSquare8 == '8') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '9' && cSquare9 == '9') {
cSquare2 = cPlayerMark;
} else {
std::cout << "INVALID GO AGAIN" << std::endl;
bValidMove = false;
}
} while (!bValidMove);
bGameOver = false;
bool bWinGame = true;
// check if game over
if(cSquare1 != '1') {
if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
bGameOver = true;
}
if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
bGameOver == true;
}
}
if(cSquare5 != '5') {
if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
bGameOver == true;
}
if (cSquare2 == cSquare5 && cSquare8 == cSquare5){
bGameOver == true;
}
if (cSquare3 == cSquare5 && cSquare7 == cSquare5){
bGameOver == true;
}
if (cSquare4 == cSquare5 && cSquare6 == cSquare5){
bGameOver == true;
}
}
if(cSquare9 != '1'){
if(cSquare3 == cSquare9 && cSquare6 == cSquare9){
bGameOver == true;
}
if(cSquare7 == cSquare9 && cSquare8 == cSquare9){
bGameOver == true;
}
}
//Check for No Win Condition
if(cSquare1 != '1' && cSquare2 != '2' && cSquare != '3' &&
cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
//Declare Winner
if(bGameOver)
{
if(bWinGame){
std::cout << "Player" << iPlayerMove << " is the victor!!" << std::endl;
}
//Print Endgame Board
std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << std::endl;
std::cout << "Game Over" << std::endl;
std::cout << "Play Again? (y/n)" << std::endl;
char cPlayAgain;
std::cin >> cPlayAgain;
//Play Again? Yes
if(cPlayAgain == 'y'){
//Clear Board
bGameOver = false;
cSquare1 = '1';
cSquare2 = '2';
cSquare3 = '3';
cSquare4 = '4';
cSquare5 = '5';
cSquare6 = '6';
cSquare7 = '7';
cSquare8 = '8';
cSquare9 = '9';
}
iPlayer1 = 1;
} else {
//Alternate Player Turns
if(iPlayerTurn == 1) {
iPlayerTurn == 2;
} else {
iPlayerTurn == 1;
}
}
} while (!bGameOver);[/source]
Advertisement
Please use [source][/source] tags to post source code - it will keep all the indentation and pretty colours. I've added them for you, this time.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

It probably can't find the file because your code is incorrect, and hence it can't compile it into anything to run.

Some problems:

  • It looks like you're missing a closing brace ('}') for the main() function (could just be a copy 'n' paste error)
  • Trying to compile the code, I get the errors (after adding a closing curly brace at the end):

    • t.cpp:96: error: ‘cSquare’ was not declared in this scope

      • Solution: you probably forgot a '3' at the end of 'cSquare' on line 96
  • t.cpp:107: error: ‘iPlayerMove’ was not declared in this scope

    • Solution: there is no iPlayerMove variable declared (you probably meant to say iPlayerTurn?)
  • t.cpp:133: error: ‘iPlayer1’ was not declared in this scope

    • Solution: there is no iPlayer1 variable declared (you probably meant to say iPlayerTurn?)




  • You should look into using arrays. They'll simplify your code greatly.

    [edit]

    Removed re-formatted code.
    [size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
    Thanks guys really appreciate it, Swiftcoder, sorry bout that lol, I'll keep that in mind next time,

    You should look into using arrays. They'll simplify your code greatly.

    [edit]



    Please elaborate briefly, arrays? (still learning so forgive me, I've only just started a few weeks ago, got a few books i read in my spare time)

    SwiftCoder, apologies, i'll keep that in mind next time.

    [quote name='Cornstalks' timestamp='1347586978' post='4979906']
    You should look into using arrays. They'll simplify your code greatly.

    [edit]



    Please elaborate briefly, arrays? (still learning so forgive me, I've only just started a few weeks ago, got a few books i read in my spare time)
    [/quote]
    In all honesty, I think google can elaborate better than I can.


    SwiftCoder, apologies, i'll keep the [source] in mind next time.

    Consider using [code ][/code ] (without the space) tags. You'll actually get syntax highlighting with code tags. Source tags on this site are broken.
    [size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

    This topic is closed to new replies.

    Advertisement