vendredi 24 juillet 2015

Check if there are more than 1 argument

I have a simple program that encrypts message using Caesar Cipher.

The program works well, but I am unable to check whether the user is entering more than 1 argument. If he is, I need to break and prompt the user to enter the correct number of arguments which is 1.

If you enter more than 1 argument in terminal, say ./caesar 13 56 hello it will still work but it shouldn't

int main(int argc, string argv[])
{
    int indexKey = 0;

    string message; 

    if (argc < 2 || atoi(argv[0]) < 0 || atoi(argv[1]) < 0) {   
        printf("Please enter a non-negative integer as an argument.\n"); 
        return 1; 
    } else {   
        indexKey = atoi(argv[1]);
    }

Aucun commentaire:

Enregistrer un commentaire