Like Tree1Likes
  • 1 Post By AndiPersti

Using argv

This is a discussion on Using argv within the C Programming forums, part of the General Programming Boards category; Hi there, I'm trying to make a program that takes a non-negative integer input from the terminal and then outputs ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Using argv

    Hi there, I'm trying to make a program that takes a non-negative integer input from the terminal and then outputs all integers starting from 0 up to the input number.

    I've ran into some problems however. Firstly, I was taught to use the atoi function to read what the number input for argv[1] is, but I've realized that if the user inputs a number, it'll return the correct value, however if the user inputs a non-numeric value, it returns 0, and this is stuffing me up.

    What I have so far is something like this:

    Code:
    if ((argc != 2) || (argv[1] < '0')) {
        printf("error")
    } else {
        printf("0,1,2...");
    }
    The problem is that
    Code:
    argv[1] < '0'
    comparison. I'm unsure as to how I can check to see that argv[1] is a number or not, because I can't use atoi since it'll return 0, and I can't differentiate this 0 from the 0 I would get if the number input was 0.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    argv[1] is a string, not a standalone char.
    So, you'd need to loop around it and call isdigit on each of the characters in it, to check if all of them are numbers.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Hmm ok, I'll research more on strings.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    There is also strtol.
    It has three arguments: the string to test, a pointer to pointer to char and the base of the number.

    After the conversion, the second argument will point to the first character which couldn't be interpreted as a valid digit. If no conversion was done (no valid number) it points to the beginning of the string.

    If you want to accept inputs like "100foo" to be interpreted as "100" you would test if the second argument points to the beginning:
    Code:
     
    int nr;
    char *end_ptr;
    
    nr = (int) strtol(argv[1], &end_ptr, 10);
    if (end_ptr == argv[1])
       printf("Not a valid number!\n");
    If you want to accept only valid numbers (all characters in the string must be consumed) you would test if the second argument points to the end of the string ('\0'):
    Code:
     
    int nr;
    char *end_ptr;
    
    nr = (int) strtol(argv[1], &end_ptr, 10);
    if (*end_ptr != '\0')
       printf("Not a valid number!\n");
    HTH, Andreas
    laserlight likes this.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    I had to do a little research on strtol()... man strtol has a nice example.

    How about something like this? Works like atoi(), but catches the '0'.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (int argc, char *argv[])
    {
        if (argc != 2) printf ("Error:\n");
        else
        {
            int x = atoi (argv[1]);
            if (x == 0 && argv[1][0] != '0') printf ("Error:\n");
            else printf ("%d\n", x);
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if argv
    By Muscovy in forum C++ Programming
    Replies: 2
    Last Post: 07-16-2009, 12:08 AM
  2. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 01:16 AM
  3. Question on *argv vs. *argv[]
    By movl0x1 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 07:03 PM
  4. argv
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 06:41 PM
  5. argv
    By Brian in forum C Programming
    Replies: 1
    Last Post: 01-26-2002, 05:55 PM

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