1.11M Members

Help with exercise

 
0
 

Hello

I'm using the book C++ Primer 5th edition and in the beginning there is an exercise to change a sum into a prodcut.

#include <iostream>

int main()
{
    std::cout << "The sum of " << v1 << " and " <<v2 << " is " << v1 + v2 << std::endl;
    return 0;
}

Do i just have to put a * instead of + and prodcut instead of sum? If so it didn't run.

The next exercise: We wrote the output in one large statement. Rewrite the
program to use a separate statement to print each operand.

What do they mean by that exactly?

Regards

Kareem

 
1
 

what error do you get ?

 
-2
 
#include <iostream.h>
#include <conio.h>
void main()
{
    int v1 = 2;
    int v2= 3;
    cout << "The sum of " << v1 << " and " <<v2 << " is " << v1 + v2 << endl;
    cout << "The product of " << v1 << " and " <<v2 << " is " << v1 * v2 << endl;

    getch();
}

try this

 
0
 

Do i just have to put a * instead of + and prodcut instead of sum? If so it didn't run.

Nope, you also need to define and initialize v1 and v2. Then it'll work:

#include <iostream>

int main()
{
    int v1 = 2;
    int v2 = 5;
    std::cout << "The sum of " << v1 << " and " <<v2 << " is " << v1 * v2 << std::endl;
    return 0;
}
Question Answered as of 1 Year Ago by HunainHafeez and deceptikon
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article