1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

do while prob on validation

Discussion in 'C' started by musicmancanora4, Mar 15, 2006.

  1. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys my while condition isnt working working properly in validateMonth what happens it just stays in there. If i type in 5 or any number between 0-12 it will still say its the wrong input. Why isnt it testing the condition properly?



    Code:
    unsigned getMonth()
    {
       
       /*** declare variables*/
      
      unsigned  tmpMonth;
      int  valid = 0;
      char *prompt = "Please enter a month between 0 - 12 !\n";
      char *month;
      unsigned valMonth;
      
    
      
      month = getUserInput(prompt);
      tmpMonth = validateMonth(valMonth, prompt);
      
    
       
       return EXIT_SUCCESS;
    
    }
    


    Code:
    unsigned validateMonth(unsigned month, char *prompt)
    {
         unsigned m;
         int flag = FALSE; /* false value*/
       do
       {
         while(flag == TRUE || month<0 || month>12) /* flag 1 for true*/
         {
    	flag = FALSE;
            printf("Month error 0 or less or equal to 12 please\n");
    	getUserInput(prompt);
            
         }
       }while(flag !=FALSE);
         
         return m;
    }
    
    
    
    
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    I have lots of queries for your validateMonth function.

    1. Why do you need do while and while loops at all.
    2. Its always an infinite loop because you have FLAG = FALSE as initial values and then you have an OR condition.
    3. You have never assigned FLAG as true but then also testing that almost in 2 while and do while loops.
    4. You are returning m which is not used in the function and so the return value is always a garbage.

    Cant it be done as
    Code:
    unsigned validateMonth(unsigned month, char *prompt)
    {
    	unsigned m =0;
    	if(month<0 || month>12) /* flag 1 for true*/
    	{
    		printf("Month error 0 or less or equal to 12 please\n");
    		getUserInput(prompt);
    		m = 1;
    	}
    	return m;
    }
     
  3. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    iv got the while loops so that if it is an error i want it to re get the user input
     
  4. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    iv used that and it still dosnt test the condition properly .... i enter for example 5 which is between
    0-12 and it still falls inside the if and says "month error 0 or less then equal to 12" then i type it a second type and it works.. why is that?
     
  5. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Then also you dont need nested ones and also you are not using the new input of user each time.
     
  6. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Thats because the month you are inputing is in different variable than that passed to the validate month variable.
    Code:
    month = getUserInput(prompt);
    tmpMonth = validateMonth(valMonth, prompt);
     
  7. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Should i be testing for the result variable instead inside the validation function rather then the "month" variable?

    Is that what u mean by the different variable?
     
  8. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    No what I meant is you are inputing the value 5 in one variable and testing it in other.
     
  9. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    im lost completely sorry .... its something simple but i dont get what your trying to say?
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    What he is trying to say is
    You are taking the input from the user in the variable month which is a local variable in getMonth function.
    month = getUserInput(prompt);
    Then you are calling the validate month function with valMonth another variable which is never initialized as
    tmpMonth = validateMonth(valMonth, prompt);
    Then valMonth is always has a grabage value.

    How do you expect it to validate the input taken from the getUserInput function.

    Thanks
    Shabbir
     
  11. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    its kewl guys i fixed it thnks for ur help
     
  12. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Our pleasure. If you have any other query drop a line or two here.
     

Share This Page