Thread: [C++] Command Line Arguments help

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    9

    [C++] Command Line Arguments help

    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]
    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;
    	}
    [View full source code]

    Thanks,
    -camelCase

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You can't use your own names for command line arguments. It's int argc (for argument count), char *argv[] (for argument vector). argv is an array of strings. The text the user types to execute the program from the command line is argv[0]. Every argument after that is argv[1], argv[2], and so on.
    Last edited by Babkockdood; 07-02-2010 at 07:29 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think there is much harm in using one's own names.
    Anyway, the problem is, once again, the C backwards compatibility. You cannot compare string literals that way.
    The best and easiest way to solve it is to wrap the command arguments inside std::strings.
    Eg:
    Code:
    std::string Args[] = { std::string(switches[0]), std::string(switches[1]), ... };
    Then you can compare them the way you do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well if all he needs to do is compare them, then std::strcmp works as well. The cost is one more header, #include <cstring>. I think the effort to make them std::string arrays is only worth it if you need to edit the arguments themselves.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    9
    Quote Originally Posted by Elysia View Post
    I don't think there is much harm in using one's own names.
    Anyway, the problem is, once again, the C backwards compatibility. You cannot compare string literals that way.
    The best and easiest way to solve it is to wrap the command arguments inside std::strings.
    Eg:
    Code:
    std::string Args[] = { std::string(switches[0]), std::string(switches[1]), ... };
    Then you can compare them the way you do.
    I kind of understand what you're implementing but I don't fully understand. I've created a variable called 'switches' with type std::string. I compare statements to this variable though it does the same thing.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm actually losing track of what you mean.

    From what I can see, switches is created as a parameter to main; its type being

    char *switches[]

    C string arrays and std::string arrays are not the same.

    I stand by my earlier advice. If all you are doing is comparing program arguments, then please use strcmp from the cstring library to do that. Otherwise, you really do have to create a string array to do other things, and it will NOT be called switches.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    9
    Quote Originally Posted by whiteflags View Post
    I'm actually losing track of what you mean.

    From what I can see, switches is created as a parameter to main; its type being

    char *switches[]

    C string arrays and std::string arrays are not the same.

    I stand by my earlier advice. If all you are doing is comparing program arguments, then please use strcmp from the cstring library to do that. Otherwise, you really do have to create a string array to do other things, and it will NOT be called switches.
    That would be a good idea, however the argument is of type CHAR and strings are of type STRING and are not compatible to be compared to one another.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Looking at it like the compiler, string literals are not std::strings, they are an array type char[whatever_size], a char array. A C-string is defined as a sequence of characters followed by an immediate terminating zero. Even if you never see the zero, you can store a string literal in a char array, so they are in all important ways equivalent.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    I'm actually losing track of what you mean.

    From what I can see, switches is created as a parameter to main; its type being

    char *switches[]

    C string arrays and std::string arrays are not the same.

    I stand by my earlier advice. If all you are doing is comparing program arguments, then please use strcmp from the cstring library to do that. Otherwise, you really do have to create a string array to do other things, and it will NOT be called switches.
    Introducing strcmp to newbies? No, thank you. Let them forget C-style string and C-style comparison every existed.
    I stand by my earlier advice to build C++ strings from them.
    That's my opinion anyway. No need to teach them about C-style comparison. They're probably not going to need it in any near future.

    Quote Originally Posted by camelCase View Post
    That would be a good idea, however the argument is of type CHAR and strings are of type STRING and are not compatible to be compared to one another.
    Yes, they are. You can compare a C-style string to a std::string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Maybe if argv and string literals were all std::string things it would be possible to completely forget what a C-string is.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If only we were that lucky. Not going to happen in any near future, I believe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM