We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with exercise

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

3
Contributors
3
Replies
14 Minutes
Discussion Span
3 Months Ago
Last Updated
16
Views
Question
Answered
Kareem Klas
Newbie Poster
18 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

what error do you get ?

HunainHafeez
Posting Whiz in Training
256 posts since Aug 2012
Reputation Points: -3
Solved Threads: 4
Skill Endorsements: 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

HunainHafeez
Posting Whiz in Training
256 posts since Aug 2012
Reputation Points: -3
Solved Threads: 4
Skill Endorsements: 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;
}
deceptikon
Challenge Accepted
Administrator
3,499 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58
Question Answered as of 3 Months Ago by HunainHafeez and deceptikon

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0664 seconds using 2.7MB