-1

I'm just started with a course for learning C, and am bumping in a problem with command line arguments. The assignment is this (there is more, but this is the part about the command line argument at the start):
- Your program must accept a single command-line argument, a non-negative integer.
- If your program is executed without any command-line arguments or with more than one command-line argument, your program should print an error message of your choice and return 1.
- You can assume that, if a user does provide a command-line argument, it will be a non-negative integer (e.g., 1). No need to check that it’s indeed numeric.

So I came up with this code:

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


    int main(int key, string plain[]) {

        if (key < 0 || plain[key] > 1)
        {
            printf("error\n");
            return 1;
        }
        else

etc...code continues.

Now I've tried several things, but I'm running into a wall.The compiler doesn't want to accept the if-condition I came up with, saying there is an error with comparison between pointer and integer which refers to the bold condition on the list of the assignment. So I understood that the argv part of the command line argument is the array of strings that the user put in. So my thought was to tell the compiler that when the user gives more than one string it should give an error message, so I wrote "plain[key] > 1)". Or is my understanding of command line arguments completely off here? Thanks.

dn3000
  • 1
  • 2
  • 2
    `plain[key]` would be a `string` not an `int` – Daniel Puiu Feb 01 '17 at 13:42
  • `plain[key]` is probably out of bounds. You should check what parameters are passed to `main`. – Gerhardh Feb 01 '17 at 13:52
  • C does not have a `string` type. What is that? The signature of `main` shall be `int main(int, int *[])` or the last aprameter can be `int **` which is identical. – too honest for this site Feb 01 '17 at 14:16
  • @Olaf, you probably wanted to write `char *[]` or `char**` – Gerhardh Feb 01 '17 at 15:05
  • The course I'm doing made string work in C, I suppose to make C a bit easier to start to work with. They made their own library that includes string. But anyways, I got that to work now, and now I bump into my second problem.. I know as per the assignment that the second char in argv (so that is argv[1]) is going to be an int. I need that int later in the code. So how do I turn argv[1] into an integer that I can use further down in my code? If I simply declare a new int and say: int key = argv[1]; the compiler doesn't accept it.. Any ideas how to fix this? – dn3000 Feb 01 '17 at 15:07
  • @Gerhardh: Of course! Thanks for the correction. Stupid flaw of me. – too honest for this site Feb 01 '17 at 15:11
  • @dn3000 We are not a tutoring site. You should read a C book or your course documentation or ask your teacher. And you cannot add new types to the C language. That cs50 library `typedef char *string` which is bad practice, as one should never `typedef` a data pointer. Youi cannot change this, but it sheds a bad light at that course. – too honest for this site Feb 01 '17 at 15:14

2 Answers2

0

You misunderstood the purpose of the arguments to main. The first int argument (usually named argc) is the number of items in the array argument.

And the array argument (usually called argv) contains all the arguments to your program (including the executable name) as text.

So if your executable is called foo, and you invoked it as foo 1 a bar, the arguments to main will be as follows:

int    argc == 4
char **argv => {"foo", "1", "a", "bar"}

So if your program must accept a single argument, it must hold that argc == 2 and argv[1] is the argument, that you must convert to a number from a string.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
0

plain[key] access the key element of plain array of string pointers (argv).

The size of that array is expressed by key (argc).

So what you want is

if (key > 1)
{
   //..
}

Moreover plain last element is key-1, 'cause is 0 based index.

LPs
  • 16,045
  • 8
  • 30
  • 61