data validation & namespace

This is a discussion on data validation & namespace within the C++ Programming forums, part of the General Programming Boards category; hello everyone... i'm new to c++ programming previously in visual basic but want to write cross platform code. here is ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Location
    Jilin Province, China
    Posts
    7

    data validation & namespace

    hello everyone...

    i'm new to c++ programming previously in visual basic but want to write cross platform code. here is simple program i tried to make but some behaviors aren't as i expected...i'm using g++ compiler.

    1. How to perform data validation on cin? I've read on the internet about validating numbers and use try catch however if the user enters invalid data the program ends with general protection fault - the input is two characters, how to limit cin to two characters?

    2. I created a namespace 'board' but it still wasn't recognized in my routines despite 'using namespace board;' i still had to add board::board[][] for example....

    3. In the main routine i used a variable 'i' but it kept getting reset to 0 after cin. I tried renaming the variable but it kept happening so i put it in a different namespace 'board' and it no longer was reset to 0.

    Thanks if you can offer any advice.

    Code:
    /* Simple tictactoe program Human vs Human */
    namespace std;
    #include<iostream>
    	
    namespace board {
    	char board[3][3];	// contains the board array	
    	int i=0;	// I put this here because it was getting resest to 0 after cin 
    }
    
    /*
    
    Draw tic tac toe board
    
    */
    void board_draw()
    {
    	using namespace board;
    	cout << "\n";
    	cout << "  A B C \n\n";  
    
    	for (int x=0;x<3;x++) {    
     		cout << x << " ";
        for (int y=0;y<3;y++) {
        	cout << board::board[x][y];
          if (y<2) cout << "|";
    	  }       
        if (x<2) cout << "\n  -----\n";
      }
    	cout << "\n\n";
    }
    
    /* Set tic tac toe board to blanks */
    void board_initialize()
    {
    	using namespace board;
    	for (int x=0;x<3;x++)
    		for (int y=0;y<3;y++)
        	board::board[x][y]=' ';
    }
    
    /*
    
    Set tic tac toe board piece
    
    Ex. board_set("A0",'X');
     
    */
    bool board_set(char c[2],char a)
    {
    	using namespace board;
    	int x,y;
          
    	switch (c[0]) {
    		case 'a':
    		case 'A':
    			y=0;
    	  	break;
    		case 'b':
        	case 'B':
          y=1;
    	  	break;
    		case 'c':
        case 'C':
        	y=2;
        	break;
    	}
    
      switch (c[1]) {
    	case '0':
    		x=0;
      	break;
    	case '1':
        x=1;
    	  break;
    	case '2':
    		x=2;
        break;
      }
    
      board::board[x][y]=a;
    	return true;
    }
    
    /* Get board piece at position X,Y */
    char board_get(int x, int y)
    {
    	int c;
    	using namespace board;
    	c=board::board[x][y];
    	return c;
    }
    
    /* Check for winner return 'X' for X winning or 'Y' for Y winning */
    char board_winner()
    {
    	using namespace board;
      char c[2]={ 'X','Y' };
      char win='\0';
    	for (int i=0;i<2;i++) {
      	for (int x=0;x<3;x++) {    
     			if (board::board[x][0]==c[i] && board::board[x][1]==c[i] && board::board[x][2]==c[i])
    		  	win=c[i];
        	for (int y=0;y<3;y++) {
       		if (board::board[0][y]==c[i] && board::board[1][y]==c[i] && board::board[2][y]==c[i])
    		  	win=c[i]; 
    			}    
    		}
    	if (board::board[0][0]==c[i] && board::board[1][1]==c[i] && board::board[2][2]==c[i])
    		win=c[i];
    	if (board::board[0][2]==c[i] && board::board[1][1]==c[i] && board::board[2][0]==c[i])
    		win=c[i];
     	}
    	return win;
    }
    
    /* main program */
    int main()
    {
    	char c[2];
      char pieces[2]={ 'X', 'Y' };
    	int i=0;
    	
    	board_initialize ();
    	
    	board_draw ();
    
    	while (!board_winner()) {
      	
    		cout << "Enter your move " << pieces[board::i] << ": ";
    		try {
    			std::cin >> c;
     			board_set (c,pieces[board::i]);
    		  board_draw ();
    			board::i++;
    			if (board::i>1) board::i=0; 	
    		}
    
    		catch (out_of_range) { 
      	  std::cin.clear();
    			cout << "\nInvalid entry! Try again: ";
    		}
    	}
      cout << "Winner is: " << board_winner();
    }

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    /* Simple tictactoe program Human vs Human */
    using namespace std;
    #include<iostream>
    #include<string>
    	
    namespace board {
    	char board[3][3];	// contains the board array	
    	int i=0;	// I put this here because it was getting resest to 0 after cin 
    }
    
    /*
    
    Draw tic tac toe board
    
    */
    void board_draw()
    {
    	using namespace board;
    	cout << "\n";
    	cout << "  A B C \n\n";  
    
    	for (int x=0;x<3;x++) {    
     		cout << x << " ";
        for (int y=0;y<3;y++) {
        	cout << board::board[x][y];
          if (y<2) cout << "|";
    	  }       
        if (x<2) cout << "\n  -----\n";
      }
    	cout << "\n\n";
    }
    
    /* Set tic tac toe board to blanks */
    void board_initialize()
    {
    	using namespace board;
    	for (int x=0;x<3;x++)
    		for (int y=0;y<3;y++)
        	board::board[x][y]=' ';
    }
    
    /*
    
    Set tic tac toe board piece
    
    Ex. board_set("A0",'X');
     
    */
    bool board_set(char c[2],char a)
    {
    	using namespace board;
    	int x,y;
          
    	switch (c[0]) {
    		case 'a':
    		case 'A':
    			y=0;
    	  	break;
    		case 'b':
        	case 'B':
          y=1;
    	  	break;
    		case 'c':
        case 'C':
        	y=2;
        	break;
    	}
    
      switch (c[1]) {
    	case '0':
    		x=0;
      	break;
    	case '1':
        x=1;
    	  break;
    	case '2':
    		x=2;
        break;
      }
    
      board::board[x][y]=a;
    	return true;
    }
    
    /* Get board piece at position X,Y */
    char board_get(int x, int y)
    {
    	int c;
    	using namespace board;
    	c=board::board[x][y];
    	return c;
    }
    
    /* Check for winner return 'X' for X winning or 'Y' for Y winning */
    char board_winner()
    {
    	using namespace board;
      char c[2]={ 'X','Y' };
      char win='\0';
    	for (int i=0;i<2;i++) {
      	for (int x=0;x<3;x++) {    
     			if (board::board[x][0]==c[i] && board::board[x][1]==c[i] && board::board[x][2]==c[i])
    		  	win=c[i];
        	for (int y=0;y<3;y++) {
       		if (board::board[0][y]==c[i] && board::board[1][y]==c[i] && board::board[2][y]==c[i])
    		  	win=c[i]; 
    			}    
    		}
    	if (board::board[0][0]==c[i] && board::board[1][1]==c[i] && board::board[2][2]==c[i])
    		win=c[i];
    	if (board::board[0][2]==c[i] && board::board[1][1]==c[i] && board::board[2][0]==c[i])
    		win=c[i];
     	}
    	return win;
    }
    
    /* main program */
    int main()
    {
    	char c[10];
      char pieces[2]={ 'X', 'Y' };
    	int i=0;
    	
    	board_initialize ();
    	
    	board_draw ();
    
    	while (!board_winner()) {
      	    
    		cout << "Enter your move "<< pieces[board::i] << ": ";
    		 
           {
                char alpha[10]={"abcABC"};
                char numb[10]={"012"};
    			std::cin >> c;
    			int size=strlen(c);
    		
    			    if (size==2)
    			    {
    			        for (int a=0; a<6; a++)
    			        {
    			            if(c[0]==alpha[a])
    			            {
    			                for (int b=0;b<3; b++)
    			                {
    			                    if(c[1]==numb[b])
    			                    {
    			
     			                     board_set (c,pieces[board::i]);
    		                         board_draw ();
    			                     board::i++;
    			                     if (board::i>1) board::i=0; 
                                    } 
                                }
                            }
                        }
                    }                    	
    		}
    
    		 
      { 
      	  std::cin.clear();
    		
    		}
    	}
      cout << "Winner is: " << board_winner();
      int stop;
      cin>>stop;
    }

    This compiles in Dev -cpp
    Although you need to sort out the check repeat function. I'll leave that to you
    Last edited by treenef; 07-05-2005 at 02:27 AM.

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    you could do:
    Code:
    void board_draw()
    {
    	using board::board;
    	cout << "\n";
    	cout << "  A B C \n\n";  
    
    	for (int x=0;x<3;x++) {    
     		cout << x << " ";
        for (int y=0;y<3;y++) {
        	cout << board[x][y];
          if (y<2) cout << "|";
    	  }       
        if (x<2) cout << "\n  -----\n";
      }
    	cout << "\n\n";
    }
    Compiles fine

  4. #4
    Registered User
    Join Date
    Jul 2005
    Location
    Jilin Province, China
    Posts
    7
    ok maybe a problem with my compiler?

  5. #5
    Registered User
    Join Date
    Jul 2005
    Location
    Jilin Province, China
    Posts
    7
    thanks treenef for that validation help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data validation
    By darren78 in forum C Programming
    Replies: 5
    Last Post: 02-19-2009, 12:14 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 10:29 AM
  4. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:15 AM
  5. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM

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