I have to create a program that will prompt a user radius and keep adding them until user says no. Then, the program gotta able to hold those numbers and calculate two things: Surface area and Volume. The program also gotta able to Sort the numbers by its volume(low to high).
And the program will also gotta able to do Displaying all the values that users put(Radius, volume and Surface)
- I don't understand why in
float Sphere::variable()
wouldn't save the value...(Seems like I can't even display what I put)
-How do I pass the radius value in float Sphere::variable()
to float Sphere::area()
and float Sphere::volume()?
?
Here's my code:
Sphere.cpp
#include <iostream>
#include "Sphere.h"
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
using namespace std;
Sphere::Sphere()
{
}
float Sphere::variable()
{
vector<int> list;
string resp;
do{
cout << "radius? " << endl;
cin >> Sph_radius;
list.push_back(Sph_radius);
cout << "Continue(y/n)";
cin >> resp;
}
while (tolower(resp[0])=='y');
cout << Sph_radius << endl;
}
float Sphere::area()
{
return 4*M_PI*pow(Sph_radius, 2);
}
float Sphere::volume()
{
return (4*M_PI*pow(Sph_radius, 3))/3;
}
Sphere.h
#ifndef SPHERE_H
#define SPHERE_H
class Sphere
{
public:
Sphere();
float variable();
float area();
float volume();
Sphere(float);
double Sph_radius, Sph_SA, Sph_Vol;
private:
};
#endif // SPHERE_H
main.cpp
#include <iostream>
#include "Sphere.h"
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
system("CLS"); //Flush the stream
int loop = 1;
int choice;
string getinput;
bool done = false;
while (!done)
{
system("CLS"); //Flush the stream
cout << "--Menu--" << endl << endl;
cout << "1. Add spheres" << endl;
cout << "2. Sort sphere" << endl;
cout << "3. Displays all" << endl;
cout << "4. Quit" << endl;
cin >> choice;
if(choice < 0 || choice > 5)
cout << "Between 1~6 please." << endl; // between 1~6
system("pause");
switch(choice)
{
case 1:// Sphere
{
Sphere info;
info.variable();
system("pause");
break;
}
case 2:// Sort sphere
{
break;
}
case 3:// Displays all
{
Sphere info;
cout << "test1" << endl;
info.area();
cout << "test2" << endl;
system("pause");
break;
}
case 4: //Quit, couldn't find any method, other than exit(0);
{
if(choice==4)
{
cout << "Thank you for using the program and See you later!";
exit(0);
}
}
}
}
return 0;
}
Someone help me please... I am so lost.