using multiple and or statements in if condition - if-statement

#include <iostream>
using namespace std;
main()
{
char a[2];
cout << " \"Welcome to Virtual University\" " << endl;
cout << " Program to predict user programming level" << endl;
cout << " Please answer in either T=True or F=False" << endl;
cout << "Q:1 Switch is a loop?" << endl;
cout << "A: ";
cin >> a[0];
cout << "Q:2 Pointer stores memory address?" << endl;
cout << "A: ";
cin >> a[1];
cout << "Q:3 Semicolon after for loop is an error? " << endl;
cout << "A: ";
cin >> a[2];
if((a[0] == 'f') || (a[0] == 'F') && (a[1] == 't') || (a[1] == 'T') && (a[2]
== 'f') || (a[2] == 'F'))
{
cout << "Your programming level is advanced" << endl;
}
else if((a[0] == 'f') || (a[0] == 'F') && (a[1] == 't' ) || (a[1] == 'T')
&& (a[2]== 't') || (a[2] == 'T'))
{
cout << "Your programming level is intermediate" << endl;
}
else if((a[0] != 'f') || (a[0] != 't') && (a[1] != 't' ) || (a[1] != 'f') &&
(a[2] != 't') || (a[2] != 'f'))
{
cout << "please enter t or f only" << endl;
}
else
{
cout << "Your programing level is begginner" << endl;
}
return 0;
}
its not giving any errors in compiler but when i run this`` program it always prints the wrong statement. can you guyz please help me.
and when i remove the || conditions with capital F and T. its working fine.
can any one help putting those capital letters as well because its a requirement in my assignment. thanks in advance.

Related

My program keeps closing after I select Y to continue

I'm trying to make a program to play Tic Tac Toe. I have it start asking if you would like to play, but when I press Y my program doesn't start running and just closes. I'm not to sure what is causing this. Here is my code :
#include "pch.h"
#include <iostream>
using namespace std;
char square[10] = { '0','1','2','3','4','5','6','7','8','9'};
void showWelcomeMenu();
void showMenu();
int winner();
void board();
int startGame();
int quitApp();
int startGame()
{
int player = 1, i, choice;
char mark;
do
{
board();
player = (player % 2) ? 1 : 2;
cout << "Player " << player << ", enter space number: ";
cin >> choice;
mark = (player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout << " Invalid move ";
player--;
cin.ignore();
cin.get();
}
i = winner();
player++;
} while (i == -1);
board();
if (i == 1)
cout << "==>\aPlayer " << --player << " wins! ";
else
cout << "==>\aDraw";
cin.ignore();
cin.get();
return 0;
}
void showWelcomeMenu()
{
char answer;
bool quit = false;
cout << "Welcome to Tic Tac Toe!\n" << endl << endl;
cout << "Would you like to play? (Y/N)" << endl << endl;
cin >> answer;
if (answer == 'Y')
{
startGame();
}
else if (answer != 'Y')
{
exit(0);
}
}
int winner()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
&& square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
&& square[9] != '9')
return 0;
else
return -1;
}
int quitApp()
{
exit(0);
}
void board()
{
system("cls");
cout << "\n";
cout << "Tic Tac Toe";
cout << "\n";
cout << "Player 1 (X) : Player 2 (0)" << endl << endl;
cout << "\n";
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}
int main()
{
showWelcomeMenu();
quitApp();
}
Any help would be appreciated! Thank you. Sorry if this has been answered! I couldn't find it.
The code is working perfectly for me.
Please recheck it.
Maybe you are providing wrong input. like using y instead of Y (case sensitive)
To ensure that code works despite of case you can modify condition as
if (answer == 'Y' || answer == 'y')
{
startGame();
}
moreover , I'll suggest you to simplify conditions from that do while loop.
eg.
if (choice == 1 && square[1] == '1')
square[1] = mark;
should be replace by
if (choice == 1)
square[1] = mark;
same for other conditions that decide which cell to mark.
why? because choice is an integer checking it against charecter is not necessary(actually can cause bugs due to implicit type casting)

