0

Hey guys could someone advice me on the flaws in the following line of code. I'm visited by the following error message:

c:30:25: error: invalid operands to binary expression ('char *' and
      'string' (aka 'char *'))
           printf("s\n" %s);

int main(int argc, string argv[])

{      
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    string s = argv[1];  


    bool is_all_digit = true;

    for (int i=0, n = strlen(s); i < n ; i++)          
        {
            if(!isdigit(s[i]))
            {
                is_all_digit = false;
                printf("Usage: ./caesar key");
            }    
        }

        if(is_all_digit){
           printf("s\n" %s);


         }

    }
0

I assume that you're trying to print the string stored at s with this statement:

       printf("s\n" %s);

The error results from the incorrect usage of placeholders in the statement. You need to use a placeholder for each var to be used in the printf statement. The variables will be used in order as each placeholder is encountered. In this case, there's only one placeholder and one variable.

The correct form of the statement would look like this:

       printf("%s\n", s );

A placeholder starts with a % sign and followed by a descriptor that tells what kind of thing should be printed. It can also include formatting code, but that's getting more complicated. You should google "printf in c" and study how to use placeholders and formatting code.

Side note: If you're validating input and find something that justifies printing out a usage statement for the program, maybe the next line of code should be a return statement to actually exit the program immediately? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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