I'm trying to write a program that prompts a user for a radius value, then uses class data to return info of a sphere based on that radius.

I believe my problem lies in declaring what information goes into what class member, but I'm not entirely sure. How do I make this work?

Also, when using cin.get(), the user has to hit enter to exit the program. Is there a way that they literally hit 'any key' for them to exit (besides of course system("Pause");?

#include <iostream>
#include <iomanip>
#include <math.h>
float pi=3.14159;
using namespace std;

class Sphere
{
      private:
              float radius;
              float Vol;
              float SA;
              
      public:
             /*Radius(double = .5);
             Radius(const Radius&);
             ~Radius();
             void SetRadius(double aRadius);  */
             float GetRadius(void);
             void ShowData(void);
             float ConvRadToVol(float arad);
             float ConvRadToSA(float arad);
};

int main (void)
{
    //Object Initialization
    Sphere Sweet;

    cout << "This program takes a given radius value and computes the data of a sphere with that radius" << endl;
    Sweet.GetRadius();
    cout << "A sphere with that radius would have the following properties:\n" << endl;
    Sweet.ShowData();
   	
    system("Pause");

	return 0;

}

//Defining radius
float Sphere::GetRadius(void)
{
      cout << "What is the radius?" << endl;
      cin >> radius >>endl;
      return radius;
}

//Calculating Vol
float Sphere::ConvRadToVol(float radius)
{
     if (radius<0)
     {
               std:cerr << "Radius can't be negative\n";
     }
     else
     {
         Vol = (4/3)*pi*pow(radius,3) ;
         return Vol;
     }
}

//Calculating Surface Area
float Sphere::ConvRadToSA(float radius)
{
      if (radius<0)
      {
                 std:cerr << "Radius can't be negative\n";
                 }
      else
      {
          SA = 4*pi*pow(radius,2) ;
          return SA;
      }
}
    
//Displaying the sphere data
void Sphere::ShowData(void)
{
     cout << "Volume: " << Vol << endl;
     cout << "Surface Area: " << SA << endl;
}

line 31: that is calling GetRadius() ok but throwing away its return value. flat radius = Sweet.GetRadius(); Other similar lines have the same problem.

line 45: endl only works with cout.

lines 54 and 68: those functions must return some value even if it fails the validation. return 0 would be an appropriate value to return.

Thanks for your help!

I applied the corrections and got it to work almost perfectly.

The volume is defined as

Vol = (4/3)*pi*pow(radius,3) ;

but it doesn't want to multiply by (4/3). Is there something about basic math operators that I forgot?

4/3 is integer math to get floating point math to 4.0/3.0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.