i want this code to keep track of whose turn it is: but i keep getting this error : error: a function-definition is not allowed here before '{' token
any help

void turn_it_is(int play1, int play2)
{
    cout << ((turn_it_is%2)==1)?(play1:play2)<<
    cout <<  " it's your turn ";
    cin>> turn_it_is;
}

Recommended Answers

All 10 Replies

hello,

it could just be with the placing of the function. Maybe you should try moving it around. I don't know it looks fine to me...

this error comes up now as wel:

error: invalid operands of types `int ()(int, int)' and `int' to binary `operator%'

void turn_it_is(int play1, int play2)
{
    cout << (((turn_it_is%2)==1)?(play1:play2));
    cout <<  " it's your turn ";
    cin >> turn_it_is;
}
void turn_it_is(int play1, int play2)
{
    cout << (((turn_it_is%2)==1)?(play1:play2));
    cout <<  " it's your turn ";
    cin >> turn_it_is;
}

What are you doing? "turn_it_is" is a function. Not a variable!. Why
are you trying to use cin >> turn_it_is ? and also why are you trying to
use the modulus operator with the function name ? Did you make a
global variable called turn_it_is or something ?

hello,

it could just be with the placing of the function. Maybe you should try moving it around. I don't know it looks fine to me...

Other than the fact you can't do this?

cout << ((turn_it_is%2)==1)?(play1:play2) << cout << " it's your turn ";

Or that the variable name and function name are identical?

void turn_it_is(int play1, int play2)
{
    cout << (((turn_it_is%2)==1)?(play1:play2));
    cout <<  " it's your turn ";
    cin >> turn_it_is;
}

What are you doing? "turn_it_is" is a function. Not a variable!. Why
are you trying to use cin >> turn_it_is ? and also why are you trying to
use the modulus operator with the function name ? Did you make a
global variable called turn_it_is or something ?

Wouldn't it be causing other errors concerning name re-use or re-definition if there were a global variable with the same name?

I think it is a combination of ambiguous naming and something else. The error says "Function definition not allowed here". That makes me think the OP is trying to define the function inside the body of another function. There's probably a close-brace missing somewhere...

opps .

int turn_it_is(int play1, int play2)
{
cout << (((play1%2)==1)?(play1:play2)) << " it's your turn " << endl;
}

is there another method to figure out which user turned it is. i found this method from looking on da net from different people problems, i really dont understand what it is doing. if this is correct and the best way of doing it and i just need to fix the errors let me know aswell.but a different way i was thinking is a if statement but it would b long and im trying to figure out ways to make my program smaller

Making a program "smaller" at the expense of readability generally isn't advisable.

((play1%2)==1)?(play1:play2))

This is a condensed version of an if statement called the "ternary operator" and I'm pretty sure it's not used correctly. There is an extra pair of parentheses around the play1:play2 part that I think are messing it up. Google it for more information.

We really can't help you without more information. What are play1 and play2, especially since they are integers? There are much better ways of doing this. But we can't really make recommendations without knowing your situation.

The rules are simple: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions:
• roll - If the player rolls a
1: the player scores nothing and it becomes the opponent's turn (“pigging out”).
2 - 6: the number is added to the player's turn total and the player's turn continues.
• hold - The turn total is added to the player's score and it becomes the opponent's turn.

• A function that handles the taking of a turn. This function should work for either player. It should handle the player’s turn and update their score if they hold or win. It should not update their score if they “pig out” and roll a 1.

We need to see more of your code, there's not enough here.

#include<iostream>
#include<ctime>

using namespace std;
void score_num(int &ns1,int &ns2,int &ns3, int &ns4, int &ns5, int &n6, char gam_answ);
void display_score(int curr_score);
void roll_hold(int roll, int answer, int turn);
int turn_it_is(int player1, int player2);
void play_again(char letter);
void score_num(int &s1, int &s2, int  &s3, int  &s4, int  &s5, int  &s6, char select_r_or_h)
{
    int turn;
    while(char ns1=0)
    {
        cout << " next player " << ((turn % 2) ==1);
        cout << " please select r for roll and h for hold ";
        cin >> select_r_or_h;
        select_r_or_h++;
    }
}
void display_score(int real_score)
{
    int gam_answ;
    real_score = gam_answ++;
}
void roll_hold( char remark)
{
     int which_Num;
    if(remark=='r')
    {
        do
        {
        unsigned seed = time(0);
        srand(seed);
        which_Num = rand() % 7;
        }while(remark== 'r');
    }
        if (remark=='h')
        {
            do
            {
            system("pause");
            }while (remark=='h');
        }
}
int turn_it_is(int play1, int play2)
{
    cout << (((play1%2)==1)?(play1:play2)) <<  " it's your turn " << endl;
}
void play_again( char selection)
{
    cout << " Would you like to play agian?( press y or n): ";
    cin >> selection;
}
int main()
{
    int total_score, print_score, action, whose_turn, go_again;
    cout << "Race to 100 " << endl;
    total_score = score_num(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& n6, char gam_answ);
    print_score= display_score(int curr_score);
    action= roll_hold(int roll, int answer, int turn);
    whose_turn= turn_it_is(int player1, int player2);
    go_again=play_again( char letter);

 return 0;
}
int main()
{
    int total_score, print_score, action, whose_turn, go_again;
    cout << "Race to 100 " << endl;
    total_score = score_num(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& n6, char gam_answ);
    print_score= display_score(int curr_score);
    action= roll_hold(int roll, int answer, int turn);
    whose_turn= turn_it_is(int player1, int player2);
    go_again=play_again( char letter);

 return 0;
}

How much of this did you write and how much did you copy?

Not one of these function calls is correct. These are all improperly-formatted prototypes at best. You can not have data-type specifiers in your calls, unless you are performing explicit type casts (which, as written, these aren't). When I try to compile, I get this:

error C2144: syntax error : 'int' should be preceded by ')'
'score_num' : function does not take 0 argument
syntax error : ')'

I get a similar group of errors for each call in main().

Also, where do you declare and initialize all of these variables that you are using as arguments? (Hint: you don't)

Here is more information on functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.