Thread: Im getting a headache

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    Im getting a headache

    lately I have been asking questions a bit over my head so Im trying to break things down a little so I can understand it better.

    What Im trying to do is look at all integers and if any of them are not 0-9 then return an error.

    I was doing well with this earlier today but I have looked at so many different ways to code this that Im starting to confuse myself. I was curious if someone could please look at what I have and see where my mistake is?
    Thank you
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int valid(); 
    
    int main (void)
    {
    	int a;
    	char buff[BUFSIZ];
    	
    	printf("Enter number: ");
    	fgets(buff, BUFSIZ, stdin);
    	sscanf("%d", &a);
    
    	if( valid(buff, BUFSIZ, a))
    		printf("Output: %d\n", a);
    	else
    		printf("This is an error string\n");
    	
    	getch();
    }
    
    
    int valid(int array[], int count, int a)
    {	
    	int i;
    
    	for (i = 0; i < count; i++)
    	{
    		if (array[a] >= 0 && array[a] <= 9)
    			return 1;
    	}
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like so

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int valid(char array[], int count );
    
    int main (void)
    {
        int a;
        char buff[BUFSIZ];
        
        printf("Enter number: ");
        fgets(buff, BUFSIZ, stdin);
    
        if( valid(buff, strlen(buff)) ) {
            sscanf( buff, "%d", &a );
            printf("Output: %d\n", a);
        } else {
            printf("This is an error string\n");
        }
        return 0;
    }
    
    int valid(char array[], int count )
    {   
        int i;
    
        for (i = 0; i < count; i++)
        {
            if ( array[i] == '\n' ) continue;   // ignore newline
            if (array[i] < '0' || array[i] > '9' )
                return 0;   // outside range
        }
        return 1;
    }

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You could also use the macros defined in ctype.h, like isdigit().

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    cool but

    ok very nice, Im much closer.... but if a the input was something like : 54k6 it would accept 54 and not return an error?

    What did I miss? I thought this would go through each character and if anything was not {0,1,2,3,4,5,6,7,8,9} then it would return an error?

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Look at Salem's code. It works. The reason why your code doesn't catch it is because you only check one character and then return in your for loop. Also, I dunno why you used array[a], it should be array[i] like in Salem's code.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    huh

    well Im home now and I was using salems code at work, but I swear it wasnt working properly.. but now that Im trying it from home.... I see it does work. go figure!

    Well I guess that means tomorrow morning Im going to go back and check my connections. (ie.. make sure my head isnt connected to my ass!)

    and why was I using a? Cause Im a newbie and dont know ____

    =)

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libusb headache
    By cbee in forum C Programming
    Replies: 2
    Last Post: 12-02-2008, 08:27 PM
  2. Headache error message
    By Strait in forum C++ Programming
    Replies: 6
    Last Post: 01-31-2005, 03:48 PM
  3. My headache
    By Morgan in forum C Programming
    Replies: 7
    Last Post: 12-03-2002, 03:33 AM
  4. Pointers + Arrays = Headache
    By Estauns in forum C++ Programming
    Replies: 7
    Last Post: 11-22-2001, 06:53 PM
  5. Linked List headache
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-23-2001, 06:42 PM