5

I think my program handles lack of argv[1] correctly, yet it fails check50's test. here my code:

if ( argc != 2 )    
    printf ("usage : ./caesar + key \n");
    return 1;                        

7

the issue is that you started your code

int main( int argc, string argv[])

{ int k = atoi(argv[1];
. . . }

/// the mistakes is in the above assignment, if the there is no argv[1] so the OS will report error and terminates the program

so you have not to do k assignment to argv[1] unless you make sure there is

argv[1]

thanks

Mohamed Abdeltawab

| improve this answer | |
  • This solved the issue I was having! thank you! – Joey Robertson Feb 14 '17 at 16:51
  • Thanks! That helped me 🙌 – gonzaloorsi Apr 8 '17 at 19:29
6

The number of command-line arguments cannot be negative. In fact, it cannot even be < 1. See this answer for more details!

If the number of command-line arguments is not 1 (i.e., argc != 2), your program should "print" an error message and "return" an error code of 1.

| improve this answer | |
  • I assume that it is sufficient to test for argc != 2. But when I implemented this, and had the program indeed issue the exit(1) when argc != 2 was True, I got the following. :( handles lack of argv[1] \ expected output, not an exit code of 1 I'm confused, as I believe I'm doing what is expected of me by the program requirements - that is, issuing the exit(1). What am I misinterpreting? Thanks. – rl777 Oct 2 '14 at 1:54
  • I've also used the following condition in doing the check - if ((argc<2) || (argc>2)) -with the same result. – rl777 Oct 2 '14 at 2:10
  • @rl777 please read the answer carefully! "your program should "print" an error message and "return" an error code of 1" – Kareem Nov 28 '14 at 20:01
  • Where does it say in the pset specification to print something? Did yell imply so? – maq Dec 11 '14 at 18:08
  • Yes, @maq...... – Kareem Dec 11 '14 at 18:10
5

You need to add your curly brackets (aka braces). Instead of

if ( argc != 2 )    
printf ("usage : ./caesar + key \n");
return 1;

You should have:

if ( argc != 2 )    
{
    printf ("usage : ./caesar + key \n");
    return 1;
}
| improve this answer | |
  • It's better to use, printf ("usage : ./caesar + k \n"); Because the instruction code also gives such examples. – Amitrajit Bose Jan 11 '18 at 19:33

You must log in to answer this question.

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