-1

What is the proper way to receive a command line argument from the user and compare it to a single char? For example if the user typed "Y" for yes run some function and "N" for no would run another function.

My main error is "comparison between ptr and integer" & "too few arguments to function call, expected 3 have 1"

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

void calculate();
void verify();

int main (int argc, char *argv[]) {
   if(argc < 2){
        printf("Please enter a mode of operation.\n");
        return 0;
    }
    else if(argc > 2){
        printf("Too many arguments supplied. Please try again.\n");
        return 0;
    }
    else if(strncmp(argv[1] == 'c') == 0)
        calculate();

    else if(strncmp(argv[1] == 'v') == 0)
        verify();

return 0;
}

void calculate(){
}

void verify(){
}
6
  • 1
    strncmp(argv[1] == 'c') == 0 --> strcmp(argv[1], "c") == 0
    – 4386427
    Commented Mar 29, 2017 at 16:54
  • strncmp(argv[1] == 'c') --> strncmp(argv[1], "c",1) Commented Mar 29, 2017 at 16:55
  • 1
    BTW, which part of the error message was unclear? Commented Mar 29, 2017 at 16:55
  • You can convert it to strncmp(argv[1], "c") == 0 to compare the argument to the string c Commented Mar 29, 2017 at 16:56
  • if you don't care what the rest of the string is you could just do if(*argv[1]=='c')? Commented Mar 29, 2017 at 16:59

2 Answers 2

1

You're not calling strncmp() correctly. It should be:

strncmp(argv[1], "c", 1)

It takes 3 arguments: two strings and a limit. 'c' is a char, not a string, string literals are put in double quotes.

You can also write simply:

if (argv[1][0] == 'c')

This just checks the first character, so the user can type y or yes and they'll match y. If you want to match the whole argument and require it be just a single character, you should use strcmp() rather than strncmp():

if (strcmp(argv[1], "c") == 0)
4
  • 1
    @4386427 - that is true and arguably, that might be what OP intended.
    – alvits
    Commented Mar 29, 2017 at 16:59
  • @4386427 I thought he just wanted to check the first letter. So you can type y or yes
    – Barmar
    Commented Mar 29, 2017 at 17:02
  • @4386427 I've added more to the answer that explains both ways.
    – Barmar
    Commented Mar 29, 2017 at 17:04
  • @Barmar - Good answer - covers both the exact match and the "match first letter" case
    – 4386427
    Commented Mar 29, 2017 at 20:18
0

This

strncmp(argv[1] == 'c') == 0

just is incorrect syntax. For starters the function strncmp requires three arguments and it deals with strings while in the avove call of the function there is supplied only one argument of type int. because the wrong expression argv[1] == 'c' has type int and is equal to 0.

I can suggest the following solution

else if ( argc > 2 || strlen( argv[1] ) != 1 ){
    printf("Too many arguments supplied. Please try again.\n");
    return 0;
}
else if ( argv[1][0] == 'c' )
    calculateCRC();

else if ( argv[1][0] == 'v' )
    verifyCRC();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.