Hello ReHaMuhamed,
I loaded up your program this morning, Although it worked I made some changes.
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
|
void shape()
{
std::cout << " " << sq1 << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << " \n";
}
void result(int choice, int result)
{
switch (result)
{
case (1):
//{ // This is not needed in the case block.
//if (choice == 'X')
//{
// // here i wanna replace sq1 in cout below with x , HOW ???
// <--- You could hard code the "X" or "O".
// std::cout << " " << "X" << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << "\n";
//}
//else if (choice == '0') // <--- The if part not needed here. Also the comparison is to zero not the letter O.
//{
//}
// This works better.
std::cout << " " << static_cast<char> (choice) << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << "\n";
break; // <--- This statement was missing in all the case statements.
//} // This is not needed in the case block.
|
I changed the "shape" function. IMHO the numbers were backwards, To me this looks better. Also removed the pipe character at the end of each line.
1 2
|
// here i wanna replace sq1 in cout below with x , HOW ???
std::cout << " " << sq1 << " |" << " " << sq2 << " | " << sq3 << " \n" << "____________\n" << " " << sq4 << " |" << " " << sq5 << " | " << sq6 << " \n" << "____________\n" << " " << sq7 << " |" << " " << sq8 << " | " << sq9 << "\n";
|
Sort answer, tell it to do so. See above code for two examples. The first in the commented code is hard coded. Not the best way. You would need to code two different statements for each case. The second example makes use of the variable "choice" which is a better way and only needs one line for either "X" or "O".
In case 1 the whole if/else if can be done away with if favor of one line. This concept can be repeated or each case.
After thinking about I realized that the whole switch will not work. Each case statement will overwrite anything that was done previously. And since this is all hard coded there is no way to determine who won. This is where an array would be of more use. Choices of "X" or "O" would be stored in the array and a nested for loops would be used to print the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int enter = 0;
char choise;
shape();
std::cout << "Enter your square number \n "; // <--- Changed you.
std::cin >> enter;
std::cout << " Enter your choice ( x/o ) \n";
std::cin >> choise;
choise = toupper(choise); // <--- Added.
std::cout << std::endl; // <--- To add a blank line after the last cin.
result(choise, enter);
//for (int n = 1; n < 10; n++)
//{
//}
|
Notice the comments for the changes I made. Do not be afraid to use blank lines to break up your code, It makes it easier to read.
Part of main would benefit from a do/while loop to allow more than one run of the program. With your program the while condition is harder to figure out since you have no way to tell when the game is over or who won.
Hope that helps,
Andy