Home Forums C Programming invalid operands of types `float’ and `int’ to bin

Viewing 2 reply threads
  • Author
    Posts
    • #2020
      chucky
      Participant

      I’m a beginner, so not too harsh on the criticism. Ok, so I have the code


      void circle()
      {
      int r;
      double Area;
      cout << "Enter the radius: ";
      cin >> r;
      Area = PI * r^2;
      cout << "The area is " << Area << "n";
      main();
      }

      and i try to compile it and i get an error saying “38 invalid operands of types `float’ and `int’ to binary `operator^'”. What should I do?

    • #3260
      msaqib
      Participant

      Hello
       
      Well there is a sligh problem with your code. You are using ^ as a power operator but there is no such operator which performs the power operations. This operator (^) is a bitwiase operator to calculate XOR.

      To calculate the power you can use pow(double x, double y) function. It will calculate x power y. 
       
      Also make sure you include math.h header in your program as pow function is defined is this header file. 
       
      So your function should look like

      void circle()
      {
       double r=0.0;
       double m=2.0;
       double area=1;
      printf("enter area");
       scanf("%lf",&r);
       area =PI* pow(r,m);
       printf("%lf",area);
       getch();
      }
    • #3261
      S.Thananchayan
      Participant
      #include 
      using namespace std;
      int main()
      {
          float pi = 3.14;
       int r;
          double area;
          cout << "Enter the radius: ";
          cin >> r;
          area =  r * r * pi;
          cout << "The area is "  << area << "n";
          return(0);
       
      }  

       
       
         

Viewing 2 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.