C++ Primer ex 7.15 ambiguous statement

A

arnuld

STATEMENT: write a main function that takes two vales as arguments and
print their sum.


the main takes classic arguments ;) int main( int argc, char **argv )

1st argument is an int while 2nd argument is an array of string literals.
how am i supposed to add a string literl to an int ?
 
G

Gianni Mariani

arnuld said:
STATEMENT: write a main function that takes two vales as arguments and
print their sum.


the main takes classic arguments ;) int main( int argc, char **argv )

1st argument is an int while 2nd argument is an array of string literals.
how am i supposed to add a string literl to an int ?

Maybe convert the strings to an int ?


int x = Convert( "-122" );
int y = Convert( "122" );

int sum = x + y;

??

What does Convert look like ?

What does Convert( "Google" ) do ?
 
D

Daniel T.

arnuld said:
STATEMENT: write a main function that takes two vales as arguments and
print their sum.


the main takes classic arguments ;) int main( int argc, char **argv )

1st argument is an int while 2nd argument is an array of string literals.
how am i supposed to add a string literl to an int ?

Try this:

int main( int argc, char* argv[] )
{
for ( int i = 0; i < argc; ++i )
cout << argv << '\n';
}

Then run your program using:

program 1 2

You should end up with the following output:

program
1
2

Now all you have to do is figure out how to add the 1 and 2 to produce 3
and output it instead.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top