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

C++ Primer ex 7.16 - arguments to main 15
C++ Primer ex 3.14 8
C++ Primer ex 9.14 11
C++ Primer ex 8.3 21
C++ Primer ex 4.30 10
C++ Primer ex 6.20 36
C++ Primer ex 7.12 2
C++ Primer ex 5.18 5

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top