Thread: Function to detect numbers, WIP.

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    93

    Function to detect numbers, WIP.

    I'm developing a function to determine if a user has entered a number or characters.

    Here's the code for that:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdint.h>
    
    int main() {
        int8_t ProgramCounter, SecondaryCounter, CharacterDetect;
        char Numbers[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        char UserInput[64];
        
        printf("Enter some numbers and characters.\n");
        fgets(UserInput, 64, stdin);
        
        for(ProgramCounter = 0; ProgramCounter < strlen(UserInput); ProgramCounter++)
            for(SecondaryCounter = 0; SecondaryCounter < 10; SecondaryCounter++)
                if(UserInput[ProgramCounter] == Numbers[SecondaryCounter])
                    printf("Number found at UserInput index: %d\n", ProgramCounter);
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm a bit confused as to how this is supposed to report results. At a minimum, wouldn't it be smarter if the function reported where the number started in the string instead of the end? Also, use isdigit().

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Also also, unless you have a serious reason to use something else, always prefer using int or unsigned instead of other types. There's absolutely no reason to be restricting your code to a maximum of 127, a limit that would easily produce bugs were you to change the size of the array.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    Right now, it just reports back where a number is located within the UserInput array.

    AFAIK, isdigit only works on int types. I'm taking input as chars.

    The way my code is right now, if I used isdigit, I couldn't use UserInput since it's type is *char.

    Am I understanding that correctly?

    What do you mean the function reporting where the number started instead of the end?

    My plan is to take a user's input, iterate through the input array, check if there's a single character that isn't a number, if there isn't, return 1, if it does find a character that isn't a number, return 0.

    If it returns 1, I know, without a doubt, that all the individual characters are numbers. I can safely use atoi without it returning 0.
    Last edited by ImageJPEG; 12-10-2017 at 01:37 AM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ImageJPEG View Post
    Right now, it just reports back where a number is located within the UserInput array.

    AFAIK, isdigit only works on int types. I'm taking input as chars.

    The way my code is right now, if I used isdigit, I couldn't use UserInput since it's type is *char.

    Am I understanding that correctly?
    No, you aren't understanding correctly. The point of isdigit() is to communicate if text is also a digit. isdigit(userinput[x]) is true if the underlying value is one of '0' through '9'. The reason that isdigit() takes an int is to handle EOF correctly.

    I may have told you this exactly, before.

    What do you mean the function reporting where the number started instead of the end?
    Well, based on the code in the OP, the printf() will execute when it finds digits. It just seemed like an odd way to report results to me. It doesn't help that initially you presented the function with zero context. I understood that you were done with this.

    My plan is to take a user's input, iterate through the input array, check if there's a single character that isn't a number, if there isn't, return 1, if it does find a character that isn't a number, return 0.
    This makes a lot more sense.

    But really if you can do this you may as well convert the input to an integer too.

    If it returns 1, I know, without a doubt, that all the individual characters are numbers. I can safely use atoi without it returning 0.[/QUOTE]

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    When you say convert the input to an integer, do you mean instead of "char UserInput[64];" I use "int UserInput[64];"?

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    And sorry, I should have provided more context. I am printing the index locations of found numbers to help me figure out if it's working or not.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ImageJPEG View Post
    When you say convert the input to an integer, do you mean instead of "char UserInput[64];" I use "int UserInput[64];"?
    No I mean like what atoi() or strtol() would do.

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I've been doing a bit more work with it. It seems to be fully working.

    And for full context, this will force a user to enter a number. If the user enters a series of numbers but adds a non-integer character, the program will ask the user to re-enter their input.

    I'm also doing this to help me understand arrays and how to iterate them. I plan on making a Hangman game in the near future.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdint.h>
    
    
    int AlphaDetector(char *Input);
    
    
    int main() {
        char UserInput[64];
        
        do {
            printf("Give me some input:\n");
            fgets(UserInput, 64, stdin);
        }
        while(AlphaDetector(UserInput));
            
        return 0;
    }
    
    
    int AlphaDetector(char *Input) {
        int8_t ArrayCounter, NumberCounter, AlphaDetect, NumberDetect;
        char Numbers[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        
        AlphaDetect = 0;
        NumberDetect = 0;
        
        for(ArrayCounter = 0; ArrayCounter < strlen(Input); ArrayCounter++)
            for(NumberCounter = 0; NumberCounter < 10; NumberCounter++) {
                if(Input[ArrayCounter] == Numbers[NumberCounter]) {
                    NumberDetect++;
                    break;
                }
                if(NumberDetect != ArrayCounter)
                    AlphaDetect = 1;
            }
        return AlphaDetect;
    }
    Last edited by ImageJPEG; 12-10-2017 at 05:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function: add all even numbers from n to 2
    By Cancino Mel in forum C Programming
    Replies: 11
    Last Post: 05-05-2014, 01:35 AM
  2. Function to detect subsequence string
    By drawar in forum C Programming
    Replies: 11
    Last Post: 05-02-2013, 08:11 PM
  3. Replies: 10
    Last Post: 02-08-2012, 03:17 PM
  4. Hash function for two numbers
    By Sfel in forum C Programming
    Replies: 10
    Last Post: 03-29-2008, 04:55 PM
  5. Adding double numbers in a function
    By ybala in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 09:36 AM

Tags for this Thread