1,105,254 Community Members

Help with exercise

Member Avatar
Kareem Klas
Newbie Poster
18 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
HunainHafeez
Posting Whiz
340 posts since Aug 2012
Reputation Points: 9 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 2 [?]
 
1
 

what error do you get ?

Member Avatar
HunainHafeez
Posting Whiz
340 posts since Aug 2012
Reputation Points: 9 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 2 [?]
 
-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

Member Avatar
deceptikon
Eternally Awesome
4,662 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
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