Restarting a program in c++

Take a look at this code, it's a game of Tic Tac Toe
After the game has finished, the program asks the user whether they want to play again and if yes, the function runs again. However, the array values remain updated as it has been declared globally. The array has to be declared globally as it's being used in multiple functions. Is there a way to reset the values of the array to their original values when the user selects the option to play again?
#include <iostream>
#include <string>
using namespace std;
void contents(); //function to draw the tic tac toe board
char letters[9] = {'a','b','c','d','e','f','g','h','i'}; //array containing
the default characters of the board
string check(); //function to check whether the game is ongoing or has ended
in a win or draw
string result; // string telling us whether the game is ongoing or has ended
in a win or draw
void contents()
{
cout <<endl<<endl<<endl<<" Tic Tac Toe"<<endl<<endl<<endl;
cout << endl;
cout << " | | " << endl;
cout << " " << letters[0] << " | " << letters[1] << " | " << letters[2]
<< endl;
cout << " | | " << endl;
cout << "----- ----- -----" << endl;
cout << " | | " << endl;
cout << " " << letters[3] << " | " << letters[4] << " | " << letters[5]
<< endl;
cout << " | | " << endl;
cout << "----- ----- -----" << endl;
cout << " | | " << endl;
cout << " " << letters[6] << " | " << letters[7] << " | " << letters[8]
<< endl;
cout << " | | " << endl << endl;
}
int main()
{
char selection1; //selection of the mark chosen by player 1 (X or O)
int player = 1;
char mark;//X or O
char selection2; //selection of the character to replace in the array chosen
by the players
char invalid; //character to enter if the user selects a character that
isn't present in the array
string choice;
while(selection1!='X' || selection1!='Y')
{
cout<<"Make your selection player 1 (X or O): ";
cin>>selection1;
if (selection1=='X' || selection1=='O')
break;
else
cout<<"Make a valid selection, press enter to select again"<<endl;
cin.ignore();
cin.get();
}
do
{
player=(player%2)?1:2;
if(selection1 =='X')
mark = (player == 1) ? 'X' : 'O';
else if (selection1=='O')
mark = (player == 1) ? 'O' : 'X';
contents();
cout << "Player " << player << ", enter a letter: ";
cin >> selection2;
if (selection2 == 'a' && letters[0] == 'a')
letters[0] = mark;
else if (selection2 == 'b' && letters[1] == 'b')
letters[1] = mark;
else if (selection2 == 'c' && letters[2] == 'c')
letters[2] = mark;
else if (selection2 == 'd' && letters[3] == 'd')
letters[3] = mark;
else if (selection2 == 'e' && letters[4] == 'e')
letters[4] = mark;
else if (selection2 == 'f' && letters[5] == 'f')
letters[5] = mark;
else if (selection2 == 'g' && letters[6] == 'g')
letters[6] = mark;
else if (selection2 == 'h' && letters[7] == 'h')
letters[7] = mark;
else if (selection2 == 'i' && letters[8] == 'i')
letters[8] = mark;
else
{
cout<<"Invalid move, press enter to continue: ";
cin.ignore();
cin.get();
player--;
}
result=check();
player++;
}while(result=="Ongoing");
contents();
if(result=="Over")
{
cout<<"Player "<<--player<<" wins ";
cin.ignore();
cin.get();
cout<<"Do you want to play again?";
cin>>choice;
if (choice =="Yes")
main();
}
else
{
cout<<"Draw";
cin.ignore();
cin.get();
cout<<"Do you want to play again?";
cin>>choice;
if (choice =="Yes")
main();
}
return 0;
}
string check()
{
if (letters[0] == letters[1] && letters[1] == letters[2])
return "Over";
else if (letters[3] == letters[4] && letters[4] == letters[5])
return "Over";
else if (letters[6] == letters[7] && letters[7] == letters[8])
return "Over";
else if (letters[0] == letters[3] && letters[3] == letters[6])
return "Over";
else if (letters[1] == letters[4] && letters[4] == letters[7])
return "Over";
else if (letters[2] == letters[5] && letters[5] == letters[8])
return "Over";
else if (letters[0] == letters[4] && letters[4] == letters[8])
return "Over";
else if (letters[2] == letters[4] && letters[4] == letters[6])
return "Over";
else if (letters[0] != 'a' && letters[1] != 'b' && letters[2] != 'c'
&& letters[3] != 'd' && letters[4] != 'e' && letters[5] != 'f'
&& letters[6] != 'g' && letters[7] != 'h' && letters[8] != 'i')
return "Draw";
else
return "Ongoing";
}
The obvious way would be to write a new function which does exactly what you want, i.e. set letters[0] to 'a' etc.
You will probably learn about classes later. A class holds multiple functions, and also multiple data members. Each instance of such a class is called an object, and you'd implement each round of the game as a new object.

