Sign in to follow this  
TraderJack

No output

Recommended Posts

TraderJack    530
Hey, Jack here. I made this program that takes the entry names in the command line and says hello to each one, and it compiles just fine, but when I run it, I get no output. Here's the code: #include <iostream> #include <cstring> using namespace std; int main (int argc, char *argv[]) { for (int rep = 1; rep > argc; rep++) { cout << "Hello " << argv[rep] << endl; } return 0; } Any suggestions? Thanks. -TJ

Share this post


Link to post
Share on other sites
Fruny    1658
You wrote as your loop termination condition rep > argc. It is initially false since argc is always at least one, for the program name, and thus the loop is never run.

You'll want to use rep < argc instead.

As a side note, get into the habit of writing ++rep instead of rep++ unless you do explicitely need the post-increment. For built-ins it doesn't matter, but for custom types (including iterators), it does, and may result in better performance.

Your program doesn't currently need the <cstring> header.

Share this post


Link to post
Share on other sites
Washu    7829
Quote:
Original post by Programmer16
Shouldn't rep start out as 0 instead of 1 (since indexing is 0 based)?


Index 0 is the program name.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Programmer16
Shouldn't rep start out as 0 instead of 1 (since indexing is 0 based)?


No, because argv[0] is the name of the executable, and not the first string passed to the program.

*cough* homework *cough* !!

;)

Share this post


Link to post
Share on other sites
Programmer16    2321
Geez, that's pretty sad. I've been using C++ for about 4 years now and I didn't know that (I've been working with Win32 and DirectX for most of the time though).

I homeschool (and my mom won't do any programming courses), so this couldn't possibly be homework :D ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this