涉及if语句和函数的循环问题

时间:2013-02-23 11:37:54

标签: c++ function if-statement

该计划的预期成果如下:

选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:1
选择计算(1)体积(2)表面积:1
输入球体半径:5.5
球体积为696.91

选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:1
选择计算(1)体积(2)表面积:2
输入球体半径:5.5
球体的表面积为380.133

选择一个Shape(1)球体(2)圆柱体(3)锥体(q)退出:2
选择计算(1)体积(2)表面积:1
输入圆柱半径:5.5
输入气缸高度:4.2
圆柱体积为399.139

选择一个Shape(1)球体(2)圆柱体(3)锥体(q)退出:2
选择计算(1)体积(2)表面积:2
输入圆柱半径:5.5
输入气缸高度:4.2
圆筒表面积为335.208

选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:3
选择计算(1)体积(2)表面积:1
输入圆锥半径:5.5
输入锥高:4.2
锥体积为133.046

选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:3
选择计算(1)体积(2)表面积:2
输入圆锥半径:5.5
输入锥高:4.2
锥的表面积是214.607

选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:q

再见!

我想你可以告诉我在哪里这个...为什么我不能让我的循环正常工作?

#include <iostream>
#include <math.h>

using namespace std;

double sphere_volume(double radius);
double sphere_surface_area(double radius);
double cylinder_volume(double radius, double height);
double cylinder_surface_area(double radius, double height);
double cone_volume(double radius, double height);
double cone_surface_area(double radius, double height);

int main()
{
double entHeight;
double entRadius;
char shapeCall;
char compCall;

cout << "Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: ";
cin >> shapeCall;
cout << "Select a Computation (1) volume (2) surface area: ";
cin >> compCall;


    if ( shapeCall == 1 && compCall == 1)
    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << sphere_volume (entRadius) << endl; 

    }
    if ( shapeCall == 1 && compCall == 2)

    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << sphere_surface_area (entRadius) << endl;
    }
    if ( shapeCall == 2 && compCall == 1)
    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << "Enter Height: ";
            cin >> entHeight;
            cout << cylinder_volume (entRadius, entHeight) << endl;
    }
    if (shapeCall == 2 && compCall == 2)
    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << "Enter Height: ";
            cin >> entHeight;
            cout << cylinder_surface_area (entRadius, entHeight) << endl;
    }
    if (shapeCall == 3  && compCall == 1)
    {
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << "Enter Height: ";
        cin >> entHeight;
        cout << cone_volume (entRadius, entHeight) << endl;
    }
    if (shapeCall == 3 && compCall == 2)
    { 
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << "Enter Height: ";
        cin >> entHeight;
        cout << cone_surface_area (entRadius, entHeight) << endl;
    }
system ("pause");
return 0;
}


double sphere_Volume(double radius) 
  { 
   return 4.0 / 3.0 * 3.14159 * pow( radius, 3 ); 
  }
double cylinder_volume(double radius, double height)
  { 
  return 3.14159 * pow(radius, 2) * height;
  }
double cone_volume(double radius, double height)
  {
  return 1.0 / 3.0 * 3.14159 * pow(radius, 2) * height;
  }
double sphere_surface_area(double radius)
  {
  return 4.0 * 3.14159 * pow(radius, 2);
  }
double cylinder_surface_area(double radius, double height)
  {
  return (2.0 * 3.14159 * pow(radius, 2)) + (2.0 * 3.14159 * radius * height);
  }
double cone_surface_area(double radius, double height)
  {
  return (3.14159 * pow(radius, 2)) + (3.14159 * radius * sqrt(pow(radius, 2) +  pow(height, 2)));
  }

1 个答案:

答案 0 :(得分:0)

有两个主要问题。您声明sphere_volume

double sphere_volume(double radius);

但您定义sphere_Volume

double sphere_Volume(double radius) 
{ 
    return 4.0 / 3.0 * 3.14159 * pow( radius, 3 ); 
}

由于您在sphere_volume中使用main,因此您应该将定义更改为sphere_volume

另一个问题是你实际上没有循环。您的main函数将运行一次,但它会点击return 0;,程序将终止。

添加循环的一种方法是在从第一个cout开始到system("pause")之前while(shapeCall != 'q')结束后将代码括起来。