Struggling with Tic tac toe C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm relatively new to coding and have been tasked with creating a tic tac toe game in C++, I thought I had all the code done but there are various issues such as user input not being allowed, and only allowing to type one players name instead of two, any help/advice you could give me on how to make this work would be really appreciated.
#include <iostream>
using namespace std;
int menumain;
int oneplayer = 'X';
char square1('1');
char square2('2');
char square3('3');
char square4('4');
char square5('5');
char square6('6');
char square7('7');
char square8('8');
char square9('9');
void toggleplayers() {
if (oneplayer == 'X')
oneplayer = 'O';
else
oneplayer = 'X';
}
char win()
{
if (square1 == 'X' && square2 == 'X' && square3 == 'X') return 'X';
if (square4 == 'X' && square5 == 'X' && square6 == 'X') return 'X';
if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X';
if (square1 == 'X' && square5 == 'X' && square9 == 'X') return 'X';
if (square3 == 'X' && square5 == 'X' && square7 == 'X') return 'X';
if (square3 == 'X' && square6 == 'X' && square9 == 'X') return 'X';
if (square1 == 'X' && square4 == 'X' && square7 == 'X') return 'X';
if (square2 == 'X' && square5 == 'X' && square8 == 'X') return 'X';
if (square1 == 'O' && square2 == 'O' && square3 == 'O') return 'O';
if (square4 == 'O' && square5 == 'O' && square6 == 'O') return 'O';
if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X';
if (square1 == 'O' && square5 == 'O' && square9 == 'O') return 'O';
if (square3 == 'O' && square5 == 'O' && square7 == 'O') return 'O';
if (square3 == 'O' && square6 == 'O' && square9 == 'O') return 'O';
if (square1 == 'O' && square4 == 'O' && square7 == 'O') return 'O';
if (square2 == 'O' && square5 == 'O' && square8 == 'O') return 'O';
return '/';
}
int main() {
int playerone, playertwo;
system("cls");
cout << "tic tac toe" << endl;
cout << "-----------" << endl;
cout << "Start game (1)" << endl;
cout << "Quit game (2)" << endl;
cout << "Press 1 or 2 to proceed." << endl;
cin >> menumain;
if (menumain == 2)
{
return 0;
} else {
cout << "Player One, please enter your name: " << endl;
cin >> playerone;
system("cls");
cout << "Player Two, please enter your name: " << endl;
cin >> playertwo;
system("cls");
int playermove;
cout << "Choose a number between 1-9 to place!" << endl;
cin >> playermove;
if (playermove == 1)
square1 = oneplayer;
else if (playermove == 2)
square2 = oneplayer;
else if (playermove == 3)
square3 = oneplayer;
else if (playermove == 4)
square4 = oneplayer;
else if (playermove == 5)
square5 = oneplayer;
else if (playermove == 6)
square6 = oneplayer;
else if (playermove == 7)
square7 = oneplayer;
else if (playermove == 8)
square8 = oneplayer;
else if (playermove == 9)
square9 = oneplayer;
cout << "Tic tac toe!" << endl;
cout << "------------" << endl;
cout << " " << square1 << " | " << square2 << " | " << square3 << " "
<< endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << " "
<< endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << " "
<< endl;
cout << " " << endl;
cout << playerone << endl;
cout << playertwo << endl;
while (1) {
if (win() == 'X') {
cout << playerone << " wins!" << endl;
break;
}
else if (win() == 'O') {
cout << playertwo << " wins!" << endl;
break;
}
toggleplayers();
}
system("pause");
return 0;
}
}
I made a quick fix. copy, paste, compile and test. it will do the job in terms of a game. It works. It's tic tac toe. :D. Good luck with further adjustments.
These }; }; }; are just markers for myself since I edited this in a text editor and not an IDE. My suggestion, add a clause for a Draw. Because It will not do anything if there is no winner.
#include <iostream>
using namespace std;
int menumain;
int oneplayer = 'X';
char square1('1');
char square2('2');
char square3('3');
char square4('4');
char square5('5');
char square6('6');
char square7('7');
char square8('8');
char square9('9');
void toggleplayers()
{
if (oneplayer == 'X')
oneplayer = 'O';
else
oneplayer = 'X';
};
char win()
{
if (square1 == 'X' && square2 == 'X' &&square3 == 'X')
return 'X';
if (square4 == 'X' && square5 == 'X' &&square6 == 'X')
return 'X';
if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
return 'X';
if (square1 == 'X' && square5 == 'X' && square9 == 'X')
return 'X';
if (square3 == 'X' && square5 == 'X' &&square7 == 'X')
return 'X';
if (square3 == 'X' && square6 == 'X' &&square9 == 'X')
return 'X';
if (square1 == 'X' && square4 == 'X' &&square7 == 'X')
return 'X';
if (square2 == 'X' && square5 == 'X' &&square8 == 'X')
return 'X';
if (square1 == 'O' && square2 == 'O' &&square3 == 'O')
return 'O';
if (square4 == 'O' && square5 == 'O' &&square6 == 'O')
return 'O';
if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
return 'X';
if (square1 == 'O' && square5 == 'O' && square9 == 'O')
return 'O';
if (square3 == 'O' && square5 == 'O' &&square7 == 'O')
return 'O';
if (square3 == 'O' && square6 == 'O' &&square9 == 'O')
return 'O';
if (square1 == 'O' && square4 == 'O' &&square7 == 'O')
return 'O';
if (square2 == 'O' && square5 == 'O' &&square8 == 'O')
return 'O';
return '/';
};
int main()
{
int playerone, playertwo;
cout << "tic tac toe" << endl;
cout << "-----------" << endl;
cout << "Start game (1)" << endl;
cout << "Quit game (2)" << endl;
cout << "Press 1 or 2 to proceed." << endl;
cin >> menumain;
if (menumain == 2)
{
return 0;
}
else
{
cout << "Player One, please enter your name: " << endl;
// cin >> playerone; create char array or string and ask for an input;
cout << "Player Two, please enter your name: " << endl;
// cin >> playertwo; create char array or string and ask for an input;
cout << "Tic tac toe!" << endl;
cout << "------------" << endl;
cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << " " << endl;
cout << " " << endl;
cout << playerone << endl; //here replace playerone with char array variable or string;
cout << playertwo << endl; //here replace playerone with char array variable or string;
while(1)
{
int playermove;
cout << "Choose a number between 1-9 to place!" << endl;
cin >> playermove;
if (playermove == 1)
square1 = oneplayer;
else if (playermove == 2)
square2 = oneplayer;
else if (playermove == 3)
square3 = oneplayer;
else if (playermove == 4)
square4 = oneplayer;
else if (playermove == 5)
square5 = oneplayer;
else if (playermove == 6)
square6 = oneplayer;
else if (playermove == 7)
square7 = oneplayer;
else if (playermove == 8)
square8 = oneplayer;
else if (playermove == 9)
square9 = oneplayer;
cout << "Tic tac toe!" << endl;
cout << "------------" << endl;
cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << " " << endl;
cout << " " << endl;
cout << playerone << endl;
cout << playertwo << endl;
if (win() == 'X')
{
cout << playerone << " wins!" << endl;
break;
}
else if (win() == 'O')
{
cout << playertwo << " wins!" << endl;
break;
};
toggleplayers();
};
return 0;
};
};
There are many issues with your code.
user input not being allowed
Actually you do get user input, at the beginning but not afterwards.
After toggling the player, you need to prompt the player for their move:
toggleplayers();
cout << "Player " << oneplayer << ", enter your move (1 - 9): ";
cout.flush();
cin >> playermove;

