Thread: Testing User Input

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Testing User Input

    I'm writing a simple c program to do a calculation using a couple of hex numbers. I've got it working but as usual, which seems to always be the case, I'm struggling with code to verify the user input is good. I have 2 issues:

    1. I need to make sure the user enters a valid hex value of just 1 digit from 0 to E.

    2. In the current setup the testing is working, I've just used an int and a while loop to test if it's > 0x0 and < 0xe, but if the user inputs 2 digits my error message prints twice, if they enter 3 my error message will print 3 times and so on. I don't know how to stop this.

    I can post code but i've started hacking it before asking.

    Do I need to except the input as a CHAR or STRING and test it then use ATOI to convert before the calculation? If so how do I test it?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your code... none of us are mind readers (that I know of).
    Lets see what we can do...

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    Do I need to except the input as a CHAR or STRING and test it then use ATOI to convert before the calculation? If so how do I test it?
    It doesn't matter. Either approach will work. Without your code it's hard to tell why your error messages are printing out multiple times.

    Can I ask why F isn't a valid hex digit for you? Just making sure it's not an accidental omission.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    76
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int start_head, calc_1, calc_2, start_cyl, end_cyl, end_head;
    	printf ("Please enter the starting cylinder: ");
    	scanf ("%x", &start_cyl);
    	getchar();
    	printf ("Please enter the starting head: ");
    	scanf ("%x", &start_head);
    	getchar();
    	
    	while (start_head < 0x0 || start_head > 0xe)    
    	{
    		printf ("Invalid input, please re-enter the starting head: ");
    		scanf ("%x", &start_head);
    		getchar ();	
    	}
    	printf ("Please enter the ending cylinder: ");
    	scanf ("%x", &end_cyl);
    	getchar();
    	printf ("Please enter the ending head: ");
    	scanf ("%x", &end_head);
    	getchar();
    	while (end_head < 0x0 || end_head > 0xe)  
    	{
    		printf ("Invalid input, please re-enter the ending head: ");
    		scanf ("%x", &end_head);
    		getchar();
    	}
    	
    	calc_1 = (start_cyl * 0x80 * 0xf) + (start_head * 0x80);
    	calc_2 = (end_cyl * 0x80 * 0xf) + (end_head * 0x80);
    	printf ("The starting block is: %d\n", calc_1);
    	printf ("The max extent is: %d\n", calc_2);
    	getchar();
    	return 0;
        
    }
    OK, here is the last working version. I'm very new to this so go easy lol. Eventually I'd like to test the start_cyl to make sure it's 5 digits or less and all 5 digits are valid hex numbers (0-f). For now I'd like to start with testing start_head to verify it is only 1 digit and between 0 and e. Now the test for the start_head and end_head work, but if you issue say rr it prints the error twice and so on.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by itsme86 View Post
    Code:
    Do I need to except the input as a CHAR or STRING and test it then use ATOI to convert before the calculation? If so how do I test it?
    It doesn't matter. Either approach will work. Without your code it's hard to tell why your error messages are printing out multiple times.

    Can I ask why F isn't a valid hex digit for you? Just making sure it's not an accidental omission.
    It's a special case and not needed for this.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well scanf returns a value that equals the number of successful conversions. So, for example if you call

    Code:
    if (scanf("%x", &start_head) == 1 && start_head > 0x0 && start_head < 0xf)
    {
       printf("start head = %x\n", start_head);
       
    }
    else 
    {
       printf("not a valid start_head\n");
    }
    One of two things will be printed. You might want to break up the verification though, so it is possible to clean up after scanf if conversion fails.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Also, you probably want:

    Code:
    start_head >= 0x0 && start_head <= 0xf
    Unless you intend to restrict the range to 0x1 - 0xe

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Subsonics View Post
    Also, you probably want:

    Code:
    start_head >= 0x0 && start_head <= 0xf
    Unless you intend to restrict the range to 0x1 - 0xe
    It's actually supposed to be restricted to 0x0 - 0xe, as mentioned by the OP:
    1. I need to make sure the user enters a valid hex value of just 1 digit from 0 to E.
    So the condition should actually be:
    Code:
    start_head >= 0x0 && start_head <= 0xe
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    76
    OK been playing with this and am uncertain as to how to implement it. If I use an if/else the program terminates if the condition is not met. This is no good. I want a loop so we prompt the user again. I would like to learn the best way to handle user input and take care of the buffer to avoid pitfalls.

    I'm very new to this and open to suggestions as many posters here seem much more well versed than me. Is the if/else the best way or is there a better way to receive the input and test it?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You want a do-while loop:
    Code:
    do {
        prompt user
        get input
        if (input is valid)
            do stuff
    } while (input is not valid)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. displaying text below user input (cin)
    By MilkyJoe in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2009, 03:00 PM
  2. Testing user input question?
    By Hoser83 in forum C Programming
    Replies: 18
    Last Post: 02-15-2006, 01:18 PM
  3. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM
  5. Beginner question- user input termination
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 02:48 PM