Weird error with }

hello i'm making a tic tac toe game(my first game actually)
but i get an weird error at the end of the main function ( the "}" ) so can someone take a look at my code. the code is not done but it should work anyway...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main ()
{
// creates all variables
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');
int iTurn(1);
bool bGameOver(true);
char cTurn('X');
do {
cout << cSquare1 << cSquare2 << cSquare3 << endl;
   }
}


i'm using microsoft visual c++ 2010
Last edited on
my compiler says you need a while loop... Which actually makes sense, cuz it's always do-while right? (I'm still a bit of a beginner, so I'm not really sure..)

that didnt work but thanks anyway kaduuk :)
1
2
3
do {
cout << cSquare1 << cSquare2 << cSquare3 << endl;
   }


That construct is incomplete. Either put in the while() condition or remove the do{}.
it still doesnt work. But i'm going to complete the code then maybe it works
why do you need a do{} construct anyway? What is the aim, just to output the content on the tic-tac-toe board? If so there is no need for a do {} construct you can just output it like so:

cout << cSquare1 << cSquare2 << cSquare3 << endl
<< cSquare4 << cSquare5 << cSquare6 << endl
<< cSquare7 << cSquare8 << cSquare9 << endl;

the whole game loop is in the {}
In the case that it's the game loop, you should use any of these:
1
2
3
4
while(true)
{
      // inside the gameloop
}


1
2
3
4
for(;;)
{
     // inside the gameloop
}


1
2
3
4
do
{
     // inside the gameloop
} while(true)
http://cplusplus.com/doc/tutorial/control/

You can read about the do-while loop somewhere in the middle.
Also...

char cSquare1('1');
char cSquare2('2');
char cSquare3('3');

//...

cout << cSquare1 << cSquare2 << cSquare3 << endl;


wtf is this. You don't have to use variables for everything. Just use char literals.


// no need to make all these different cSquareX variables
// that's a lot of work for no gain

// you can just print like this:
cout << '1' << '2' << '3' << endl;

// or better yet... like this:
cout << "123" << endl;


EDIT: oh wait... is this for tic-tac-toe or something?

nm I see why you're doing that. It'd still be simpler with arrays, but whatever.
Last edited on
if you're using 'int main()...' shouldn't you be returning a return-value to the operating system that executes your program after your program is complete? maybe your compiler is looking for a "return #" prior to your closing curly-brace for the 'main' function.
int main() actually has an implied return 0; so that isn't necessary. In all other cases you would be correct.
Depends on the compiler unfortunately...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main() {
    // creates all variables
    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');
    int iTurn(1);
    bool bGameOver(true);
    char cTurn('X');
    do {
        cout << cSquare1 << cSquare2 << cSquare3 << endl;
    } while (bGameOver == false); // You need to give a condition to base the loop on... I figure this is what you intended...
    return 0; // You probably don't need a return statement, but it is good practice, cause sometimes you do.
}

Fixed.

PS: Is this a Xoax.net tutorial?
Also, excuse the formatting, my IDE auto-formats to the way I like it, and I won't bother reformatting it...
Last edited on
yes this is the xoax.net tutorial.
I'm nearly done with it so i can try all answers soon :)
Topic archived. No new replies allowed.