Thread: Problem preventing unwanted inputs

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Problem preventing unwanted inputs

    Hey everyone,

    I'm a C programming newbie (and recently registered with this website as well) and I'm having a bit of a coding problem. I'm designing a program that takes command line number inputs in the form "./remove_duplicates 5 6 6 5" and removes the duplicate numbers (the output here would be "5 6").

    My only problem is when I try to restrict the input possibilities---I only want spaces and numbers allowed. Any letters for example, be it standalone or tagged on the end of a number, should get an invalid input message and return 1. That's the part I'm struggling with. Below is my attempt, I'd greatly appreciate any help.

    Thanks,
    Noah

    Code:
    // function: to receive a string of command line numbers and return the same string of numbers with duplicates removed
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main(int argc, char *arg[]) {
      int i, j, printmeplz, l;
      char number[l];
      printf("\n");
    
    
      for (i=1; i<argc; i++) {
        l=strlen(arg[i]);
        number[l]=*arg[i];
        for (j=1; j<l; j++) {
          if (isdigit(number[j])==0) {
        printf("INVLAID INPUT\n\n");
        return 1;
          }
        }
      }
    
    
      if (argc < 2)
        {
          printf("\nThe program receives command-line inputs---numbers separated by a space---and returns the same numbers but with all the duplicates removed from the string.)\n\n");
        }
      else {
        for (i=1; i<argc; i++) {
          printmeplz=1;
          for (j=1; j<i; j++) {
        if (strcmp(arg[i],arg[j])==0) {
          printmeplz=0;
          j=i;
        }
        else {
          printmeplz=1;
        }
          }
          if (printmeplz==1) {
        printf("%d ", atoi(arg[i]));
          }
        }
        printf("\n\n");
      }
      return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your first problem is most likely on line 17 ... the contents of command line strings (argv) are ascii characters that do not convert directly to numbers... to get the integer value from an ascii character you have to offset the values, like this...
    Code:
    number = arg[i][0] - '0';
    By subtracting the ascii value for 0, you end up with the actual number, not it's ascii value. (Note the use of apostrophies, not quotations)

    Also number does not need to be an array in this case...

    Finally, you can simplify that entire section to this...
    Code:
      for (i=1; i<argc; i++) {
        if ( ! isdigit(arg[i][0])) 
          {
            printf("INVLAID INPUT\n\n");
            return 1;
          }
    Simple guildelines... after you get something to work, simplify it as much as you can and never make variables you don't actually need.
    Last edited by CommonTater; 11-16-2011 at 08:25 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Thanks for the reply,

    I tried your code and slightly modified mine but I'm still having the problem.

    The goal of lines 15-24 (this is where the problem is) is to look at each individual command line argument, then look at each bit of each argument and make sure that all the bits are numbers. Just wanted to clarify about the goal of that piece of code.

    Thanks,
    Noah

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you are going to be accepting more than single digit numbers, yes you need to assess the entire length of each string...
    Code:
    int i, j;   // loops
    int len;   // length
    
      for (i=1; i <argc; i++) 
       {
          len = strlen(arg[i]);  // get length
          for (j = 0; j < len; j++)
            if ( ! isdigit(arg[i][j])) 
             {
               printf("INVLAID INPUT\n\n");
               return 1;
             }
       }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Thanks very much CommonTater, that worked for me.

    I actually had that exact same code but instead of your line 8 I had

    Code:
    if (isdigit(arg[i][j])==0) {
    Not sure why that doesn't work but either way, thank you very much.

    Noah

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually your version should have worked as well... odd.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The original code was broken long before you got the the loops.

    int i, j, printmeplz, l;
    char number[l];


    l is uninitialised, so how big do you think your array is going to be?

    As for validating strings as a whole number, try man page strtol section 3
    as in
    Code:
    char *endptr;
    long int result = strtol( argv[i], &endptr, 10 );
    if ( errno != 0 && endptr != argv[i] && *endptr != '\0' ) {
      // it's all good
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with limiting inputs of char functions
    By hobolux in forum C Programming
    Replies: 3
    Last Post: 10-01-2010, 01:13 PM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. Problem in reading inputs
    By Bargi in forum C Programming
    Replies: 15
    Last Post: 06-18-2008, 09:28 AM
  4. Problem with taking inputs/calculating slope
    By toadkiwi in forum C Programming
    Replies: 12
    Last Post: 02-22-2008, 12:05 AM
  5. unwanted blankness
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 04:48 PM

Tags for this Thread