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!

1 Year
Discussion Span
Last Post by Kenneth_3

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;

Edited by Moschops


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?

Edited by Kenneth_3


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.

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.