vendredi 6 novembre 2015

Else If Statement not working C++

What's the Problem?

Running an if statement with 3 else if statements and an else statement at the end doesn't work.

What do you mean "It doesn't work"?

int handleArgs(int argc, char *argv[]) {    int x=0;
if (argc <= 1) {return x;}
x = 1;

for (int i=1; i < argc; i++) {
string argument = argv[i];                                  // Convert argument to string

if (argument=="-v" || argument=="--version") {              //*** If user asks for version info ***\\
    cout << "\nYou are running \n  Dev0.0.0" << endl;
} else if (argument == "-a " || argument == "--admin") {    //*** If user tries to open console ***\\
    cout << "\nSorry, console feature currently unavailable" << endl;
} else if (argument=="-h" || argument=="--help") {          //*** If user asks for help ***\\
    cout << "\nUsage: " << argv[0] << " -[switch] argument(s)\n";
} else {cout << "   Is you dumb?\nIncorrect syntax/argument(s)\n    '" <<      argv[0] << " --help' for help." << endl;}
}
return x;}

Specifically, the if(condition) never executes even if the condition is true.

The 1st else if(condition) never executes either.

The 2nd else if statement executes if true.

The else executes when else if is true AND

  1. The if statement is true.
  2. The 1st else if statement is true.
  3. None of the statements are true.
  4. Not if the 2nd else if statement is true.

Summary

Basically, it's like the if and the first else if don't exist.

Aucun commentaire:

Enregistrer un commentaire