sqrt problem

Printable View

• 12-09-2006
kkid28
sqrt problem
I'm trying to get a math program I'm writing to work but I can't get the sqrt function thing to work, can someone tell me what the problem is?

Code:

```  case 3:       float cone(float r, float h, float s);       cout<<"What is the radius of the base?"; cin>> r ; cout<<">>>\n\n";           cout<<"What is the height of the cone?"; cin>> h ; cout<<">>>\n\n";           s = sqrt * (r * r + h * h);  <--- This is the error line           cout<<"The area of the base is: "<< 3.14159265358979323846 * (r * r) <<"\n\n";           cout<<"The surface area of the triangle is: "<< 3.14159265358979323846 * r * (r+s) <<"(requires the slope)\n\n";           cout<<"The height of the sloped side is: "<< s <<"(only valid if radius and height are given)\n\n";           cout<<"The volume of the cone is: "<< 3.14159265358979323846 * r * r * h/3 <<"\n\n";       break;```
Also when I go to compile I get this error message at the bottom...

"invalid operands of types `double ()(double)' and `float' to binary `operator*' "

I'm using float number since I need pi.
• 12-09-2006
robatino
Try
Code:

`    s = sqrt(r * r + h * h);`
• 12-09-2006
kkid28
Oh! I see now, I'm multiplying the square root.. thanks for pointing that out ;)
• 12-09-2006
Tonto
>> float cone(float r, float h, float s);

I don't know what you intend by this line, but what it actually is is a declaration of a function named 'cone' that takes 3 floats for arguments and returns a float. To be normal you could say

Code:

`float r, h, s;`
In place of that. But who wants to be normal? To be funky, you could be like

Code:

```struct cone {     float r, h, s;     float surfaceArea() { /* implementation */ }     float baseArea() { /* implementation */ }     float slopeHeight() { /* implementation */ }     float volume() { /* implementation */ } } c; cin >> c.r >> c.h >> c.s; cout << blather_bloother_toother;```
Note: If you #define _USE_MATH_DEFINES before #including <cmath> than you can use the M_PI symbol.
• 12-09-2006
kkid28
Yyyyyyyeah..... I'm like brand new at C++, I started yesterday, so far this is my most advanced program:

Code:

```#include <iostream> #include <math.h> using namespace std; float r, h, s; int n;         int main() {   int input;     cout<<"\n\n              ============ Welcome to the Round shapes program ============\n\n\n";   while (input != 4){   cout<<"            1. Circle\n";   cout<<"            2. Sphere\n";   cout<<"            3. Cone\n";   cout<<"            4. Exit\n\n\n";   cout<<"Selection: ";   cin>> input;   switch ( input ) {   case 1:       float circle(float r);       cout<<"What is the radius of the circle?"; cin>> r ; cout<<">>>\n\n";             cout<<"The circumference of the circle is: "<< (2 * r) * 3.14159265358979323846 <<"\n\n";             cout<<"The area of the circle is: "<< 3.14159265358979323846 * (r * r) <<"\n\n";       break;         case 2:       float circle(float r);       cout<<"What is the radius of the circle?"; cin>> r ; cout<<">>>\n\n";             cout<<"The surface area of the sphere is: "<< 4 * 3.14159265358979323846 * (r*r*r) <<"\n\n";             cout<<"The volume of the sphere is: "<< (4 * 3.14159265358979323846)/3 * (r*r*r) <<"\n\n";       break;         case 3:       float cone(float r, float h, float s);       cout<<"What is the radius of the base? >>>"; cin>> r ; cout<<"\n\n";           cout<<"What is the height of the cone? >>>"; cin>> h ; cout<<"\n\n";           s = sqrt(r * r + h * h);           cout<<"The area of the base is: "<< 3.14159265358979323846 * (r * r) <<"\n\n";           cout<<"The surface area of the triangle is: "<< 3.14159265358979323846 * r * (r+s) <<"\n\n";           cout<<"The height of the sloped side is: "<< s <<"\n\n";           cout<<"The volume of the cone is: "<< 3.14159265358979323846 * r * r * h/3 <<"\n\n";       break;         case 4:       cout<<"Press \"Enter\" to close...\n";     break;       default:     cout<<"Error, bad input, quitting\n";     break;   }   cout<<"Press \"Enter\" to continue on.\n\n";   cin.get();   cin.get();   } }```

-----Edit-----
Don't you just love how I took pi to 21 digits? lol Overkill much?