1

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?

2 Answers2

2

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";
Grant Williams
  • 1,469
  • 1
  • 12
  • 24
  • good luck! I wrote this same problem as my first ever program in basic on my calculator back in the day. It definitely gets easier with practice! – Grant Williams Apr 18 '18 at 14:57
0
 + "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"+"

Tobias
  • 43
  • 9
  • 1
    I dont know if it really makes sense to cast the doubles to a string when you can just print the statements separately. – Grant Williams Apr 18 '18 at 14:56
  • You are right, i didn't really know how to "spell" it but the "<<" operator is the better way. I'll edit my answer – Tobias Apr 18 '18 at 14:57