Can't find the bug/probleem in my program [duplicate]

This question already has answers here:
Why is this only returning "yes"
(2 answers)
Closed 6 years ago.
I'm making my first project c++. It's a simple temperature converter.
I made a test section [code 1] with if statements. the if statement would compare the user input. for example if you user typed c and then k(Celsius-Kelvin). it should run the function[code 2] CtoK(); but i doesn't it runs all function why does it do this?
It try to use return but i didn't(it also didn't gave a error so i kept it)
If you guys see something else pls say it Code on pastebin
Also thinks to keep it mind:
Just stated to learn C++
Not native English so if there are spelling and grammar mistakes please say it so i can learn form it
[code 1]
void whatToWhat(char firstDegrees, char secondDegrees) {
if (firstDegrees == 'C' || 'c') {// tests if the user want form c to f
if (secondDegrees == 'F' || 'f') {
CtoF();
}
}if (firstDegrees == 'C' || 'c') {// tests if the user want form c to k
if (secondDegrees == 'K' || 'k') {
CtoK();
}
}if (firstDegrees == 'F' || 'f') {// tests if the user want form f to c
if (secondDegrees == 'C' || 'c') {
FtoC();
}
}if (firstDegrees == 'F' || 'f') {// tests if the user want form f to k
if (secondDegrees == 'K' || 'k') {
FtoK();
}
}if (firstDegrees == 'K' || 'k') {// tests if the user want form k to f
if (secondDegrees == 'F' || 'f') {
KtoF();
}
}if (firstDegrees == 'K' || 'k') {// tests if the user want form k to c
if (secondDegrees == 'C' || 'c') {
KtoC();
}
}
}
[code 2]
void CtoF() {// c to f furmula
double input;
cout << "Enter a number[Celsius-Fahrenheit]" << endl;
cin >> input;
cout << "it's " << input * 1.8 + 32 << " Fahrenheit " << endl;
return;
}
void CtoK() {// c to k furmula
double input;
cout << "Enter a number[Celsius-Kelvin]" << endl;
cin >> input;
cout << "it's " << input + 273.15 << " Kelvin " << endl;
return;
}
void FtoC() {//f to c furmula
double input;
cout << "Enter a number[Fahrenheit-Celsius]" << endl;
cin >> input;
cout << "it's " << input / 1.8 - 32 << " Celsius " << endl;
}
void FtoK() {//f to k furmula
double input;
cout << "Enter a number[Fahrenheit-Kelvin]" << endl;
cin >> input;
cout << "it's " << input / 1.8 - 32 + 273.15 << " Kelvin " << endl;
return;
}
void KtoF() {// k to f furmula
double input;
cout << "Enter a number[Kelvin-Fahrenheit]" << endl;
cin >> input;
cout << "it's " << (input - 273.15) * 1.8 + 32 << " Fahrenheit " << endl;
}
void KtoC() {// k to c furmula
double input;
cout << "Enter a number[Kelvin-Celsius]" << endl;
cin >> input;
cout << "it's " <<273.15 - input << " Celsius " << endl;
return;
}
if(firstDegrees == 'K' || 'k') will always evaluate to true since k as it is is Not Null, means Valid, means True.
You need to write all your expressions in a similar way to this: (firstDegrees == 'K' || firstDegrees == 'k')
Also, you would want to add elses after each if, for better and clearer logic control.

