int main(int argc, char * argv[]) - basic question

M

mlt

I have an application with the follwowing main function:

int main(int argc, char * argv[])
{
String p1, p2, c;

switch ( argc ){
case 1:{
std::cerr << "case1" << std::endl;
return 1;
}
case 2:{
p1 = argv[1];
std::cerr << "case2" << std::endl;
return 1;
}
case 3:{
p1 = argv[1];
p2 = argv[2];
std::cerr << "case3" << std::endl;
break;
}
case 4:
p1 = argv[1];
p2 = argv[2];
c = argv[3];
break;
}

I run it from cmd like:

app.exe 2 "c:\test\test.txt"

But it matches case 3. I also matches case 3 if I do:

app.exe 7 "c:\test\test.txt"

I thought that the first argument to app.exe would be stored in 'argc' which
is then switched upon. But that seems not to be the case, any ideas?
 
V

Victor Bazarov

mlt said:
I have an application with the follwowing main function:

int main(int argc, char * argv[])
{
String p1, p2, c;

'String' undefined.
switch ( argc ){
case 1:{
std::cerr << "case1" << std::endl;
return 1;
}
case 2:{
p1 = argv[1];
std::cerr << "case2" << std::endl;
return 1;
}
case 3:{
p1 = argv[1];
p2 = argv[2];
std::cerr << "case3" << std::endl;
break;
}
case 4:
p1 = argv[1];
p2 = argv[2];
c = argv[3];
break;
}

I run it from cmd like:

app.exe 2 "c:\test\test.txt"

But it matches case 3. I also matches case 3 if I do:

app.exe 7 "c:\test\test.txt"

I thought that the first argument to app.exe would be stored in 'argc'
which is then switched upon. But that seems not to be the case, any ideas?

<g> OK... OK... You *thought* that the first argument to app.exe would
be stored in 'argc'? Why did you think that? Where did you get the
idea? And what does your favourite C++ book say about the arguments to
the 'main' function? No, really. RTFM, will you?

V
 
V

Victor Bazarov

Donkey said:
ANY book, no matter how cheap, will do.

ANY book may not actually be worth getting in general, unless the 'main'
function is all that's needed from the book... :)

V
 
J

James Kanze

ANY book may not actually be worth getting in general, unless
the 'main' function is all that's needed from the book... :)

"Any" book may do more harm than good. See the recent thread
started by someone who had read Schildt.

Different people react differently to different pedological
presentations, so there is no one best book for everyone. On
the other hand, there are definitely books that are best avoided
by everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top