Help with a function!

This is a discussion on Help with a function! within the C Programming forums, part of the General Programming Boards category; how do you make it so that if the user inputs anything but integers the program will give an error ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Help with a function!

    how do you make it so that if the user inputs anything but integers the program will give an error and prompt the user to enter again?
    ex. 123456## or ?? should give an error and prompt the user to enter again
    help?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You use an input function to get the data, then you parse the data yourself, or use built-in functions to parse the data, and check their output, and you put all of that in a loop.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    One way would be to take your input as a char string and check each character against it's ascii value:
    Code:
    #include <string.h>
    #include <stdio.h>
    
    
    int main() {
    	int i;
    	char input[1024];
    	scanf("%s",input);
    	for (i=0;i<strlen(input);i++) {
    		if (input[i]<48 || input[i]>57) {
    			puts("Not a number!");
    			return -1;
    		}
    	}
    	printf("Number was %d\n",atoi(input));
    	return 0;
    }
    This doesn't accept decimals or + or - in "the number" but you should be able to modify it to do so.

    Also you could look at the documentation for atoi and strtol for another strategy.
    Last edited by MK27; 11-26-2008 at 04:19 PM.
    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

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    i appreciate the replies!
    is there anyway i can use it with an int instead of a char?
    the digit has to be a 6 digit number, so this is what i have so far
    Code:
    while(int < 100000 || int > 999999) {
    			printf("Invalid! Enter again: ");
    			scanf("&#37;d", &int);
    		}
    Of course, this is so only integers that are 6 digits are allowed.. but im having trouble with anything besides integers. How would I implement what you have above into this? O.O

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    Code:
    		if (input[i]<'0' || input[i]>'9')
    Why not use the character constants, instead of numeric values. Not only does it make it easier to read, but also allows for the code to compile on platforms that have other character sets than ASCII.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by phufool View Post
    i appreciate the replies!
    is there anyway i can use it with an int instead of a char?
    the digit has to be a 6 digit number, so this is what i have so far
    Code:
    while(int < 100000 || int > 999999) {
    			printf("Invalid! Enter again: ");
    			scanf("%d", &int);
    		}
    Of course, this is so only integers that are 6 digits are allowed.. but im having trouble with anything besides integers. How would I implement what you have above into this? O.O
    If you are using scanf, then you need to check the return value of scanf to see if the number of items accepted is 1. If not, you should empty the input buffer - see the FAQ entry on that subject.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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