Hello,

I am writting a code for class and I am having a hard time inserting my variables into my "cout". I know how to do it the long way but I know it has to be easier.

Right know this is the portions I am working on:

void displaySlpInt (double m, double b)
{
    cout << (("y = %f x - %f)\n"), m, b) << endl;
}

I have already solved for the values m and b in an earlier function but when I run this function it only gives me the "b" value.
Both m and b are set to double and I have included: iostream,iomanip,string and while I know it is frowned upon I do have "using namespace std." Any guidence is appreciated, thanks in advance!

Recommended Answers

All 4 Replies

You've mixed up printf and cout.

cout doesn't do substitution of %f. That's a printf thing. With cout , you just state what you want output with a sequence of << operators:

cout << "y = " << m << " x - " << b << endl;

Is there a particular benifit from one over the other, we have not gone over the printf and is there any reason you should not mix the two?

Use one or the other in C++, not both. Cout is preferred. You can use sprintf() to push the data into a string, and then output that with cout. Sometimes, that is a reasonable alternative, allowing you to use printf() formatting options to build the string you want to output.

Thanks for the help. I got the code looking the way I want and the correct outputs.

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.