Thread: sqrt problem

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Try
    Code:
        s = sqrt(r * r + h * h);

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Oh! I see now, I'm multiplying the square root.. thanks for pointing that out

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> 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.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM