Jump to content
  • Advertisement
Sign in to follow this  
TraderJack

No output

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!