1

I am attempting to create a program that calculates the area and circumference of a circle. But I keep getting an error that states

error: invalid operands to binary expression ('const char *' and 'double') 

on two lines and I don't know how to solve it here is the code:

int main()
{
    const double MY_PI = 3.14159265;
    double radius;

    cout << "Program calculates the area and circumference of a circle" << endl;
    cout << "enter circle radius" << endl;
    cin >> radius;

    double area = MY_PI * (radius*radius);
    double circumference = 2 * MY_PI*radius;

    //these are the lines with errors
    double AREA_STR = "Area of circle with radius " + radius + " is " + area;
    double CIRCUM_STR = "Circumference of a circle with radius " + radius + " is " + circumference;

    cout << AREA_STR << endl;
    cout << CIRCUM_STR << endl;

    return 0;
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
A-A-Ron
  • 13
  • 3
  • What you're doing makes no sense... What is it you try to accomplish? Why would a variable named `AREA_STR` (indicating that it should be a *string*) be of type `double`? Perhaps it's time you [get a few good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282)? – Some programmer dude Jan 22 '19 at 05:32
  • As for what I *think* you want to do, don't use the temporary `AREA_STR` (etc.) variables, just use the output directly, as in: `std::cout << "Area of circle with radius " << radius << " is "<< area << '\n';` – Some programmer dude Jan 22 '19 at 05:34

1 Answers1

1

The variables AREA_STR and CIRCUM_STR are the type of double, where you are trying to put some const char* (strings), which gives you the error.

You need simply:

std::cout << "Area of circle with radius " << radius << " is " << area << '\n';
std::cout <<  "Circumference of a circle with radius " << radius << " is " << circumference << '\n';

In case you need them in a single variable, the doubles(i.e. area, radius, and circumference) have to be coverted std::to_string()

#include <string>

const std::string AREA_STR = "Area of circle with radius " + std::to_string(radius) + " is " + std::to_string(area);
const std::string CIRCUM_STR = "Circumference of a circle with radius " + std::to_string(radius) + " is " + std::to_string(circumference);
JeJo
  • 30,635
  • 6
  • 49
  • 88