C++ ERROR

JC0724

Weaksauce
Joined
Oct 11, 2008
Messages
105
I am having an issue in my code I can't seem to figure out. I stopped when I realized there was an error. It is in the boolean function below. The bracket is giving me an error and I am not sure what I have double checked and all of my brackets match up so not sure what the problem is.

Code:
 //Tic-Tac-Toe
//Plays the game of tic-tac-toe against a human opponent

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;


//global constant
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';

//function prototypes
void instuctions();
char askYesNo(string question);
int askNumber(string question, int high, int low=0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>& board);
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board, int move);
int humanMove(const vector<char>& board, char human);
int computerMove(vector<char> board, char computer);
void announceWInner(char winner, char computer, char human);


//main function
int main() {
}//end of main

//instructions
void instructions() {
    cout << "Welcome to the ultimate man-machine showdown!!.  Tic-Tac-Toc,"<<endl;
    cout << "--where human brain is pit against silicon processor."<<endl;

    cout <<"\nMake your move known by entering a number, 0 - 8.  The number"<<endl;
    cout <<"corresponds to the desired board position, as illustrated:\n\n";

    cout <<"        0 | 1 | 2"<<endl;
    cout <<"        ---------"<<endl;
    cout <<"        3 | 4 | 5"<<endl;
    cout <<"        ---------"<<endl;
    cout <<"        6 | 7 | 8"<<endl;

    cout <<"Prepare yourself, human, The battle isabout to begin.\n"<<endl;
}//end instructions


char askYesNo(string question) {
    char response;

    do {
        cout << question << "(y/n): ";
        cin >> response;

    }while (response != 'y' && response != 'n');
}//end of askYesNo

int askNumber(string question, int high, int low) {
    int number;

    do {
        cout << question << " (" << low << " - " <<high <<"): ";
        cin >> number;
    }while (number > high || number < low);

    return number;
}//end of askNumber

char humanPiece() {
    char go_first = askYesNo("Do you require the first move?");

    if(go_first == 'y') {
        cout <<"\nThen take the first move. You will need it.\n";
        return X;
    }
    else {
        cout <<"\nYour bravery will be your undoing... I will go first.\n";
        return O;
    }
}//end of humanPiece

char opponent(char piece) {
    if(piece == X) {
        return O;
    }
    else {
        return X;
    }
}//end of opponent

void displayBoard(const vector<char>& board) {
    cout << "\n\t" << board[0] << " | " << board[1] << " | " <<board[2];
    cout << "\n\t" << "----------";
    cout << "\n\t" << board[3] << " | " << board[4] << " | " <<board[5];
    cout << "\n\t" << "----------";
    cout << "\n]t" << board[6] << " | " << board[7] << " | " <<board[8];
    cout <<"\n\n";
}//end of display board

