return statement acting crazeeey!

hey guys. this segement of code is not working properly as it should. even after executing return statement, it goes on to the rest of the part of the code and executed even executes conditional statements that isn't true.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 int x=1;///declared globally 
 enum turn{X_turn, O_turn};


struct struct_board
{
  mark array_board[3][3];
  turn get_turn();
  void show_board();
  void initialize();///to initialize all array slots to empty
  void get_input();
};
turn struct_board::get_turn()
{

    int a=x%2;
    ++x; ///increase value of x for next time

    if(a==1)
    {
        cout<<"X_turn"<<endl;
        return X_turn;
        cout<<"HI this exeuted even after return";

    }

    else if(a==0)
    {
        cout<<"HI this exeuted even after return in next connditional case";

        cout<<"O_turn"<<endl;
        return O_turn;
    }

    else
    {
        cout<<"%% operator not working properly";///shouldn’t hit this
    }
}

int main()
{
   struct_board b;
   b.get_input();///just some partial code of int main.
}
Last edited on
I yanked and pasted your code into my function and it works. What specifically is your problem? What output are you getting?

Here is what worked for me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int x = 0;

bool funct()
{
    int a=x%2;
    ++x; ///increase value of x for next time
    if(a==1)
    {
        cout<<"X_turn"<<endl;
        return true;
        cout << "shouldn't be here\n";
    }

    else if(a==0)
    {
        cout<<"O_turn"<<endl;
        return false;
    }
    else
    {
        cout<<"%% operator not working properly";///shouldn’t hit this
    }
}
int main()
{
    for(int i = 0; i < 10; i++)
        funct();
    return 0;
}


My output:
O_turn
X_turn
O_turn
X_turn
O_turn
X_turn
O_turn
X_turn
O_turn
X_turn
Could you post your:

 
void get_input();


function definition?
never mind, solved it. the error was indeed in the get_input() function.
Topic archived. No new replies allowed.