skipping input?

This is a discussion on skipping input? within the C++ Programming forums, part of the General Programming Boards category; Hello guys, I'm writing a simple tic-tac-toe game just to get some practice with function (for those that read my ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    skipping input?

    Hello guys,

    I'm writing a simple tic-tac-toe game just to get some practice with function (for those that read my previous post), and I have encounterd a problem which I've been trying to solve for sometime.

    The problem is that if an invalid position has been entered and the lop goes back to the prompt again and a new CORRECT position has been entered it simply says it is wrong again.

    Below I'm posting the two functions that handle that process.If anyone could help I would greaty appreciate it!

    and by the way I don't know how to use arrays therefore it is all functions.

    axon

    Code:
    //function switching the coordinates to the global vars
    int switchCoordinatesToPosition(char letterPosition, int numberPosition, int&badMove)
    {
    	if( (letterPosition == 'A') && (numberPosition == 1) ) 
    	{
    		if(p1 != ' ') {badMove = -1;}
    		else{ return 1; }
    	}
    	else if( (letterPosition == 'A') && (numberPosition == 2) )
    	{
    		if(p2 != ' ') {badMove = -1;}
    		else{ return 2; }
    	}
    	else if( (letterPosition == 'A') && (numberPosition == 3) ) 
    	{
    		if(p3 != ' ') {badMove = -1;}
    		else{ return 3; }
    	}
    	else if( (letterPosition == 'B') && (numberPosition == 1) ) 
    	{
    		if(p4 != ' ') {badMove = -1;}
    		else{ return 4; }
    	}
    	else if( (letterPosition == 'B') && (numberPosition == 2) ) 
    	{
    		if(p5 != ' ') { badMove = -1;}
    		else{ return 5; }
    	}
    	else if( (letterPosition == 'B') && (numberPosition == 3) ) 
    	{
    		if(p6 != ' ') { badMove = -1;}
    		else{ return 6; }
    	}
    	else if( (letterPosition == 'C') && (numberPosition == 1) ) 
    	{
    		if(p7 != ' ') {badMove = -1;}
    		else{ return 7; }
    	}
    	else if( (letterPosition == 'C') && (numberPosition == 2) ) 
    	{
    		if(p8 != ' ') { badMove = -1;}
    		else{ return 8; }
    	}
    	else if( (letterPosition == 'C') && (numberPosition == 3) ) 
    	{
    		if(p9 != ' ') { badMove = -1;}
    		else{ return 9; }
    	}
    	else { cout << "\nInvalid command...exiting!"; exit(-1);}	//default escape
    
    }
    
    //=================================================================================
    
    	//function displaying the 'to move' prompt
    void displayMovePrompt(char& playerValue, char& letterPosition, int& numberPosition, 
    					   int& positionNumberOnBoard, int badMove)
    {
    	do
    	{
    		switch(playerValue){
    			case 'X': cout << "\n\nPlayer X type in a move position: ";
    				break;
    			case 'O': cout << "\n\nPlayer O type in a move position: ";
    				break;
    		}
    		cin >> letterPosition >> numberPosition;
    	
    		letterPosition = toupper(letterPosition);	//convert to CAPS
    
    				//switch to position number
    		positionNumberOnBoard = switchCoordinatesToPosition(letterPosition, numberPosition, badMove);
    
    				//check for bad imput
    		if(badMove == -1)
    		{
    			cout << "That is an invalid move!";
    			badMove = -1;
    		}
    	}
    	while(badMove == -1);
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I guess I'll aswer myself then:

    It works perfectly well when I initialized badMove to equal one inside the do statements.

    Can anyone tell me exactlywhy this happens?

    thanks

    axon

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    attach the whole cpp file.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by RoD
    attach the whole cpp file.
    I figured it out, but here is the attachments of the whole cpp file....warning...its still in development.

    axon

    Code:
    /*
    	Program:	Simple tic-tac-toe game
    	Date:		02/21/03
    */
    
    
    #include <iostream>
    #include <cctype>
    #include <cstdlib>
    using namespace std;
    
    	//declaring global variables for the positions on the board
    char p1, p2, p3, p4, p5, p6, p7, p8, p9;
    
    	//function displaying the game instructions
    void displayInstructions()
    {
    	cout << "\tWelcome To TicTacToe!!\n\n"
    		 << "Play against another player or the computer,\n"
    		 << "     by choosing from the menu below.\n\n"
    		 << "Enter positions as a letter and number.\n"
    		 << "\t    ENJOY!!\n\n\n";
    }
    
    void displayMenu(int& menuOption)
    {
    	cout << "Choose from the following options:\n"
    		 << "     1. Two Player Game\n"
    		 << "     2. Single Player as 'X'\n"
    		 << "     3. Single Player as 'O'\n"
    		 << "Selection: ";
    	cin >> menuOption;
    
    	if(menuOption == 1)
    		{cout << "\n\n   You chose a Single Player Game!\n\n";}
    	else if(menuOption == 2)
    		{cout << "\n\n   You chose to play against the computer as 'X'\n\n";}
    	else if(menuOption == 3)
    		{cout << "\n\n   You chose to play against the computer as 'O'\n\n";}
    }
    
    //================================================================================
    	//function displaying the game board
    void displayBoard()
    {
    	cout << "\n\n"
    		 << "\t    1   2   3 \n"
    		 << "\t A  "<<p1<<" | "<<p2<<" | "<<p3<<" \n"
    		 << "\t   ----------- \n"
    		 << "\t B  "<<p4<<" | "<<p5<<" | "<<p6<<"  \n"
    		 << "\t   ----------- \n"
    		 << "\t C  "<<p7<<" | "<<p8<<" | "<<p9<<"  \n\n";
    	cout << endl;
    }
    
    
    
    //================================================================================
    	//function switching the coordinates to the global vars
    int switchCoordinatesToPosition(char letterPosition, int numberPosition, int&badMove)
    {
    	if( (letterPosition == 'A') && (numberPosition == 1) ) 
    	{
    		if(p1 != ' ') {badMove = -1;}
    		else{ return 1; }
    	}
    	else if( (letterPosition == 'A') && (numberPosition == 2) )
    	{
    		if(p2 != ' ') {badMove = -1;}
    		else{ return 2; }
    	}
    	else if( (letterPosition == 'A') && (numberPosition == 3) ) 
    	{
    		if(p3 != ' ') {badMove = -1;}
    		else{ return 3; }
    	}
    	else if( (letterPosition == 'B') && (numberPosition == 1) ) 
    	{
    		if(p4 != ' ') {badMove = -1;}
    		else{ return 4; }
    	}
    	else if( (letterPosition == 'B') && (numberPosition == 2) ) 
    	{
    		if(p5 != ' ') { badMove = -1;}
    		else{ return 5; }
    	}
    	else if( (letterPosition == 'B') && (numberPosition == 3) ) 
    	{
    		if(p6 != ' ') { badMove = -1;}
    		else{ return 6; }
    	}
    	else if( (letterPosition == 'C') && (numberPosition == 1) ) 
    	{
    		if(p7 != ' ') {badMove = -1;}
    		else{ return 7; }
    	}
    	else if( (letterPosition == 'C') && (numberPosition == 2) ) 
    	{
    		if(p8 != ' ') { badMove = -1;}
    		else{ return 8; }
    	}
    	else if( (letterPosition == 'C') && (numberPosition == 3) ) 
    	{
    		if(p9 != ' ') { badMove = -1;}
    		else{ return 9; }
    	}
    	else { cout << "\nInvalid command...exiting!"; exit(-1);}	//default escape
    
    }
    
    //=================================================================================
    
    	//function displaying the 'to move' prompt
    void displayMovePrompt(char& playerValue, char& letterPosition, int& numberPosition, 
    					   int& positionNumberOnBoard, int badMove)
    {
    	
    	
    	do
    	{
    		badMove=1;		
    		switch(playerValue){
    			case 'X': cout << "\n\nPlayer X type in a move position: ";
    				break;
    			case 'O': cout << "\n\nPlayer O type in a move position: ";
    				break;
    		}
    		cin >> letterPosition >> numberPosition;
    	
    		letterPosition = toupper(letterPosition);	//convert to CAPS
    
    				//switch to position number
    		positionNumberOnBoard = switchCoordinatesToPosition(letterPosition, numberPosition, badMove);
    
    				//check for bad imput
    		if(badMove == -1)
    		{
    			cout << "That is an invalid move!";
    			badMove = -1;
    		}
    	}
    	while(badMove == -1);
    }
    
    //================================================================================
    	//function making the move
    void makeTheMove(int positionNumberOnBoard, char playerValue)
    {
    	switch(positionNumberOnBoard) {
    		case 1: p1 = playerValue; break;
    		case 2: p2 = playerValue; break;
    		case 3: p3 = playerValue; break;
    		case 4: p4 = playerValue; break;
    		case 5: p5 = playerValue; break;
    		case 6: p6 = playerValue; break;
    		case 7: p7 = playerValue; break;
    		case 8: p8 = playerValue; break;
    		case 9: p9 = playerValue; break;
    		default: cout << "Error !MaThMo!\n"; exit(-1); break;
    	}
    
    }
    
    //=================================================================================
    	
    
    int main()
    {
    	p1=p2=p3=p4=p5=p6=p7=p8=p9=' ';		//setting the globals to " "
    
    	bool notDone = true;
    
    	char playerValue = 'X';		//setting the player value to X, as X starts
    								//the game
    
    	char letterPosition;		//value of the letter coordinate
    	int numberPosition;			//value of the number coordinate
    	int positionNumberOnBoard;
    	int menuOption;				//value for the beginning menu
    	int badMove;
    	
    
    	displayInstructions();		//displaying instructions
    	displayMenu(menuOption);	//displaying the menu
    	displayBoard();
    
    	while(notDone)
    	{
    		if(menuOption == 1)
    		{
    			displayMovePrompt(playerValue, letterPosition, numberPosition,
    							  positionNumberOnBoard, badMove); //displaying which player's move
    		
    			makeTheMove(positionNumberOnBoard, playerValue);
    
    			displayBoard();
    			
    			//switchPlayers();
    			
    			//checkForMill();
    
    			//areWeDone();
    		}
    
    	}
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21