Public Group

# Compiler Error

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

## Recommended Posts

Hey! This bit of code is supposed to read the names in the command line and say hello to each of them in turn. This is done with a loop that repeats each string in the command line (argv) untill it has reached the total number of strings (argc). However, it will not compile. for the command line: projectname Jack Todd it should read: Hello Jack Hello Todd Not exactly sure where my error is. I'm compiling with Visual Toolkit 2003. 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]; }; return 0; }; Anyone got any answers? -TJ

##### Share on other sites
int main(int argc, char *argv[])

Is the proper prototype.

And the for statement should look something like
for (int rep = 1; rep < argc; rep++)

Notice no ; at the end! If you do this it will execute a blank statement each time through the loop.
You also shouldn't put a ; after a }.

##### Share on other sites
Quote:
 Original post by uavfunYou also shouldn't put a ; after a }.
It's not so much that you shouldn't as that it has no effect. The key here is learning the meaning of these statements, proper syntax, etc, etc.

##### Share on other sites
#include <iostream>#include <cstring> // Not needed in this case, but can't hurt// See, the code doesn't actually construct any std::strings;// "string literals" in C++ are (unfortunately, to some ways// of thinking) still represented as char *'s.using namespace std;int main (int argc, char argv)// As mentioned, needs to be 'char *argv[]'. An array of char *.// 'char' is just a single letter. Actually, it's a number// which is either between 0 and 255, or -128 and 127, depending// on your implementation, which can be *interpreted* as a single// letter. You start to get the idea of why std::string is a// Good Thing(TM). Heh... Anyway, that causes a problem with the// array dereferencing.{ 	for (int rep = 1, rep > argc, rep++);// As mentioned, get rid of that semicolon! It is legal in C++// to have a for loop with an empty body, which is what that// does. It's also legal to have {} around a block of code that// doesn't need them; it creates a new variable scope.// (probably more useful in C, where you don't have the same// flexibility WRT where you declare your local variables.){	cout << "Hello " << argv[rep];// Not mentioned yet, but you'll want an extra "<< endl" at the// end of that << chain. 'endl' is a special object recognized by// cout, which when output will:// 1) write a newline// 2) (important!) "flush" the stream.// You could also do it with '<< "\n" << flush;' (but why?)// A stream is "buffered" you see; it collects up some data// in memory in a buffer, and only outputs when the buffer is // full (i.e. it needs to make room) or is explicitly told to.// It should also flush at the end of your program, but you// don't really want to count on that.};// Again the semicolon isn't needed here, but this one won't// do any harm (except the harm caused by more experienced // programmers laughing at you ;) )return 0;}; // Ditto this one.

##### Share on other sites
Thanks for the help. It compiles just fine now, but the program still doesn't give me any output. Any suggestions.

Oh, and what is the signifigance of the * before the name of the prototype array?

Thanks again.

-IV

##### Share on other sites
char * means a pointer to a char. Suppose you have char *pc = "Hello!"; The compiler actually stores this as Hello! followed by a zero. pc points to the H; it knows where the end is because of the 0.

So argv[0], argv[1], etc. each point to the beginning of an argument.

• 21
• 15
• 9
• 17
• 13