0

In my program I used a if statement to check if key = atoi(argv[1]) == 0, and if key is == 0, then my program returns a 1. However, it wasn't until I tested my program for corner cases that I realised (unrealisticly) what if the user enters a key of 0?

Now, because of the if statement, I am not sure how I can correct this so that a key of 0 can be entered and my program can still check for strings being inputted for the argument.


int main(int argc, string argv[]){

if (argc != 2){
    printf("ERROR! Please enter a positive number and try again.\n");
    return 1;
    }

int key = atoi(argv[1]);

if (key == 0){
    printf("ERROR! Please enter a positive number and try again.\n");
    return 1;
    }
1
  • The spec didn't say anything about the key being 0. It only said the key has to be a non-negative integer. So I'm pretty sure 0 is neither negative or positive. – Xihai Luo Jan 17 '15 at 0:29
1

I don't believe the assignment indicates that the key cannot be 0. I think it might mention that it would not scramble the message, but I believe your code is still supposed to handle a key of 0 (i.e., outputting a string identical to the input).

Also, sort of an aside, I believe generally when check50 checks to see if you handle an improper amount of arguments, it expects an output explaining the usage, like "Usage: ./caesar.c (what argv[1] should be) (what argv[2] should be) etc.." (without parentheses of course, and using square brackets around optional arguments).

0

Per the spec:

Incidentally, you can assume that the user will only type integers at the prompt. You don’t have to worry about them typing, say, foo, just to be difficult

You must log in to answer this question.

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