0

I'm a total beginner and I'm self studying c++, there is this exercise in jumping into C++ in which it asks to write a tic tac toe game, Now I am half way through the program but currently stuck with a problem inside while loop. I will not put my current code here as most part of it will be irrelevant to my question but I wrote the code below which is similar to the problem I am facing in my tic tac toe game:

**No matter what character I give to any of the -char test- variables they keep getting re- initialized to their initial chars because of the while loop in the main(), the only way I know out of this problem is to change the variable's scope to global but I don't want to do that.

??So how can I stop the variables from getting re-initialized ? I can't really move the while loop from main() as it would affect the other functions in my tic tac toe game... Please consider that I'm a beginner and I only know loops, conditional statements and bools, I will not understand big programming words.

Thank you

#include <iostream>

int show(int choice, char x_o);

int main(){
    int i=0;
    int choice;
    char x_o=' ';
    while(i<2){
        //enter 2 and X the first time for example
        //enter 3 and O the second time for example
        std::cin>>choice>>x_o;
        show(choice, x_o);
        ++i;
    }
}

int show(int choice, char x_o){
    char test='1';
    char test2='2';
    char test3='3';

    switch(choice){
        case 1:
            test=x_o;
            break;
        case 2:
            test2=x_o;
            break;
        case 3:
            test3=x_o;
            break;
    }

    std::cout<<test2<<'\n'; //test2 prints 'X' the first time
    //test2 the second time prints '2' again
}
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
BigRedMachine
  • 55
  • 1
  • 5
  • If you want to store these values in `show`, make the variables static. If you would rather store them in `main`, pass them by reference. – Beta Dec 02 '14 at 05:59

2 Answers2

1

It's simple. Make them static.

int show(int choice, char x_o){

    static char test='1';
    static char test2='2';
    static char test3='3';
    // ...
}

The static keyword means that they'll retain their value after the method exists. You probably want to set the values elsewhere:

static char test;
static char test2;
static char test3;

int show(int choice, char x_o){
    // ...
}
max
  • 9,708
  • 15
  • 89
  • 144
  • For the intricacies of using static local variables check out: http://stackoverflow.com/questions/246564/what-is-the-lifetime-of-a-static-variable-in-a-c-function
    The more "mainstream" way of doing what you want to do is using call by reference
    – markus Dec 02 '14 at 06:02
  • If you are making it global as depicted in second example, you don't need to make it static in that case. – Abhineet Dec 02 '14 at 06:05
1

If you define your variables local to your method show() they will be redefined every time you declare the variable. You can however change the way your variables are defined. You could for example:

  • define them global
  • define them local to your main method and pass them as references or pointers to your show method

I'd go for number 2. This allows you define the variables locally in your main method. Their scope will be that of main(). You can then pass a reference/pointer to your show method which allows you to read and change its value inside show.

markus
  • 1,631
  • 2
  • 17
  • 31