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
}