Combining float with string

Hi guys, I recently started programming in C++. I don't have much experience with desktop programming but have extensive experience (4 years +) of web languages (PHP etc.). Anyways that's beside the point.

Pretty much for my computer science class, we were told to develop a quadratic formula which would find the x-intercepts. The program works perfectly except when it comes to imaginary roots.

The variables roots_imaginary_1 & roots_imaginary are both string datatype while all other variables are float. The compiler gives me an error "invalid operands of types `const char[3]' and `float' to binary `operator<<".

Anyways I was wondering if someone can help me out with this. Thanks!

1
2
3
4
roots_imaginary_1 = "( " << (-b) << " + " << (sqrt(-discriminant)) << 
                                      "i ) / " << ( 2 * a );
 roots_imaginary_2 = "( " << (-b) << " - " << (sqrt(-discriminant)) <<
                                      "i ) / " << ( 2 * a );
Last edited on
Strings don't use the << operator for combining them. They use the + operator.

If you want to convert a number to a string you can use this nifty converter function:
1
2
3
4
5
6
7
template<typename NumTy>
string String(NumTy Num)
{
	stringstream StrStream;
	StrStream << Num;
	return(StrStream.str());
}
You need to include <string> and <sstream>
As an example, to convert an int to a string: SomeString = String<int>(SomeInt);
Last edited on
Thank you very much L B. Works like a charm. :)
One more question L B. I am successfully able to convert the float to strings but for some reason, the compiler gives me an error about the operator +.

1
2
roots_imaginary_1 = "( " + ss_b + " + i" + ss_discriminant + " ) / " + ss_a;
roots_imaginary_2 = "( " + ss_b + " - i" + ss_discriminant + " ) / " + ss_a;


I'm including the necessary sstream.h & string.h files but I don't know what the problem is.


The error:
no match for 'operator+' in '"( " + ss_b'
Last edited on
Is ss_b the same variable type as ss_a? Or does " ) / " + ss_a have an error as well?
Are the ss_ variables stringstreams? If so, you need to convert them to strings first with ss_whatever.str()
Sorry for the confusion. I don't think it matters if it's ss_b, ss_a, or ss_discriminant. ss standing for string stream.

Here is the code that I have.

1
2
3
4
5
6
7
8
9
10
11
stringstream ss_a, ss_b, ss_discriminant;

ss_a << (2 * a);
ss_a.str();
ss_b << (-b);
ss_b.str();
ss_discriminant << sqrt(-discriminant);
ss_discriminant.str();
                
roots_imaginary_1 = "( " + ss_b + " + i" + ss_discriminant + " ) / " + ss_a;
roots_imaginary_2 = "( " + ss_b + " - i" + ss_discriminant + " ) / " + ss_a;
Hey guys, I compiled this in CodeBlocks and I get the same error.
Stringstreams are not strings. You cannot add them together like strings. To convert them to a string you need to use MyStringStream.str()

Simply putting MyStringStream.str() by itself does nothing...

You want this:
1
2
roots_imaginary_1 = "( " + ss_b.str() + " + i" + ss_discriminant.str() + " ) / " + ss_a.str();
roots_imaginary_2 = "( " + ss_b.str() + " - i" + ss_discriminant.str() + " ) / " + ss_a.str();
Last edited on
Thank you very much for your help L B. Really appreciated. You saved me a whole bunch of trouble and headache. Thanks! :)
Topic archived. No new replies allowed.