I'm not sure what to change for it to work for this problem. I'm suppose to make changes to my original code (at the bottom) and make it look like this:
Here is the board:
_ _ _
_ _ _
_ _ _
Please enter a position (0-8): 0
Please enter an X or O: X
X _ _
_ _ _
_ _ _
Please enter a position (0-8): 9
Please follow the directions!
Please enter a position (0-8): 8
Please enter an X or O: A
Please follow the directions!
Please enter an X or O: O
X _ _
_ _ _
_ _ O
^C (stops the program)
#include<iostream>
usingnamespace std;
int main(){
cout << "Here is the board: "int count = 0;
char array [9];
while (count < 9){
cout<<"Please enter an X or O for position "<< count <<": ";
//user input
char answer;
cin >> answer;
//doing if it is either X or O
if(answer == 'X' || answer =='O'){
array[count] = answer;
count++;
}
//If user puts a invalid answer
else{
cout << "Please follow the directions!" << endl;
continue;
}
}
int i;
//printing the contents
cout << "Your answers were: ";
for(i = 0;i < 9;i++){
cout << array[i];
}
cout << endl;
//how to tell if there is a triple
int triple = 0;
for(i = 0;i < 7;i++){
if((array[i]==array[i+1]) && (array[i]==array[i+2]) && (array[i]=='X')){
triple = 1;
cout << "There is at least one triple X in this data" << endl;
//stop after we found the triple
break;
}
}
//if no X's triple is found
if(triple == 0){
// haven't fount any X's triple
cout << "Thee are no triples in this data" << endl;
}
return 0;
}
You can use 2D array[3][3]; it will give you a 3X3 square.
when user enter 7 and X, it will assign X to array[2][1], so how?
in C++, when you do integer division 7/3, it will give you 2.(remove all decimal) now you have row number. To get column number you can do 7%3, it will give you remainder 1.
each time when user enter a number and a letter, test them, if they are valid, assign to 2D array.