Hi everyone ,
I am writing a program that require user to choose whether to enter in hexadecimal form or in normal form. The 'while" is to check whether the user enter the correct key or not. If not, then it will always prompt the user.
My problem is why the while does not break away even i enter the correct key ( y or n )??
Can anyone help me with this.
Code://Ask user whether to input in hexadecimal form or not int hex; printf("Do you want to input in Hexdecimal? Y/N : "); hex = getchar(); while( hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N') { printf("Please enter Y or N only!\n"); hex = getchar(); } //-------------------------------------------------------- //if user input 'Y' or 'y' then proceed with the following if(hex == 'Y' || hex == 'y') { printf("Please enter in hexadecimal form : \n"); hexadecimal = getchar(); while(hexadecimal != '\n') { if('0' <= hexadecimal && hexadecimal <= '9') { decimal = decimal * 16; decimal = decimal + (hexadecimal - '0'); } else if('A' <= hexadecimal && hexadecimal <= 'F') { decimal = decimal * 16; decimal = decimal + (hexadecimal - 'A')+10; } else if('a' <= hexadecimal && hexadecimal <= 'f') { decimal = decimal * 16; decimal = decimal + (hexadecimal - 'a')+10; } else { break; } count++; i++; hexadecimal = getchar(); } printf("The decimal for hexadecimal is : %d\n",decimal); }