Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way that I can get there to be only one input in my program?

I am writing a program where I calculate the area of a circle, the volume of a sphere, and the surface area of a sphere and I need a way to get one source of user input. I don’t want the program to keep asking for user input the way that it is in the functions.

I want my program to run like this:

Enter a radius in inches: 3.3
Area of a circle is 34.21 square inches
Volume of a sphere is 150.53 cubic inches
Surface area of a sphere is 136.85 square inches

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is what I have so far though:

#include <iostream>
#include <cmath>
#define Pi 3.1415
using namespace std;


void area_of_circle() {
  double radius;
  std::cout << "Enter a radius in inches: " << endl;
  cin >> radius;
  double areaCircle = Pi * radius * radius;
  std::cout << "The area of the circle is " << std::ceil(areaCircle * 100.0) / 100.0 << " square inches";
}


void volume_of_sphere() {
  double radius;
  std::cout << "Enter a radius in inches: " << endl;
  cin >> radius;
  double volumeSphere = (4/3) * Pi * radius * radius * radius;
  std::cout << "The volume of the sphere is " << std::ceil(volumeSphere * 100.0) / 100.0 << " cubic inches";
}

void surface_area_of_sphere() {
  double radius;
  std::cout << "Enter a radius in inches: " << endl;
  cin >> radius;
  double surfaceAreaSphere = 4 * Pi * radius * radius;
  std::cout << "The volume of the sphere is " << std::ceil(surfaceAreaSphere * 100.0) / 100.0 << " square inches";
  
}

int main() {
  area_of_circle();
  volume_of_sphere();
  surface_area_of_sphere();
}

Can someone help, please?

I tried getting creating a separate function to deal with input, but it’s not working:

void input() {
  std::cout << "Enter a radius in inches: " << endl;
}
  
int main() {
  double radius;
  area_of_circle();
  volume_of_sphere();
  surface_area_of_sphere();
}

>Solution :

  1. Get an input
  2. Store the input to a variable
  3. Pass the input value as an argument
#include <iostream>
#include <cmath>
#define Pi 3.1415
using namespace std;

double input() {
  double radius;
  std::cout << "Enter a radius in inches: " << endl;
  cin >> radius;
  return radius;
}

void area_of_circle(double radius) {
  double areaCircle = Pi * radius * radius;
  std::cout << "The area of the circle is " << std::ceil(areaCircle * 100.0) / 100.0 << " square inches";
}


void volume_of_sphere(double radius) {
  double volumeSphere = (4/3) * Pi * radius * radius * radius;
  std::cout << "The volume of the sphere is " << std::ceil(volumeSphere * 100.0) / 100.0 << " cubic inches";
}

void surface_area_of_sphere(double radius) {
  double surfaceAreaSphere = 4 * Pi * radius * radius;
  std::cout << "The volume of the sphere is " << std::ceil(surfaceAreaSphere * 100.0) / 100.0 << " square inches";
  
}

int main() {
  double radius = input();
  area_of_circle(radius);
  volume_of_sphere(radius);
  surface_area_of_sphere(radius);
}

Also note that (4/3), which is used in the function volume_of_sphere, means 1 because dividing an integer by an integer in C++ yields an integer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading