![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 18
| Why my while loop is not working ??? pls help 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);
}
|
| spurs01 is offline | |
| | #2 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| First of all the problem in your code is here Code: int hex; // You are taking an int it should be a char
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();
}
and why your last piece of code is not in the while loop as you are using hex there also ??? |
| RockyMarrone is offline | |
| | #3 |
| Robot Join Date: Mar 2009
Posts: 100
| This will always be true: Code: hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N' |
| Memloop is offline | |
| | #4 | |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Quote:
What kind of ........ you are sayin man just check the code if it will be && it will never satisfy the condition think getchar will only return single char it can be y || Y || n || N it can't be y && Y && N && n | |
| RockyMarrone is offline | |
| | #5 |
| Registered User Join Date: Nov 2009
Posts: 18
| Hello everyone , thanks a lot. I used char too. it is still the same problem. And for the following code ---------------------------------------------------------- hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N' ---------------------------------------------------------- I think it is correct. Because if the user does not enter the above keys , it should keep looping unitl the correct key is enter. |
| spurs01 is offline | |
| | #6 |
| Registered User Join Date: Nov 2009
Posts: 18
| using char or int should not be the problem. because in the later part of the problem , i also use int to read in user input and convert it to decimal and later convert it to roman character. |
| spurs01 is offline | |
| | #7 |
| Registered User Join Date: Aug 2006
Posts: 76
| No, it isn't correct. The result is true, no matter what. Enter 'N'. Well, then hex != 'y' is true, no? Enter '8' hex != 'y' is still true... see the point? |
| rdrast is offline | |
| | #8 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Can you tell me what you want to do in a while loop if it is y' || hex != 'n'|| hex != 'Y' || hex != 'N' then go ahead or ask repeating the input again from the user if this is the case then your program is incorrect because your while will work till you enter y || Y || n || N Code: while( hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N')
{
printf("Please enter Y or N only!\n");
hex = getchar();
}
Code: while (1) {
printf("Please enter Y or N only!\n");
hex = getchar();
if (hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N') break;
}
|
| RockyMarrone is offline | |
| | #9 |
| Robot Join Date: Mar 2009
Posts: 100
| Like I wrote, this will ALWAYS evaluate as true: Code: hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N' Code: hex != 'y' && hex != 'n' && hex != 'Y' && hex != 'N' |
| Memloop is offline | |
| | #10 | |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Quote:
| |
| RockyMarrone is offline | |
| | #11 |
| Registered User Join Date: Nov 2009
Posts: 18
| Thanks using && really works !! but i have the second problem if the user enter y then it should proceed with the following code. Inside the code , i still ask user to enter hexadecimal number.... but it does not stop at "hexadecimal = getchar();" , it just continue all the way to the end..... Code: //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')
{
.....
.....
|
| spurs01 is offline | |
| | #12 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Dude this condition i am telling you will never be satisfied look at the code above Code: if(hex == 'Y' && hex == 'y') {//Blah Blah Blah}
Code: if(hex == 'Y' || hex == 'y') {//Blah Blah Blah}
|
| RockyMarrone is offline | |
| | #13 |
| Registered User Join Date: Nov 2009
Posts: 18
| I can't use "switch" because the teacher still did not teach us yet. If so , i might just use scanf . from my program , everyone should be able to see that it is so primitive. no choice. Got reject at the first time |
| spurs01 is offline | |
| | #14 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Haaaaaaaaaa From where the teacher come between you and this forum LOL |
| RockyMarrone is offline | |
| | #15 |
| Registered User Join Date: Nov 2009
Posts: 18
| becuase this is an assignment. he don't come between me n this forum. But he will come between me and my grade !!! |
| spurs01 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| need help with a loop | Darkw1sh | C Programming | 19 | 09-13-2009 09:46 PM |
| funny-looking while loop | Aisthesis | C++ Programming | 3 | 08-30-2009 11:54 PM |
| "try again" loop not working | scwizzo | Game Programming | 5 | 04-01-2007 09:56 PM |
| A mini-compilier I made stuck in an infinite loop (lots of code) | dan06 | C++ Programming | 1 | 10-27-2006 01:21 PM |
| while() loop isn't working right.. | Captain Penguin | C++ Programming | 20 | 10-03-2002 10:29 PM |