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

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    memloop is right, you need to use && instead of ||.

  2. #17
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Ok dude did your problem solved or not what i suggested did you looked that or you are just doing argument here

  3. #18
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by ikislav View Post
    memloop is right, you need to use && instead of ||.
    Hey can you tell me why we need && instead of ||

  4. #19
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    Dear RockyMarrone,
    ou
    To answer your #8 thread :
    I am trying to determine what kind of input the user wants to enter. User wants to enter in hexadecimal format or normal decimal format.

    If the user wants to enter in Hex format then i will jump to the function that perform hexadecimal to decimal.

    My idea is that user can only enter Y or y or N or n. otherwise it will keep prompting for the right key.

  5. #20
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    dear RockyMarrone,

    To answer your #17 thread :
    not quite really solve the problem. I am not doing an argument. I tried the program myself. It seem to go out of control in the later part of the program.

    For example ,
    user enter Y or y , the program should check if user enter Y or y , if so , then prompt user for the
    second input( now in hexadecimal format ). Now the program did not stop to prompt the user
    for the 2nd input. It just run over it ? i am not so sure.

    But if i remove the first part ( that prompt user whether to enter in Hex or decimal format )
    everything is fine again

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spurs01 View Post
    For example ,
    user enter Y or y , the program should check if user enter Y or y , if so , then prompt user for the
    second input( now in hexadecimal format ). Now the program did not stop to prompt the user
    for the 2nd input. It just run over it ? i am not so sure.

    But if i remove the first part ( that prompt user whether to enter in Hex or decimal format )
    everything is fine again
    I think you better post that code as it is now then, presuming you changed it (if you didn't, you need to -- hopefully it is already clear why II should be && there).

    Rocky was wrong for saying you should use a char with getchar(). getchar() returns an int, and that is what you want. However, this will leave a character in the (std)input buffer, because what you actually type is:

    Y\n

    '\n' is from the enter key. So the next call to getchar() will process that, and skip right thru the next question! (The input buffer is persistant -- it does not go away. If you ask for one character and the user enters 2 or 5, the remain characters wait there on a first in, first out basis for a request, eg, with getchar or scanf()). If that is what is happening, insert an extra getchar() that does nothing:
    Code:
    getchar();
    This will eat the newline and clear the input buffer. Even better:
    Code:
    int x;
    while (x != '\n') x=getchar();
    This is guaranteed to eat all spare characters in the buffer up to a newline -- which logically, a '\n' will always be the last character in the stdin buffer.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    With your all post i think you need this kind of code

    Code:
    #include <stdio.h>
    
    #define SPACE ' '
    
    int main() {
      char choice     = SPACE;
      int hexadecimal = 0;
    
      while (1) {
        printf("Please enter your choice \n");
        choice = getchar();
        printf("choice === %c\n", choice);
        if (choice == 'y' || choice == 'Y' ||\
            choice == 'n' || choice == 'N')
          break;
        else
          continue;
      }
    
      if (choice == 'y' || choice == 'Y') {
        printf("Please enter a number in hexadecimal\n");
        scanf("%x", &hexadecimal);
        // Write your logic here
      }
    
      return 0;
    }

  8. #23
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    Thank you very much ! The program is ok.
    The following should be my final code.

    Code:
    #include<stdio.h>
    
    int romanize(int q[], int base)
    {
      int counter, temp;
      
      printf("\nThe Romanize number is ");
      
       if(base == 4)
        {
    	    // 4 digit
    	     for(counter = 1; counter <= q[0]; counter++)
    	       {
    	        printf("m");
    	       }
    	
    	    // 3 digit more than 5
    	     if(q[1] >= 5)
    	      {
    	       temp = q[1] - 5;
    	       printf("d");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		  
    		 // 3 digit less than 5
    	     if(q[1] < 5)
    	      {
    	       for(counter = 1; counter <= q[1]; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		 
    		 
    		 // 2 digit more than 5
    	     if(q[2] >= 5)
    	      {
    	       temp = q[2] - 5;
    	       printf("l");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    
             // 2 digit less than 5
    	     if(q[2] < 5)
    	      {
    	       for(counter = 1; counter <= q[2]; counter++)
    	         {
    		      printf("x");
    		     }
    	      }	
       
             // 1 digit more than 5
    	     if(q[3] >= 5)
    	      {
    	       temp = q[3] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[3] < 5)
    	      {
    	       for(counter = 1; counter <= q[3]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    
    
        if(base == 3)
        {
    	    // 3 digit more than 5
    	     if(q[0] >= 5)
    	      {
    	       temp = q[0] - 5;
    	       printf("d");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		  
    		 // 3 digit less than 5
    	     if(q[0] < 5)
    	      {
    	       for(counter = 1; counter <= q[0]; counter++)
    	         {
    		      printf("c");
    		     }
    	      }
    		 
    		 
    		 // 2 digit more than 5
    	     if(q[1] >= 5)
    	      {
    	       temp = q[1] - 5;
    	       printf("l");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    
             // 2 digit less than 5
    	     if(q[1] < 5)
    	      {
    	       for(counter = 1; counter <= q[1]; counter++)
    	         {
    		      printf("x");
    		     }
    	      }	
       
             // 1 digit more than 5
    	     if(q[2] >= 5)
    	      {
    	       temp = q[2] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[2] < 5)
    	      {
    	       for(counter = 1; counter <= q[2]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    	
    	
    	if(base == 2)
        {
    	    // 2 digit more than 5
    	     if(q[0] >= 5)
    	      {
    	       temp = q[0] - 5;
    	       printf("l\n");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    		  
    		 // 2 digit less than 5
    	     if(q[0] < 5)
    	      {
    	       for(counter = 1; counter <= q[0]; counter++)
    	         {
    		      printf("x");
    		     }
    	      }
    		 
    		// 1 digit more than 5
    	     if(q[1] >= 5)
    	      {
    	       temp = q[1] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[1] < 5)
    	      {
    	       for(counter = 1; counter <= q[1]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    	
    	
    	if(base == 1)
        {
    	    // 1 digit more than 5
    	     if(q[0] >= 5)
    	      {
    	       temp = q[0] - 5;
    	       printf("v");
    	       for(counter = 1; counter <= temp; counter++)
    	         {
    		      printf("i");
    		     }
    	      }
    
             // 1 digit less than 5
    	     if(q[0] < 5)
    	      {
    	       for(counter = 1; counter <= q[0]; counter++)
    	         {
    		      printf("i");
    		     }
    	      }   
    	}
    	
    }
    
    main()
    {
        int i, j, n, ch, count, hexadecimal, decimal, number[10], temp[10];
    	int hex;
        decimal=0, count=0, i=0, j=0;
    	
          printf("Enter in hexadecimal format ? (Y/N):");
    	  hex = getchar();
    	  printf("\n");
    	 while(hex != 'Y' && hex != 'y' && hex != 'N' && hex != 'n')
    	   {
    	     getchar();
    		 printf("Please enter the correct key! (Y/N):");
    		 hex = getchar();
    		 printf("\n");
    	   } 
    	 //if yes then propmt user for hexadecimal input 
    	 if(hex == 'Y' || hex == 'y')
          {
           	  getchar();
    	      printf("Please enter in hexadecimal format : ");
              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;
             }
    
             hexadecimal = getchar();
    		}
    
            printf("\nyour input number in decimal format is %d\n",decimal); 
    
           while(decimal != 0)
            {
             temp[i] = decimal%10;
             decimal= decimal/10;
             i++;
             count++;
            }
    
    		i=i-1;		
           for(j=0; j < count; j++)
            {
             number[j]=temp[i];
    		 i--;
            }
    		
    		
    		if(count < 5 ) // Handle number up to 9999 only
    	     {
              romanize(number,  count);
    	     } 
    	    else
            printf("\nPlease enter hexadecimal number up to 270F only.\n");
           
    		
    	  } 
    	  
    	  else
    	  {
    	      ch = getchar();
    	      printf("Please enter in decimal format : ");
              ch = getchar();
    	  
    	    while(ch!='\n')
              {
                if('0' <= ch && ch <= '9')
    	         {
    	          //This is to count the number of digit the user has enter//
    		      n = 0;
    		      n = n * 10;
    		      n = n + (ch - '0');
    		      number[i] = n;     //Store each entered digit into array "intnumber[]"//
    		      count++;
    		      i++;
    	         }
    	  
    	       ch = getchar();
              }
    	  
    	    if(count < 5 ) // Handle number up to 9999 only
    	     {
              romanize(number,  count);
    	     } 
    	    else
            printf("\nPlease enter number up to 9999 only.\n");
    	  
    	  
    	  }	  
    
    	 
        
    }

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, 09:46 PM
  2. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  3. "try again" loop not working
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 04-01-2007, 09:56 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. while() loop isn't working right..
    By Captain Penguin in forum C++ Programming
    Replies: 20
    Last Post: 10-03-2002, 10:29 PM