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

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?

Do you mean this?

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

I tried putting double radius = input(); into my functions and then typing

 double radius = input();
 area_of_circle(radius);
 volume_of_sphere(radius);
 surface_area_of_sphere(radius);

and it’s not working.

Declare a parameter as in:

void area_of_circle(radius) {
  double radius = input();
  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";
}

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

Is this what you meant?

#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\n";
}

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

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);
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.