C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-10-2009, 05:57 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 18
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);
	   }
spurs01 is offline   Reply With Quote
Old 11-10-2009, 06:04 AM   #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   Reply With Quote
Old 11-10-2009, 06:07 AM   #3
Robot
 
Join Date: Mar 2009
Posts: 100
This will always be true:

Code:
hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N'
Change || to && and it will work.
Memloop is offline   Reply With Quote
Old 11-10-2009, 06:09 AM   #4
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
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
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 06:15 AM   #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   Reply With Quote
Old 11-10-2009, 06:18 AM   #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   Reply With Quote
Old 11-10-2009, 06:20 AM   #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   Reply With Quote
Old 11-10-2009, 06:22 AM   #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();
	   }
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;
	   }
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 06:25 AM   #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'
To get the desired effect, change it to:

Code:
hex != 'y' && hex != 'n' && hex != 'Y' && hex != 'N'
Memloop is offline   Reply With Quote
Old 11-10-2009, 06:28 AM   #10
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
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
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 06:29 AM   #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   Reply With Quote
Old 11-10-2009, 06:31 AM   #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}
I think you should use this one

Code:
if(hex == 'Y' || hex == 'y') {//Blah Blah Blah}
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 06:32 AM   #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   Reply With Quote
Old 11-10-2009, 06:34 AM   #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   Reply With Quote
Old 11-10-2009, 06:35 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:35 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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