0
int main( int argc, string argv[])
{
    if(argc ==2)
    {

        for(int i = 0, n = strlen(argv[i]); i < n; i++)
        {

            if(isdigit(argv[1][i]))
            {
                 int key = atoi(argv[1]);
        printf("success, %s\n", argv[1]);
    }
            else
                printf("no\n");
            return 1;
    } 
           }
    else 
        printf("noo\n");
}

Whenever i type the command line argument like 20x, its accepts it too as a digit and doesnt terminates the program. What am i doing wrong here?

0

From your code, I assume the input of 20x would make the programme print two "success" and one "no" message.

Maybe you shouldn't act on the first digit, but only on the first non-digit, print usage instructions and exit.

If you return on a non-digit, and eventually reach code after the for loop, that means that all characters were digits and you can safely convert the parameter to an integer. After, not within the loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .