Skipping a loop?

This is a discussion on Skipping a loop? within the C++ Programming forums, part of the General Programming Boards category; hey, guys I have another question regarding my program. The code is below, and the problem I'm having is that ...

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

    Skipping a loop?

    hey, guys

    I have another question regarding my program. The code is below, and the problem I'm having is that when the program is executed, and a wrong position is given, i.e. A2 a prompt message come up to eter a new position. The next input position, whether bad or wrong, does not show up, even thought the board does.On the following turn everything works fine again...what could be wrong?

    thanks,

    axon

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    using namespace std;
    
    	//declaring global variables that will serve as positions on board
    char p1, p2 ,p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14;
    char p15, p16, p17, p18, p19, p20, p21, p22, p23, p24;
    
    	
    	//function displaying the instructions
    void displayInstructionsInfo()
    {
    	cout << "Each player has nine pieces. The players take turns putting their \n"
    		 << "pieces (one at a time) on the board at places where lines meet. \n"
    		 << "Each player tries to get three pieces in a row (a \"mill\") while \n"
    		 << "preventing her opponent from doing the same.\n\n" 
    		 << "When a player gets three pieces in a row then she may take one of her \n"
    		 << "opponent's pieces off the board. After both players have put all of \n"
    		 << "their pieces on the board, a piece is moved by sliding it from its \n"
    		 << "space to a neighboring empty space. \n\n"
    		 << "Players keep trying to get three pieces in a row so they can take \n"
    		 << "away an opponent's piece. The game is over when one player \n"
    		 << "is left with two pieces.\n\n"
    		 << "Moves are made by referencing a row and column, e.g. A1 is the upper\n"
    		 << "left corner.\n";
    	cout << endl;
    }
    
    	//function displaying the board setup
    void displayBoard()
    {
    	cout << "\n";
    	cout << "   1   2   3  4  5   6   7\n"
    		 <<	"A  "<<p1<<".........."<<p2<<".........."<<p3<<"  A\n"
    	  	 <<	"   .          .          .\n"
    		 <<	"B  .   "<<p4<<"......"<<p5<<"......"<<p6<<"   .  B\n"   
    		 <<	"   .   .      .      .   .\n"
    		 <<	"C  .   .   "<<p7<<".."<<p8<<".."<<p9<<"   .   .  C\n"
    		 <<	"   .   .   .     .   .   .\n"
    		 <<	"D  "<<p10<<"..."<<p11<<"..."<<p12<<"     "<<p13<<"..."<<p14<<"..."<<p15<<"  D\n"
    		 <<	"   .   .   .     .   .   .\n"
    		 <<	"E  .   .   "<<p16<<".."<<p17<<".."<<p18<<"   .   .  E\n"
    		 <<	"   .   .      .      .   .\n"
    		 <<	"F  .   "<<p19<<"......"<<p20<<"......"<<p21<<"   .  F\n"   
    		 <<	"   .          .          .\n"
    		 <<	"G  "<<p22<<".........."<<p23<<".........."<<p24<<"  G\n\n"
    		 <<	"   1   2   3  4  5   6   7\n";
    	cout << endl;
    }
    
    	//function converting user inputed position to one of the global vars
    void determineCorespondingPositionVariable(char row, int column)
    {
    	if(row=='A' && column == 1){p1='x';}
    	else if(row=='A' && column == 4){p2='x';}
    	else if(row=='A' && column == 7){p3='x';}
    	else if(row=='B' && column == 2){p4='x';}
    	else if(row=='B' && column == 4){p5='x';}
    	else if(row=='B' && column == 6){p6='x';}
    	else if(row=='C' && column == 3){p7='x';}
    	else if(row=='C' && column == 4){p8='x';}
    	else if(row=='C' && column == 5){p9='x';}
    	else if(row=='D' && column == 1){p10='x';}
    	else if(row=='D' && column == 2){p11='x';}
    	else if(row=='D' && column == 3){p12='x';}
    	else if(row=='D' && column == 5){p13='x';}
    	else if(row=='D' && column == 6){p14='x';}
    	else if(row=='D' && column == 7){p15='x';}
    	else if(row=='E' && column == 3){p16='x';}
    	else if(row=='E' && column == 4){p17='x';}
    	else if(row=='E' && column == 5){p18='x';}
    	else if(row=='F' && column == 2){p19='x';}
    	else if(row=='F' && column == 4){p20='x';}
    	else if(row=='F' && column == 6){p21='x';}
    	else if(row=='G' && column == 1){p22='x';}
    	else if(row=='G' && column == 4){p23='x';}
    	else if(row=='G' && column == 7){p24='x';}
    
    }
    
    	
    
    
    	//function displaying 'to move' prompt and storing move position
    void displayPromptAndGetMove(char& letter, int& number)
    {
    
    
    	cout << "Player x has x pieces left. Enter position to move: ";
    	cin >> letter >> number;
    
    	letter = toupper(letter);
    
    		
    }
    
    	//function displaying message if invalid position
    void displayInvalidMessagePrompt()
    {
    	char letter;
    	int number;
    
    	cout << "Sorry, this move is invalid\n"; 
    	
    	displayPromptAndGetMove(letter, number);
    
    }
    	
    
    
    	//function that checks if position is legal
    void determinIfPositionOK(char row,int column)
    {
    	
    	if(row=='A' && column == 2){displayInvalidMessagePrompt();}
    	else if(row=='A' && column==3 ){displayInvalidMessagePrompt();}
    	else if(row=='A' && column==5 ){displayInvalidMessagePrompt();}
    	else if(row=='A' && column==6 ){displayInvalidMessagePrompt();}
    	else if(row=='B' && column==1 ){displayInvalidMessagePrompt();}
    	else if(row=='B' && column==3 ){displayInvalidMessagePrompt();}
    	else if(row=='B' && column==5 ){displayInvalidMessagePrompt();}
    	else if(row=='B' && column==7 ){displayInvalidMessagePrompt();}
    	else if(row=='C' && column==1 ){displayInvalidMessagePrompt();}
    	else if(row=='C' && column==2 ){displayInvalidMessagePrompt();}
    	else if(row=='C' && column==6 ){displayInvalidMessagePrompt();}
    	else if(row=='C' && column==7 ){displayInvalidMessagePrompt();}
    	else if(row=='D' && column==4 ){displayInvalidMessagePrompt();}
    	else if(row=='E' && column==1 ){displayInvalidMessagePrompt();}
    	else if(row=='E' && column==2 ){displayInvalidMessagePrompt();}
    	else if(row=='E' && column==6 ){displayInvalidMessagePrompt();}
    	else if(row=='E' && column==7 ){displayInvalidMessagePrompt();}
    	else if(row=='F' && column==1 ){displayInvalidMessagePrompt();}
    	else if(row=='F' && column==3 ){displayInvalidMessagePrompt();}
    	else if(row=='F' && column==5 ){displayInvalidMessagePrompt();}
    	else if(row=='G' && column==3 ){displayInvalidMessagePrompt();}
    	else if(row=='G' && column==5 ){displayInvalidMessagePrompt();}
    	else if(row=='G' && column==6 ){displayInvalidMessagePrompt();}
    	else {determineCorespondingPositionVariable(row, column);}
    }
    
    
    
    
    int main()
    {
    
    		//initializing all position variables to '.'
    	p1 = p2 = p3 = p4 = p5 = p6 = p7 = p8 = p9 = p10 = p11 = p12 = p13 = p14 = '.';
    	p15 = p16 = p17 = p18 = p19 = p20 = p21 = p22 = p23 = p24 = '.';
    
    	bool notDone = true;
    	
    	
    
    		//function displayin authors information
    	//displayIdInfo();
    
    		//function displaying game instructions
    	displayInstructionsInfo();
    
    		//function displaying the gane board
    	displayBoard();
    
    	while(notDone){
    	
    	char letter=' ';
    	int number=0;
    	
    	
    		//determinePlayerToMove();
    			
    			//function displaying 'to move' prompt and storing move position
    		displayPromptAndGetMove(letter, number);
    		
    		
    		determinIfPositionOK(letter, number);
    
    		displayBoard();
    
    	
    
    
    
    
    	}
    
    
    	return 0;
    }
    Last edited by axon; 02-16-2003 at 07:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

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