Willi
Willi

Reputation: 361

How to check if command line parameters are integers

First of all, I am talking about old-fashioned ANSI-C (I mean the ANSI standard and no C99 or newer) compiled with gcc. I am only allowed to use the libraries that can be seen below in the code.

My problem is I have a program that is called in the following way on the Terminal:

program < integer_1 integer_2

While I have been able to figure out how to check for the number of arguments, I'm stuck on checking if those are integers. If the program is called like this:

program < 1 -13

it should run without complaining but if it is run like this:

program < s 7

it should throw out an error.

Whatever I have tried so far has been utter rubbish. The best thing I have managed so far has been an error message if the second number has been a character. None of my tries has been able to deal with more than one digit but I have figured out why that is.

The problem is that I haven't used command line / terminal arguments with any programming language i now (C++, Java). I would really appreciate it if someone could show me how check for correct input as frankly I am out of ideas.

Am I correct that if I want to deal with numbers bigger than 9, I have to iterate through argv starting from index 2 until I find a space?

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(int arc, char *argv[])
{
  if(arc != 3)
  {
    printf("Error: You have entered %d arguments, but two were expected!", arc - 1);
    return -1;
  }

  return 0;
}

Upvotes: 4

Views: 7834

Answers (4)

RoadRunner
RoadRunner

Reputation: 26315

You can try something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int check_cmd_args(char const *str[], int numargs);

int 
main(int argc, char const *argv[]) {

    if (argc < 2) {
        printf("Not enough command line arguments entered\n");
        exit(EXIT_FAILURE);
    }

    if (check_cmd_args(argv, argc)) {
        printf("All Command line arguments are integers\n");
    } else {
        printf("Error, non-integer command line argument entered\n");
    }

    return 0;
}

int
check_cmd_args(char const *str[], int numargs) {
    int n, i = 0;

    for (n = 1; n < numargs; n++) {
        if (str[n][0] == '-') {
            i = 1;
        }
        for (; str[n][i]; i++) {
            if (!isdigit(str[n][i])) {
                return 0;
            }
        }
    }
    return 1;
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

The easiest way out is to iterate over the argv[n]s and pass one by one to them to strtol() or similar. Then, check for the error and make the decision. To quote the man page, (emphasis mine)

long int strtol(const char *nptr, char **endptr, int base);

[...]

If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.

That said, program < integer_1 integer_2 is not exactly the way to pass the command-line arguments. If you want to pass the values arguments as command-line arguments, you shall lose the redirection operator and work with argc and argv[n]s directly..

Upvotes: 4

cup
cup

Reputation: 8237

Just a comment: not an answer

If you are going to use

program < arg1 arg2

You will not see arg1 or arg2 in the main parameters. arg1 is typically a filename or device which contain data which will be read by the program. I don't know if the program will even be able to access arg2. If you wish to pick up arg1 arg2 etc, lose the <

program arg1 arg2

Upvotes: 0

Sohil Omer
Sohil Omer

Reputation: 1181

Best way is to create a function for checking whether it is number or not.if the below function returns true then use atoi(argv[]) to convert them to integers to use it further.

bool isNumber(char number[])
{
    int i = 0;

    //checking for negative numbers
    if (number[0] == '-')
        i = 1;
    for (; number[i] != 0; i++)
    {
        //if (number[i] > '9' || number[i] < '0')
        if (!isdigit(number[i]))
            return false;
    }
    return true;
}

Upvotes: 0

Related Questions