Lucas Knook
Lucas Knook

Reputation: 15

C++ invalid operands of types 'double' and 'const char [5]' to binary 'operator+'

I am new to c++ and I have been learning for about 4 weeks, and I now started to try programming my own things, like this thing here I am working on:

#include <iostream>
#include <cmath>
using namespace std;

class abcformule{
    public:
        int discriminant(double a, double b, double c){
            return pow(b, 2) - 4 * a *c;
        }

        void answer(double a2, double b2, double c2){
            cout << (-b2 + discriminant(a2, b2, c2)) / (2 * a2) + "and " + (-b2 - discriminant(a2, b2, c2)) / (2 * a2);
        }
};

int main() {
    abcformule abc;
    abc.answer(5, -2, -7);
    return 0;
}

It isn't done yet, but as you might guess, it is supposed to use the abc formula to solve x for quadratic formulas. (I am only 14 years old, I don't want to make homework lol).

So, it gives me this error:

12  58  D:\c++ dev-c\abc.cpp    [Error] invalid operands of types    'double' and 'const char [5]' to binary 'operator+'

What does this mean, and what do I do to fix it?

Upvotes: 1

Views: 1363

Answers (2)

Tobias
Tobias

Reputation: 43

 + "and " + 

in the line

 cout << (-b2 + discriminant(a2, b2, c2)) / (2 * a2) + "and " + (-b2 - discriminant(a2, b2, c2)) / (2 * a2);

does not work as the part before + and the part after the second + are double values which you want to be added to a string. Print these double values directly with cout using "<<" operator instead of"+"

Upvotes: 0

Grant Williams
Grant Williams

Reputation: 1537

The problem is you cant use the "+" operator the way you are in the print statement. Use the "<<" operator instead. Try changing your cout statement to look something like:

cout << (-b2 + discriminant(a2, b2, c2)) / (2 * a2) << " and " << (-b2 - discriminant(a2, b2, c2)) / (2 * a2) << "\n";

Upvotes: 2

Related Questions