samedi 26 janvier 2019

Command Line Arguments not Being Read Properly

The problem im having is whenever i enter only name of the program, the if statement where the condition is argc == 1 doesn't work and nothing is printed in my command prompt but every other if statement works when i test it.I am really not sure what i have done wrong, thanks for your help.

Also i know the towers function is working correctly because for any other test run i do it prints the correct output, its only when no command line arguments other than the program name is written it does not print anything.

int main(int argc, char **argv) {

int n;
int from;
int dest;

if ((atoi(argv[2]) > 3) || (atoi(argv[3]) > 3)) {
        fprintf(stderr, "Cant have more than 3 towers.");
        exit(1);
} else
if ((atoi(argv[1]) < 0)) {
    fprintf(stderr, "Cant have negative number of disks.");
    exit(1);
} else
if (argc == 1){
    fprintf(stderr, "No command line argument was given./n");
    n = 3;
    from = 1;
    dest = 2;
} else
if (argc == 2){
    fprintf(stderr, "One command line argument was given\n");
    n = atoi(argv[1]);
    from = 1;
    dest = 2;
} else
if (argc == 4) {
    fprintf(stderr, "Three command line arguments were given\n");
    n = atoi(argv[1]);
    from = atoi(argv[2]);
    dest = atoi(argv[3]);
}

towers(n, from, dest);

exit(0);
}

Aucun commentaire:

Enregistrer un commentaire