I have a PvP TicTacToe program running, and everything is working fine, except for one thing: when a text character (q,w,e,etc) is entered the loop goes wild.
Here's the portion of the code the loop is in, and if a text character is input instead of a digit, the loop keeps repeating without asking for another value.
cmove=0;
while (cmove!=1)
{
if (loop==1) cout<<"Player x's turn."<<endl;
if (loop==3) cout<<"Player x's turn."<<endl;
if (loop==5) cout<<"Player x's turn."<<endl;
if (loop==7) cout<<"Player x's turn."<<endl;
if (loop==9) cout<<"Player x's turn."<<endl;
if (loop==2) cout<<"Player o's turn."<<endl;
if (loop==4) cout<<"Player o's turn."<<endl;
if (loop==6) cout<<"Player o's turn."<<endl;
if (loop==8) cout<<"Player o's turn."<<endl;
cout<<"Which space do you wish to fill?"<<endl;
cin>>hmove;
cmove=3;
if (hmove==1) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==2) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==3) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==4) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==5) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==6) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==7) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==8) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==9) if (space [hmove]==0) cmove=1; else cmove=2;
if (cmove==2) cout<<"That space is already taken."<<endl;
if (cmove==3) cout<<"That is not a valid space."<<endl;
cout<<endl;
}
cmove=0;
If it helps you understand what is going on, here is the full program:
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int main()
{
int space [9];
int hmove;
int fmove;
int cmove;
int loop;
while (1==1)
{
space [1]=0;
space [2]=0;
space [3]=0;
space [4]=0;
space [5]=0;
space [6]=0;
space [7]=0;
space [8]=0;
space [9]=0;
hmove=0;
cmove=0;
fmove=0;
cout<<"New game!"<<endl;
cout<<endl;
for (loop=1; loop<10; loop++)
{
cout<<endl;
if (space [1]==0) cout<<"- ";
if (space [1]==1) cout<<"x ";
if (space [1]==2) cout<<"o ";
if (space [2]==0) cout<<"- ";
if (space [2]==1) cout<<"x ";
if (space [2]==2) cout<<"o ";
if (space [3]==0) cout<<"- ";
if (space [3]==1) cout<<"x ";
if (space [3]==2) cout<<"o ";
cout<<" 1 2 3";
cout<<endl;
if (space [4]==0) cout<<"- ";
if (space [4]==1) cout<<"x ";
if (space [4]==2) cout<<"o ";
if (space [5]==0) cout<<"- ";
if (space [5]==1) cout<<"x ";
if (space [5]==2) cout<<"o ";
if (space [6]==0) cout<<"- ";
if (space [6]==1) cout<<"x ";
if (space [6]==2) cout<<"o ";
cout<<" 4 5 6";
cout<<endl;
if (space [7]==0) cout<<"- ";
if (space [7]==1) cout<<"x ";
if (space [7]==2) cout<<"o ";
if (space [8]==0) cout<<"- ";
if (space [8]==1) cout<<"x ";
if (space [8]==2) cout<<"o ";
if (space [9]==0) cout<<"- ";
if (space [9]==1) cout<<"x ";
if (space [9]==2) cout<<"o ";
cout<<" 7 8 9";
cout<<endl;
cout<<endl;
cmove=0;
while (cmove!=1)
{
if (loop==1) cout<<"Player x's turn."<<endl;
if (loop==3) cout<<"Player x's turn."<<endl;
if (loop==5) cout<<"Player x's turn."<<endl;
if (loop==7) cout<<"Player x's turn."<<endl;
if (loop==9) cout<<"Player x's turn."<<endl;
if (loop==2) cout<<"Player o's turn."<<endl;
if (loop==4) cout<<"Player o's turn."<<endl;
if (loop==6) cout<<"Player o's turn."<<endl;
if (loop==8) cout<<"Player o's turn."<<endl;
cout<<"Which space do you wish to fill?"<<endl;
cin>>hmove;
cmove=3;
if (hmove==1) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==2) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==3) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==4) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==5) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==6) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==7) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==8) if (space [hmove]==0) cmove=1; else cmove=2;
if (hmove==9) if (space [hmove]==0) cmove=1; else cmove=2;
if (cmove==2) cout<<"That space is already taken."<<endl;
if (cmove==3) cout<<"That is not a valid space."<<endl;
cout<<endl;
}
cmove=0;
if (loop==1) space [hmove]=1;
if (loop==3) space [hmove]=1;
if (loop==5) space [hmove]=1;
if (loop==7) space [hmove]=1;
if (loop==9) space [hmove]=1;
if (loop==2) space [hmove]=2;
if (loop==4) space [hmove]=2;
if (loop==6) space [hmove]=2;
if (loop==8) space [hmove]=2;
if (space [1]==1) {
if (space [2]==1) {
if (space [3]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [4]==1) {
if (space [5]==1) {
if (space [6]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [7]==1) {
if (space [8]==1) {
if (space [9]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [1]==1) {
if (space [4]==1) {
if (space [7]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [2]==1) {
if (space [5]==1) {
if (space [8]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [3]==1) {
if (space [6]==1) {
if (space [9]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [1]==1) {
if (space [5]==1) {
if (space [9]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [3]==1) {
if (space [5]==1) {
if (space [7]==1){
cout<<"Player x wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [1]==2) {
if (space [2]==2) {
if (space [3]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [4]==2) {
if (space [5]==2) {
if (space [6]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [7]==2) {
if (space [8]==2) {
if (space [9]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [1]==2) {
if (space [4]==2) {
if (space [7]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [2]==2) {
if (space [5]==2) {
if (space [8]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [3]==2) {
if (space [6]==2) {
if (space [9]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [1]==2) {
if (space [5]==2) {
if (space [9]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (space [3]==2) {
if (space [5]==2) {
if (space [7]==2){
cout<<"Player o wins!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
if (loop==9){
cout<<"It is a draw!"<<endl;
cout<<"Press any key to play again."<<endl;
getch();
loop=10;
}
}
}
}
Than you for your help.