Trying to validate an entry, New to C

This is a discussion on Trying to validate an entry, New to C within the C Programming forums, part of the General Programming Boards category; The program compiles, but when I enter the number 22 it falls out of the while loop. I want to ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    Trying to validate an entry, New to C

    The program compiles, but when I enter the number 22 it falls out of the while loop. I want to tell the operator that they entered an incorrect value and please try again and again.... if neccessary. I am not sure where my logic is flawed.

    Thank You for lookin at this.

    Code:
     /*
     *Loop till valid entry with message
     */
    #include <stdio.h>   
    
    int main(void)
    
    {
    	double value;   // stores numerical value  
    	int exitvalue;    //declatarations
    	exitvalue=0;     //initialize variable
    
    	do
    	{	
    	//  ask for and get numeric value 
    	printf("Please enter numeric value between 1 and 10:  \n");
    	scanf("%lf",  &value );
    
    	    if (value< 1 ||  value>10) // check to see if value less than 1 OR  greater than 10
    	        {
    	        exitvalue=0;
    	        printf("Invalid entry please try again\n"); // value is outside of acceptance range - loop again
    	        }
    	   else
    	       {
    	       exitvalue=1;  //value is in acceptance range - exit while loop
    	       }
    
    	}
     	while (exitvalue==1);  //exit when exitvalue is equal to 1
    	
    	printf("condition met  \n");
    					
    return (0);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    81
    So you want to enter 22 and in the last printf to have "an anwer that 22 is not between 1 and 10"??
    output:

    vasileios@vasileios-laptop:~/desktop$ ./a.out
    Please enter numeric value between 1 and 10:
    22
    Invalid entry please try again
    condition met

    what exactly do you want to have??
    Last edited by nullifyed; 05-16-2010 at 12:59 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have your exit values backwards. You should be staying in the loop as long as it's not right, not exiting.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    The values I am setting right now are to test and make sure it works.
    if I enter a 5 it prompts me to enter another number. If I enter a number above 10
    it tells me "invalid entry please try again" then tells me "condition is met" meaning it exited the while loop.

    I am confused as to the exit values since I am setting them.

    I want a value to be vaild as designated by my if statement before exiting the loop
    Last edited by jm0825; 05-16-2010 at 01:12 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    do
    {	
        if (value< 1 ||  value>10) // check to see if value less than 1 OR  greater than 10
        {
            exitvalue=0;
        }
        else
        {
            exitvalue=1;  //value is in acceptance range - exit while loop
        }
    } while (exitvalue==1);  //exit when exitvalue is equal to 1
    Your comments do not match what you say they're doing.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    "Your comments do not match what you say they're doing."

    Please enlighten me....

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What part of "while this is one, keep looping" is confusing? You set it to 0, which makes it NOT ONE, which makes your loop stop.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Sorry, Brain dead.... Changed the ==1 to 0 it works fine.
    Thanks

    Jack

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  2. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  3. validate numerical entry
    By taurus in forum C Programming
    Replies: 27
    Last Post: 10-08-2007, 12:45 AM
  4. Database entry - avoiding duplicate records
    By ChadJohnson in forum Tech Board
    Replies: 2
    Last Post: 02-04-2006, 12:43 AM
  5. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 06:35 PM

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