main(int arc, char *argv[])

P

pkirk25

int main(int argc, char *argv[])
{
int z = 0;

cout << *argv[2] << endl;
return z;
}

tester hi james outputs the letter 'j'

How can I get ti to outout the whole word?
 
P

Phlip

pkirk25 said:
int main(int argc, char *argv[])
{
int z = 0;

cout << *argv[2] << endl;
return z;
}

tester hi james outputs the letter 'j'

This is not chat; please edit typos in your lines!
How can I get ti to outout the whole word?

Take out the *.
 
V

VJ

pkirk25 said:
int main(int argc, char *argv[])
{
int z = 0;

cout << *argv[2] << endl;
return z;
}

tester hi james outputs the letter 'j'

How can I get ti to outout the whole word?

Like this:


#include <iostream>
int main(int argc, char *argv[])
{
std::string str( argv[2] );
std::cout << str << std::endl;
}
 
V

Victor Bazarov

pkirk25 said:
int main(int argc, char *argv[])
{
int z = 0;

cout << *argv[2] << endl;

Neither 'cout' nor 'endl' are defined. Did you forget to include
a proper header? BTW, your program has undefined behaviour if
the number of arguments is less than 3.
return z;
}

tester hi james outputs the letter 'j'

How can I get ti to outout the whole word?

Drop the asterisk in the output statement.

V
 
V

Victor Bazarov

VJ said:
pkirk25 said:
int main(int argc, char *argv[])
{
int z = 0;

cout << *argv[2] << endl;
return z;
}

tester hi james outputs the letter 'j'

How can I get ti to outout the whole word?

Like this:


#include <iostream>
int main(int argc, char *argv[])
{
std::string str( argv[2] );

'std::string' is undefined.
std::cout << str << std::endl;

And why do you need the 'str' anyway? Couldn't you just do

std::cout << argv[2] << std::endl;

?

V
 
P

pkirk25

Sorry for my awful spelling in original post.

Many thanks for pointing out that removing the * fixed my problem.

As you probably guessed, tester was written just to post this question
rather than bore you with extra variables from the program where I need
to pass arguments.
 
F

Frederick Gotham

Victor Bazarov:
And why do you need the 'str' anyway? Couldn't you just do

std::cout << argv[2] << std::endl;


There is a trend currently propogating whereby C++ programmers try to make
their programs as inefficient as possible, for sake of appearing to use safe
features. This extends to the use of "vector" when all that was needed was a
simple array.
 
V

VJ

Victor said:
VJ said:
#include <iostream>
int main(int argc, char *argv[])
{
std::string str( argv[2] );


'std::string' is undefined.

Forgot to
#include <string>
but my program compiled. It is probably included in the iostream (at
least in the library I am using)
std::cout << str << std::endl;


And why do you need the 'str' anyway? Couldn't you just do

std::cout << argv[2] << std::endl;

?

You could :)
Thanks
 
V

VJ

Victor said:
But you shouldn't rely on that.

Yes, I dont. When reviewing my own code, I always include missing
headers (like in this case), alhtough it compiled well.

But this time, I didnt review this example ;)
 

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

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top