Hello CProgrammers,
I have been struggling all afternoon trying to bring a resolution to this issue.
Application: Calculator
Issue: Command Line Arguments
When the user gets to the command line and enters the file name and switch the program doesn't read the switch properly. The source code is below. When it reads the switch, even if it is correct, it doesn't perform the correct operation. I believe it isn't reading the switch right.
Basically when the program reads the switch, it doesn't do the code i've asked it to. Example: If (a = "-m") { do this } else { do that } and even if it should be the first if block, it does the else.
The area i'm having trouble with is colored in red.
[View full source code]
[View full source code]Code:int main(int sizeOfArrayV, char *switches[]) { // This application will accept switches for (int x = 0; x < sizeOfArrayV; x++) { cout << "Switch #" << x << " =: " << switches[x] << endl; } cout << endl; if(switches[1]) { // If switches 1 is defined: if(switches[1] == "-m" || switches[1] == "-M") { // error handling cout << "Multiple variables... [ENABLED!]" << endl; } else if(switches[1] == "-x" || switches[1] == "-X") { // The user did not enter the switch: cout << "Multiple variables... [DISABLED!]" << endl; } else { // The user did not enter '-x' or '-m'. cout << "You must enter a proper value! Please enter '-x' to disable the switch!" << endl; } } else { // The user must enter -m or -x because if they don't the order will be messed up. // This will be called if the user simply clicks the '.exe' instead of using the CL. cout << "Switches... [DISABLED!]" << endl; }
Thanks,
-camelCase