Why my while loop is not working ??? pls help

This is a discussion on Why my while loop is not working ??? pls help within the C Programming forums, part of the General Programming Boards category; Hi everyone , I am writing a program that require user to choose whether to enter in hexadecimal form or ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    21

    Why my while loop is not working ??? pls help

    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);
    	   }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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 ???

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This will always be true:

    Code:
    hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N'
    Change || to && and it will work.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Memloop View Post
    This will always be true:

    Code:
    hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N'
    Change || to && and it will work.

    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

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    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.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    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.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    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?

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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();
    	   }
    if you want to exit from this when these choice are there just change your code to

    Code:
    	while (1) {
    	        printf("Please enter Y or N only!\n");
    		hex = getchar();
                    if (hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N') break;
    	   }

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Like I wrote, this will ALWAYS evaluate as true:

    Code:
    hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N'
    To get the desired effect, change it to:

    Code:
    hex != 'y' && hex != 'n' && hex != 'Y' && hex != 'N'

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Memloop View Post
    Like I wrote, this will ALWAYS evaluate as true:

    Code:
    hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N'
    To get the desired effect, change it to:

    Code:
    hex != 'y' && hex != 'n' && hex != 'Y' && hex != 'N'
    Yup this can be done but i think for this we can use switch that will be better than if and than just break from the loop

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    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')
               {
                 .....
                 .....

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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}
    I think you should use this one

    Code:
    if(hex == 'Y' || hex == 'y') {//Blah Blah Blah}

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    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

  14. #14
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Haaaaaaaaaa From where the teacher come between you and this forum LOL

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    becuase this is an assignment. he don't come between me n this forum. But he will come between me and my grade !!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 10:46 PM
  2. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2009, 12:54 AM
  3. "try again" loop not working
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 04-01-2007, 10:56 PM
  4. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  5. while() loop isn't working right..
    By Captain Penguin in forum C++ Programming
    Replies: 20
    Last Post: 10-03-2002, 11:29 PM

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