char winner(const vector<char>& board) {
    //all possible winning rows
    const int WINNING_ROWS[8][3] = { {0, 1, 2},
                                     {3, 4, 5},
                                     {6, 7, 8},
                                     {0, 3, 6},
                                     {1, 4, 7},
                                     {2, 5, 8},
                                     {0, 4, 8},
                                     {2, 4, 6} };
    const int TOTAL_ROWS = 8;

    //if any winning row has three values that are the same (and not EMPTY)
    //then we have a winner

    for (int row = 0; row < TOTAL_ROWS; ++row) {
        if ( (board[WINNING_ROWS[row][0]] != EMPTY) && (board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) && (board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
        {
            return board[WINNING_ROWS[row][0]];
    }

        //since nobody has won, check for a tie (no empty squares left)
        if(count(board.begin(), board.end(), EMPTY) == 0) {
            return TIE;
        }
        else {
            return NO_ONE;
        }

}//end of winner

   
    bool isLegal(const vector<char>& board, int move)
    { //error here with this bracket
        return (board[move] == EMPTY);
    }//end of isLegal function
   
   

    int humanMove(const vector<char>& board, char human) {
        int move = askNumber("Where will you move?", (board.size() - 1));

            return move;
    }//end of human Move
 
On line 126ish you are missing the end bracket for the if statement after

Code:
return board[WINNING_ROWS[row][0]];
 
On line 126ish you are missing the end bracket for the if statement after

Code:
return board[WINNING_ROWS[row][0]];

Yeah, I'm not sure what development you're using, but something like Visual Studio with intellisense should point that out to you, and even if you're not using an IDE, you should be able deduce a simple problem like that based off of compiler errors.
 
I think there's some bad style near that point that confused you. I think you should put brackets on a newline for a code block

if ( )
{
// code
}

Now.. I know I'm probably about to get people tell me this is just "style" and that many people do it the other way, but I think that is one reason you missed it. Either-way, stick with one style over the other. (Your If-statement right before it has it on a newline)
 
I think there's some bad style near that point that confused you. I think you should put brackets on a newline for a code block

I think he shouldn't, because there's literally no reason to get into the habit of wasting space.

Either-way, stick with one style over the other. (Your If-statement right before it has it on a newline)

This, on the other hand, he should do. Either always use the K&R style, or always use the I-like-to-waste-space-because-I-haven't-developed-the-ability-to-visually-locate-scopes-yet braces. Don't mix and match, because that will generally make other people who are reading your code hate you.
 
I think he shouldn't, because there's literally no reason to get into the habit of wasting space.

It's easier to match braces and looks better, there's also almost no chance of my brace being off the right side of my screen and confusing me on whether there's a code block or a single statement after an if-statement.

You think you are in the majority? :D

P.S. I write all my code on one line and never separate code with extra spaces for formatting because it is an incredible waste of my limited resource of code-lines.
 
It's easier to match braces

Which is kind of pointless, because matching braces is not hard to begin with, and even if it was most good editors will do it for you anyways.

and looks better,

Looks worse, you mean.

there's also almost no chance of my brace being off the right side of my screen and confusing me on whether there's a code block or a single statement after an if-statement.

Of course, that shouldn't confuse you anyways, since there's something called an indent in C++. If you find the start of an if statement that runs off the side of the screen, and the line after it is indented one step further, the only logical conclusion an intelligent person could come to is that the if statement is not a one-liner and that the code underneath it is part of that statement.

Either way, dealing with K&R braces shouldn't be a challenge for anybody cognitively capable of writing worthwhile code..

You think you are in the majority? :D

The majority of the programmers and codebases I have worked with reflect my opinion, so yes.

P.S. I write all my code on one line and never separate code with extra spaces for formatting because it is an incredible waste of my limited resource of code-lines.

Says only people who have never tried to work with real code. Code lines aren't limited, but screen real-estate is, and I'd rather not have to spend 5 minutes scrolling because some fool decided lines of whitespace are better than lines of code.
 
What are you a perl programmer? Javascript? :eek: :cool: :confused:

People who can define their role simply by stating that they're an '<insert language here> programmer' are generally inferior. I would humor you, but since there isn't one language I limit myself to exclusively, I can't really give you an answer that meets your '<insert language here> programmer' template.
 
Yeah definitely stick to lining up braces, MUCH MUCH MUCH easier to read.

TBH the first thing I did when I looked at that code is I put it in notepad++ and fixed all the braces, then I tried compiling it. Though the issue was rather clear after I fixed the braces.

I see lot of professional publications that use that style, and it just looks so bad I can't believe they actually use that. May as well just write the whole program on one line, because it saves space right? :p
 
I see lot of professional publications that use that style,

Gee, I wonder why. It probably has something to do with the fact that most professionals have figured out how to read code well enough that reading either style is trivial.
 
I have recently taken the side of "Don't put opening braces on their own line"

If you indent your functions you can tell what's what. Some things like Python or HAML take that to an extreme...no braces just indentation! It's hard to get used to at first but why ugly-up the code with extra characters?
 
Yeah, I'm not sure what development you're using, but something like Visual Studio with intellisense should point that out to you, and even if you're not using an IDE, you should be able deduce a simple problem like that based off of compiler errors.

The same way you should be able to notice the grammatical errors in your sentences before posting them here?

Look: not everyone dropped from the womb knowing how to do the things that you've learned how to do. If you want to help, please do. If you want to ridicule people because it makes you feel better about yourself, you're better off watching Jerry Springer.

I have recently taken the side of "Don't put opening braces on their own line"
I'm of the opinon that it doesn't matter. I have a preference, but the team can decide what they want to do and I'll stick with it. There are much more important problems to solve, and since this decision is completely inconsequential, those more pressing issues should be the focus. It's really remarkable, though, how assertive some people can be about the decision -- using it to claim they're some how more experienced or professional for example -- when it just doesn't matter.
 
what is this whole deal about saving space?

it is my understanding that the reason saving space was important was because back in the day on those old terminal machines every line matter which is why all those old school coders code in that style. but with today's tech it's seems silly to focus on such matters. what is most important is that you are consistent with what ever style you choose.

my biggest problem has always been with code that mixes styles. not only is it really ugly code (cosmetically that is), but it's extremely difficult to read.

bottom line here is: stay consistent.
 
Last edited:
The same way you should be able to notice the grammatical errors in your sentences before posting them here?

Yes, which is why I have not yet made a thread asking for someone to correct the grammatical errors in my posts before I post them.

Look: not everyone dropped from the womb knowing how to do the things that you've learned how to do.

...Which is why you won't find me criticizing him for not being able to successfuly do this on his own. While I don't disagree with your assertion, I do not really see much purpose in it, because you're not pointing out anything I haven't realized prior to posting in this thread.

If you want to help, please do. If you want to ridicule people because it makes you feel better about yourself, you're better off watching Jerry Springer.

I pointed out that there are several useful items at his disposal which would facilitate easily discovering and resolving his error. I believe that it would be helpful for him to be aware of these in future as they would make him more capable of finding the errors in his code by himself. Do you disagree? Do you believe it is not helpful for him to start learning how to use his tools to help isolate and correct syntactical errors? Which portion of my post do you interpret as ridicule? Perhaps, since forum text is quite removed from face to face interaction, you have mistaken this as an attack on the thread starter. If that is the case, I can assure you that no such bad intentions were held when I made this post and if the thread starter feels insulted by my post, I welcome him to seek an apology from me.

what is this whole deal about saving space?

I personally believe that it's valuable to be able to see more of the code at one time.

my biggest problem has always been with code that mixes styles. not only is really ugly code (cosmetically that is), but it's extremely difficult to read.

bottom line here is: stay consistent.

This is certainly the important part. Mixing styles brings out the inner OCD side in everybody. Though a page full of wasted space is somewhat annoying to me, a page fully of inconsistent style choices is considerably more annoying.
 
Last edited:
Which portion of my post do you interpret as ridicule?
Post #3, plus this one:
pr always use the I-like-to-waste-space-because-I-haven't-developed-the-ability-to-visually-locate-scopes-yet braces.
This expresses your opinion of a particular coding style, but in a very derisive way. Added to the tone of Post 10 ... well, I'm sure you can understand my doubts about your intentions.
 
As much as I love (and hate) C++, at the end of the day, I think we can all agree that trying to make C++ pretty is mostly an exercise in futility. It's beautifully functional, but not beautifully syntactic. Bracing style is hardly worth worrying about in the grand scheme of things.
 

I'm sorry, but your feedback is rather vague/non-existant. What part of that post do you feel is ridiculing and why? I personally don't see what's ridiculing about pointing out to him that there are features of both his compiler and likely his development environment that will help him identify these errors on his own in the future. Or perhaps this is the wrong kind of helpful, in your view of the word. Maybe I should have copied his code, fixed the error, and posted the fixed code back to him with no explaination or tips. Would that have been more helpful?
 
Genmay is actually not as bad TBH... People in this coding forum seem to get their panties in a bunch over the silliest matters. Let's start talking about gun control or something. :p
 
The attitude I'm pointing out extends to your response here, too.

This is basically why I was trolling you about the braces too. You have a condescending tone and holier-than-thou attitude.

Anyway, my real opinion is that doesn't really matter, but I do prefer the braces on a new line. I also think it would be a good idea to use the style that reflects the team you work on or the language you are currently using. You may find many C/C++ developers who use the brace on the same line, but you'll find a vast majority of C# developers and I believe Java developers with new lines for braces. I'd probably adapt my style to suit the common style for the language that I was currently writing.

I apologize for somewhat intentionally starting a bracket fight.
 
The attitude I'm pointing out extends to your response here, too.

So what I'm gathering from this is that you're not really interested in whether or not my posts are helpful to the thread starter. In that case, I'm entirely disinterested in what you have to say.
 
this is really funny, the OP started this thread looking for an answers to a simple problem but instead ended up with the thread being hijacked. but at the same time the OP hasn't responded to any of the posts and pretty much allowed this to happen.
 
this is really funny, the OP started this thread looking for an answers to a simple problem but instead ended up with the thread being hijacked. but at the same time the OP hasn't responded to any of the posts and pretty much allowed this to happen.

This happens routinely. I find it entertaining, as there are better places to find help on programming than [H]...
 
Genmay is actually not as bad TBH... People in this coding forum seem to get their panties in a bunch over the silliest matters. Let's start talking about gun control or something. :p

Because this forum is full of super-neckbeards with egos to match. It happens anywhere you go that is further down the nerd-tree. Everyone is smarter than everyone else, etc.
 
this is really funny, the OP started this thread looking for an answers to a simple problem but instead ended up with the thread being hijacked. but at the same time the OP hasn't responded to any of the posts and pretty much allowed this to happen.

No real way to stop Coding Style Pedantic people from hijacking a thread. :)
 
Back
Top