Beginner C++: Tic Tac Toe

I am taking my first coding class this year in school (High school/C++), and this is my first real attempt at coding anything other than book exercises.
I just wanted to make a simple Tic Tac Toe game for my first project by myself (not school related, just for fun), but I have been struggling to fix some errors for the past few days.
First, I have been wondering if there is a way for me to return a variable to my 'Checkerboard' function so I don't have to call it after every move. Right now I have to call the 'Checkerboard' after every move, but I'm wondering if there is a way to return the placement of the X or O right to the original function so a new board does not pop up after every move.
Next, when I am using the 'CheckMove' functions my two "if" statements will not run one after the other (i,e. if I type a number that has already been used and then a number outside of the scope 0-10, my program does not run correctly). My program runs fine if I only do one of these things, but as soon as I do them back to back it fails to work properly.
Finally, I cannot figure out why my code will not stop running when a winner is declared (see 'CheckWinner function).
On a side note, as this is my first code any other suggestions on how to improve it would be highly appreciated.
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
void Checkerboard(char SpaceNumber[]);
void CheckMoveX(int Player[], int Choice, char SpaceNumber[]);
void CheckMoveO(int Player[], int Choice, char SpaceNumber[]);
int CheckWinner(char SpaceNumber[], int CheckWin);
void main()
{
char SpaceNumber[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int Player[2] = {1, 2}, Choice, CheckWin=0;
cout << " Tic Tac Toe\n\n";
cout << "Player 1: 'X' --- Player 2: 'O'\n\n";
Checkerboard(SpaceNumber);
do {
cout << "\nPlayer " << Player[0] << " pick a number to place your mark...";
cin >> Choice;
CheckMoveX(Player, Choice, SpaceNumber);
CheckWinner(SpaceNumber, CheckWin);
cout << "\nPlayer " << Player[1] << " pick a number to place your mark..."; //The rest of the main function is a duplicate of the first half, but it is for player 2.
cin >> Choice;
CheckMoveO(Player, Choice, SpaceNumber);
CheckWinner(SpaceNumber, CheckWin);
}while (CheckWinner != 0); //Program isn't ending when winner occurs.
_getch();
}
void Checkerboard(char SpaceNumber[])
{
cout << setw(3) << SpaceNumber[0] << setw(3) << "|" << setw(3) << SpaceNumber[1] << setw(3) << "|" << setw(3) << SpaceNumber[2] << endl;
cout << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << endl;
cout << setw(3) << SpaceNumber[3] << setw(3) << "|" << setw(3) << SpaceNumber[4] << setw(3) << "|" << setw(3) << SpaceNumber[5] << endl;
cout << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << endl;
cout << setw(3) << SpaceNumber[6] << setw(3) << "|" << setw(3) << SpaceNumber[7] << setw(3) << "|" << setw(3) << SpaceNumber[8] << endl;
}
void CheckMoveX(int Player[], int Choice, char SpaceNumber[]) //I cannot seem to have the first/second "if" statements run back to back.
{
do {
if (Choice > 0 && Choice < 10)
{
if (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O') //This loop ensures that the same square cannot be picked twice.
{
do {
cout << "This number has already been chosen. Please pick a different number...";
cin >> Choice;
} while (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O');
}
SpaceNumber[Choice - 1] = 'X';
Checkerboard(SpaceNumber);
}
if (Choice < 1 || Choice > 9)//Forces the user to pick a number on the board.
{
do {
cout << "Invalid choice. Please pick another number...";
cin >> Choice;
} while (Choice < 1 || Choice > 9);
SpaceNumber[Choice - 1] = 'X';
Checkerboard(SpaceNumber);
}
} while (SpaceNumber[Choice - 1] != 'X');
}
void CheckMoveO(int Player[], int Choice, char SpaceNumber[]) //Duplicate of CheckMoveO
{
if (Choice > 0 && Choice < 10)
{
if (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O')
{
do {
cout << "This number has already been chosen. Please pick a different number...";
cin >> Choice;
} while (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O');
}
SpaceNumber[Choice - 1] = 'O';
Checkerboard(SpaceNumber);
}
if (Choice < 1 || Choice > 9)
{
do {
cout << "Invalid choice. Please pick another number...";
cin >> Choice;
} while (Choice < 1 || Choice > 9);
SpaceNumber[Choice - 1] = 'O';
Checkerboard(SpaceNumber);
}
}
int CheckWinner(char SpaceNumber[], int CheckWin)
{
if (SpaceNumber[0] == 'X' && SpaceNumber[3] == 'X' && SpaceNumber[6] == 'X') //Not sure how to do this without if/else statements.
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[1] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[7] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return CheckWin;
}
else if (SpaceNumber[2] == 'X' && SpaceNumber[5] == 'X' && SpaceNumber[7] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins";
return 0;
}
else if (SpaceNumber[0] == 'X' && SpaceNumber[1] == 'X' && SpaceNumber[2] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[3] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[5] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[6] == 'X' && SpaceNumber[7] == 'X' && SpaceNumber[8] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[0] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[8] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins";
return 0;
}
else if (SpaceNumber[2] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[6] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins";
return 0;
}
//End Player 1 Win. Begin Player 2 Win (Duplicate Code).
else if (SpaceNumber[0] == 'O' && SpaceNumber[3] == 'O' && SpaceNumber[6] == 'O') //Not sure how to do this without if/else statements.
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[1] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[7] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[2] == 'O' && SpaceNumber[5] == 'O' && SpaceNumber[7] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins";
return 0;
}
else if (SpaceNumber[0] == 'O' && SpaceNumber[1] == 'O' && SpaceNumber[2] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[3] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[5] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[6] == 'O' && SpaceNumber[7] == 'O' && SpaceNumber[8] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[0] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[8] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins";
return 0;
}
else if (SpaceNumber[2] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[6] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins";
return 0;
}
//End player 2 win; begin cout for tie game.
else
return 1;
}
1) You're not saving the value of CheckWinner to CheckWin
Replace CheckWinner(SpaceNumber, CheckWin); with CheckWin = CheckWinner(SpaceNumber, CheckWin);
2) You need to check if (CheckWin != 0) instead of (CheckWinner != 0)
3) You're not checking if the game ended before taking the input for the next move.
Replace your do.. while block in your main function with this
do {
CheckWin = CheckWinner(SpaceNumber, CheckWin);
if (CheckWin!=0)
{
cout << "\nPlayer " << Player[0] << " pick a number to place your mark...";
cin >> Choice;
CheckMoveX(Player, Choice, SpaceNumber);
}
CheckWin = CheckWinner(SpaceNumber, CheckWin);
if (CheckWin != 0 )
{
cout << "\nPlayer " << Player[1] << " pick a number to place your mark...";
cin >> Choice;
CheckMoveO(Player, Choice, SpaceNumber);
}
}while (CheckWin != 0);
That should solve your problem. Let me know if you have some other issue with the code.
You're not calling CheckWinner in your while condition, you are comparing the address of CheckWinner which will never be zero.
You have a lot of duplicate code. Rather than two CheckMove functions, just have one and pass in an 'X' or 'O' to compare with. You can do the same thing with CheckWinner (which has an incorrect subscript in the 3rd comparison).
For your second question, the problem in CheckMove is that in the do/while loop you get the input value but don't validate it.
There are two reasons why your program doesn't stop when you have a winner: The first is that after 'X' moves you check for a winner but don't check the result of that before having 'O' move. The second is that your comparison, CheckWinner != 0 is checking the address of a function for zero (which will never happen), rather than your CheckWin variable. This would work better if CheckWinner returned the winner rather than setting a global variable.
I'm not sure what you're asking with your first question.