2013-02-23 88 views
-5

本方案的所期望的结果的语句和函数如下:问题与环路涉及如果是

选择形状(1)球(2)筒(3)锥形(q)中退出:1
选择计算(1)体积(2)表面积:1
输入球体的半径:球体的5.5
体积是696.91

选择形状(1)球(2)筒(3)锥形(q)中退出:1
选择计算(1)体积(2)表面积:2
输入球体的半径:球体的5.5
表面积是380.133

选择形状(1)球(2)筒(3)锥形(q)中退出:2
选择一个计算(1)体积( 2)表面积:1
输入气缸的半径:5.5
输入气缸的高度:4.2
卷筒的是399.139

选择形状(1)球(2)筒(3)锥形(q )quit:2
选择一个计算(1)volume(2)surface ar EA:2
输入气缸的半径:5.5
输入气缸的高度:气缸的4.2
表面积是335.208

选择形状(1)球(2)筒(3)锥形(q)中退出:3
选择一个计算(1)体积(2)表面积:1
输入锥体的半径:5.5
输入锥体的高度:4.2
卷锥体的是133.046

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

选择形状(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))); 
    } 
+0

以什么方式不能正常工作?你到目前为止做了哪些调试? – 2013-02-23 11:39:05

+1

请不要发表两次相同的问题。 – 2013-02-23 11:40:53

回答

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

由于您使用在mainsphere_volume,你应该定义更改为sphere_volume

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

添加循环的一种方法是在从第一个cout开始并在system("pause")while(shapeCall != 'q')之间结束之前包含代码。