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

Recommended Answers

All 3 Replies

what error do you get ?

commented: Thanks for the reply. Well after the help of deceptikon it worked. But, it closes directly after it opens. Anyway to make the file pause or freeze when it opens? +0
#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

commented: You made the code worse by reverting it to prestandard Turbo C++ style. -3
commented: Thanks for the reply. But this code is very weird. like the <iostream.h> and <conio.h>. But thank you for trying to help me, I really appreciated it, and now I see a different way of doing it. +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;
}
commented: Thank you! It helped me alot. Weird that the book didn't say me to define and intialize v1 & v2. Really thanks alot! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.