Hi newbie there,this is code for chess game(just started) ,i want to convert the input in characters to integers via getMove function i have tried to made but its not working..
output is "invalid input" every time

#include <iostream>
using namespace std;

void printBoard(char board[][8],int row,int col);
void getMove(int arr[],char sr,int sc,char dr,int dc);
bool validMove(int a[],int n);


void main (){
	char sr,dr;
	int sc,dc;
	int n=4;
	
	cout <<"Enter source coordinates      :"  ;
	cin >> sr;
	cin >> sc;

	cout <<"Enter destination coordinates :" ;
	cin >> dr;
	cin >> dc;
	int move[4]={sr,sc,dr,dc};
		
	int row=8;int col=8;
	sr=toupper(sr);//capitalize char coordinates
	dr=toupper(dr);
	
	getMove( move, sr, sc, dr, dc);
	 
     bool valid =validMove( move,n);
	 if(valid == false){

	 cout << "Invalid input!";
	 }
	 
	 cout << endl;cout << endl;
	char board[8][8];
	printBoard(board,row,col);
		
}



void printBoard(char board[][8],int row,int col){



	for(int i=0;i<8;i++){
	  for(int j=0;j<8;j++){

	   
		 
		   if (i==1 ){
			  board[i][j]='P';
		  cout << " " << board[i][j] << '|' << " ";

	      }else if (i==6 ){
			  board[i][j]='p' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==0 && j==0 || i==0 && j==7 ){//Rook
			  board[i][j]='R' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==7 && j==0 || i==7 && j==7 ){//rook
			  board[i][j]='r' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==0 && j==1 || i==0 && j==6 ){//Night
			  board[i][j]='N' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==7 && j==1 || i==7 && j==6 ){//night
			  board[i][j]='n' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==0 && j==2 || i==0 && j==5 ){//Bishop
			  board[i][j]='B' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==7 && j==2 || i==7 && j==5 ){//bishop
			  board[i][j]='b' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==0 && j==3  ){               //Queen
			  board[i][j]='Q' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==7 && j==3  ){               //queen
			  board[i][j]='q' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==0 && j==4  ){               //King
			  board[i][j]='K' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else if (i==7 && j==4  ){              //king
			  board[i][j]='k' ;
		  cout << " " << board[i][j] << '|' << " ";
		   }else{
		  
			  
		   board[i][j]= ' ';
		  cout << " " <<  board[i][j]   << "|" << " ";
		   board[0][1]= 'R';
		  }



	  }
	  cout << endl ;
	   cout << "_______________________________";
	   cout << endl ;
	}



}

void getMove(int arr[],char sr,int sc,char dr,int dc){

	arr[0]=sr-65;//get ascii of rows 
	arr[1]=sc-1;//get index started from 0
	arr[2]=dr-65;
	arr[3]=dc-1;

}

bool validMove(int a[],int n){
	 n=4;
 
	
	      if(a[0] != 0 ||a[0] != 1 ||a[0] != 2 ||a[0] != 3 ||a[0] != 4 ||a[0] != 5 ||a[0] != 6 ||a[0] != 7  ){

		return false;
	}else if(a[2] != 0 ||a[2] != 1 ||a[2] != 2 ||a[2] != 3 ||a[2] != 4 ||a[2] != 5 ||a[2] != 6 ||a[2] != 7 ){

		return false;
	}else if(a[1] != 0 ||a[1] != 1 ||a[1] != 2 ||a[1] != 3 ||a[1] != 4 ||a[1] != 5  ||a[1] != 6 ||a[1] != 7  ){

		return false;
	}else if(a[3] != 0 ||a[3] != 1 ||a[3] != 2 ||a[3] != 3 ||a[3] != 4 ||a[3] != 5  ||a[3] != 6 ||a[3] != 7  ){

		return false;
	}else {
		return true;}

}

Whether it be making ascii to int conversions, or splitting up a string into tokens, probably the best method to use is <stringstream>. Here is a small example:

// using stringstream constructors.
#include <iostream>
#include <sstream>
using namespace std;

int main () {

  int val = 0;
  stringstream ss;

  ss << "120 42 377 6 5 2000";

  for (int n=0; n<6; n++)
  {
    ss >> val;
    cout << val*2 << endl;
  }

  return 0;
}

and it works both ways (int to ascii).

void getMove(int arr[],int sr,int sc,int dr,int dc);


void getMove(int arr[],int sr,int sc,int dr,int dc){


arr[0]=sr-65;//get ascii of rows
arr[1]=sc-1;//get index started from 0
arr[2]=dr-65;
arr[3]=dc-1;


}

how can u subtract int value(65) from char value ???? no na.... so make this changes in ur progam ur getmove function will hold all the integer value. to check -> display a[0].

not getting wat ur trying to do with ur ....validmove function ??? hmmmm hope OR logic is right in this case ??

Actually for some of you, you can subtract an integer from a character, as characters and integers are just zeros and ones in the computer. So basically a character can be interpreted as an integer with value equal to its ascii code. Now, learner guy, ifyou don't want to use a stringstream try subtracting 42 or 48 from the character, I think one of these is the right number.

Actually user is giving one coordinate of input in alphabets(A to H) and the other one in integers(from 1 to 8)

so the thing that is required is the conversion of alphabet to a value between 0 and 7 e.g if A is entered then it has ascii value 65 so my formula is

(sr-65)
as sr is A(with ascii 65 )
so 65-65=0
0 is what i wan in this case
this is what i am not getting ..My program is giving every input as invalid input because it is unable to convert from alphabet to integer(from 0 to 7)...

Plz help and do ask questions if u have

try subtracting 42 or 48 from the character, I think one of these is the right number.

It's 48 in ASCII and Unicode. But you don't have to remember what the value is if you simply subtract the character '0':

for (char x = '0'; x <= '9'; x++)
    cout << x - '0' << '\n';

(sr-65)
as sr is A(with ascii 65 )
so 65-65=0
0 is what i wan in this case

bool chess_coord(char col, char row, int& x, int& y)
{
    col = toupper((unsigned char)col);
    
    if ((col < 'A' || col > 'H') &&
        (row < 1 || row > 8))
    {
        return false;
    }
    
    x = col - 'A';
    y = row - '0' - 1; // -1 to force 0-based indexing

    return true;
}

This assumes coordinates will be 1-based coming in and 0-based going out, so the ranges of 1-8(rows) and A-H(columns) will be converted to 0-7.

thanksyou so so much Narue ,its working ...can u plz explain in a bit detailed way where i was wrong

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.