command line arguments

This is a discussion on command line arguments within the C++ Programming forums, part of the General Programming Boards category; I am writing a simple program using command line arguments (int main(int argc, char* argv[]). It must search through a ...

sentence[j] is a character while argv[1] is a character string. You can not compare a character to a character string. If you want the individual character in argv[1] you would use argv[1][j]. But you also need to insure that j is never larger than strlen(argv[1]).

Also this line should be causing problems:

Code:

cin >> sentence[counter];

Your sentence variable is a std::string but your input says it should be an array of std::string.

Your sentence variable is a std::string but your input says it should be an array of std::string.

I disagree: the expression actually resolves to a char type. You're allowed to fill in the elements of a string just like you are allowed to fill in the elements of an array directly. The only issue is that, if sentence is really a sentence, then the fact that cin>> ignores whitespace is a potential problem.

sentence[j] is a character while argv[1] is a character string. You can not compare a character to a character string. If you want the individual character in argv[1] you would use argv[1][j]. But you also need to insure that j is never larger than strlen(argv[1]).

Since argv[1] is expected to be a single character, he only needs to dereference argv[1]. Then the code should be:

Thank you all :)
I didn't need to output the string with whitespace, that was just for me to check. I also re-read a chapter on pointers and such and figured it out. Sorry!
Ushakal is correct, by dereferencing I was able to compare to each character of the string.

Thank you all
I didn't need to output the string with whitespace, that was just for me to check. I also re-read a chapter on pointers and such and figured it out. Sorry!
Ushakal is correct, by dereferencing I was able to compare to each character of the string.

Do note that you should first check argc, which you weren't doing in the code you posted in #9, to see that the user has actually written any arguments or else *argv[1] might dereference an invalid pointer.