Printing command line arguments in C++
Is this actually a good alternative to the second version that uses indexing? And if so, how could it be improved?


I'm not sure what you're asking here. Obviously the first version is fine; obviously the second version is pretty silly (because it does the same thing as the first version but in a more confusing and verbose way).

As for "using indexing", doesn't the first version also use indexing? What do you think argv[i] is, if not "indexing"? It sure seems like you've got the hang of "indexing" well enough. :)



Your second program is also silly in that it uses a while loop instead of a for loop, even though you have all three pieces of a for-loop (initialization, condition, and increment) in close proximity. Why didn't you use a for loop? Just for practice with different kinds of loops? In that case, I'd dock you points for not using a do-while loop! ;)



To nitpick as long as I'm here:


Everyone will tell you (so you might as well start listening now) that using namespace std is a bad habit. Write std::cout and std::endl explicitly.
You have a stray ; on line 1 — a transcription error, since the compiler wouldn't accept it if you actually tried to compile that.
Your second program uses saner loop index names: i and j. Naming a loop control variable number is more likely to confuse the reader than help them.
Explicit return 0 from main is unnecessary in C++ (and also in C, as of 18 years ago). Omitting it from simple programs like this can help the reader focus on the important stuff instead of the boilerplate.


Putting it all together:

#include &lt;iostream&gt;

int main(int argc, char **argv)
{
    for (int i = 1; i &lt; argc; ++i) {
        std::cout &lt;&lt; argv[i] &lt;&lt; std::endl;
    }
}