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

Featured Replies in this Discussion

 
1
 

what error do you get ?

Comments
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?
 
-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

Comments
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.
You made the code worse by reverting it to prestandard Turbo C++ style.
 
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;
}
Comments
Thank you! It helped me alot. Weird that the book didn't say me to define and intialize v1 & v2. Really thanks alot!
Question Answered as of 2 Years 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