Visual Studio调试问题:仅显示Sphere模块,无法输入其他选项
C++形状表面积与体积计算程序调试问题
我编写的C++程序用于让用户选择形状,输入参数计算其表面积和体积,但调试时仅显示Sphere(球体)部分,无法输入其他选项。以下是我的代码:
#include <iostream> #include <iomanip> #include <cmath> #include <string> using namespace std; int main() { //Text cout << "\nHello there! This program will help " << endl; cout << "\ncalulate the surface areas and the " << endl; cout << "\nvolumes of the displayed shapes below." << endl; cout << endl; cout << "----------------------------------------" << endl; cout << " A: Sphere " << endl; cout << " B: Cube " << endl; cout << " C: Dodecahedron " << endl; cout << " D: Cylinder " << endl; cout << " E: Cone " << endl; cout << "----------------------------------------" << endl; cout << endl; //Variables double Sphere = 0.0, Cube = 0.0, Dodecahedron = 0.0, Cylinder = 0.0, Cone = 0.0; float A = (Sphere), B = (Cube), C = (Dodecahedron), D = (Cylinder), E = (Cone); //Question // enter code here cout << "Please select one of the following..." << endl; cin >> A; cin >> B; cin >> C; cin >> D; cin >> E; { //Sphere int SphereRadius = 0.0; double pi = 3.1415926535898; double SphereSA = (4 * pi * (SphereRadius * SphereRadius)); double SphereV = ((int(4 / 3)) * (pi * (SphereRadius * SphereRadius * SphereRadius))); if (Sphere); cout << "\nIn order to find the surface area and " << endl; cout << "\nvolume we will need a radius. " << endl; cout << endl; cout << "\nPlease input a positive number for the radius: " << endl; cin >> SphereRadius; cout << endl; cout << "\nWith the given radius... " << endl; cout << endl; cout << "\nSurface area will be: " << setprecision(2) << SphereSA << endl; cout << endl; cout << "\nVolume will be: " << setprecision(2) << SphereRadius << endl; } { //Cube int CubeSide = 0.0; double CubeSA = (6 * (CubeSide * CubeSide)); double CubeV = (CubeSide * CubeSide * CubeSide); if (Cube); cout << "\nIn order to find the surface area and " << endl; cout << "\nvolume we will need a side length. " << endl; cout << endl; cout << "\nPlease input a positive number for the side length: " << endl; cin >> CubeSide; cout << endl; cout << "\nWith the given side length... " << endl; cout << endl; cout << "\nSurface area will be: " << setprecision(2) << CubeSA << endl; cout << endl; cout << "\nVolume will be: " << setprecision(2) << CubeV << endl; cout << endl; } { //Cone int ConeH = 0.0; double ConeR = 0.0; double ConeSA = (pi * ConeR * (ConeR + sqrt((ConeH * ConeH) + (ConeR * ConeR)))); double ConeV = (pi * ((ConeR * ConeR) * (ConeH / 3))); if (Cone); cout << "\nIn order to find the surface area and " << endl; cout << "\nvolume we will need a radius and a height. " << endl; cout << endl; cout << "\nPlease input a positive number for the radius: " << endl; cin >> ConeR; cout << endl; cout << "\nPlease input a positive number for the height: " << endl; cin >> ConeH; cout << endl; cout << "\nWith the given side length... " << endl; cout << endl; cout << "\nSurface area will be: " << setprecision(2) << ConeSA << endl; cout << endl; cout << "\nVolume will be: " << setprecision(2) << ConeV << endl; cout << endl; } { //Dodecahedron int DodeEdge = 0.0; double DodeSA = (3 * (sqrt(25 + 10 * sqrt(5) * DodeEdge))); double DodeV = (DodeEdge * ((15 + (7 * sqrt(5)) / 4))); if (Dodecahedron); cout << "\nIn order to find the surface area and " << endl; cout << "\nvolume we will need to have an edge. " << endl; cout << endl; cout << "\nPlease input a positive number for the edge: " << endl; cin >> DodeEdge; string DodeEdge; cout << "\nWith the given edge length... " << endl; cout << endl; cout << "\nSurface area will be: " << setprecision(2) << DodeSA << endl; cout << endl; cout << "\nVolume will be: " << setprecision(2) << DodeV << endl; cout << endl; } { //Cylinder int CylinderH = 0.0; int CylinderR = 0.0; double CylinderSA = ((2 * CylinderR * pi * CylinderH) + (2 * pi * (CylinderR * CylinderR))); double CylinderV = (pi * (CylinderR * CylinderR) * CylinderH); if (Cylinder); cout << "\nIn order to find the surface area and " << endl; cout << "\nvolume we will need to have a radius and a height. " << endl; cout << endl; cout << "\nPlease input a positive number for the radius: " << endl; cin >> CylinderR; cout << endl; cout << "\nPlease input a positive number for the height: " << endl; cin >> CylinderH; cout << endl; cout << "\nWith the given radius and height... " << endl; cout << endl; cout << "\nSurface area will be: " << setprecision(2) << CylinderSA << endl; cout << endl; cout << "\nVolume will be: " << setprecision(2) << CylinderV << endl; cout << endl; } return 0; }
我是编程新手,这是考试相关内容,希望得到错误点提示而非完整代码,非常感谢!
错误点提示
- 选项输入逻辑错误:当前代码要求用户连续输入5个float值(
cin >> A; cin >> B; ...),但实际只需要读取一个代表形状选项的字符(比如A/B/C/D/E),应该用char类型变量接收单个选项输入。 - 条件判断完全失效:所有
if (变量);语句后的分号会让if成为空语句,后续代码不受条件约束会全部执行;且判断的变量初始值都是0,逻辑完全不成立,应该根据用户选择的选项来分支执行对应形状的代码。 - 变量类型与用途不匹配:用
float类型的A、B等存储选项是错误的,选项是字符类型,应该定义char choice;来接收用户输入。 - 计算逻辑提前执行:所有形状的表面积、体积计算都是在用户输入参数前完成的,使用的是变量初始值(比如SphereRadius初始为0),应该在用户输入参数后再进行计算。
- 变量重复定义:Dodecahedron部分中,已经定义过
int DodeEdge,又重复定义string DodeEdge,这会导致编译错误。 - 常量作用域问题:
pi仅在Sphere的代码块内定义,其他形状(如Cone、Cylinder)使用pi会出现未定义的编译错误,应该将pi定义在main函数开头或全局作用域。 - 输出内容错误:Sphere部分输出体积时,错误地输出了
SphereRadius而非计算好的SphereV。 - 代码块无分支约束:每个形状的代码块只是用
{}包裹,但没有对应的条件判断,程序会依次执行所有形状的代码流程,这就是为什么会直接进入球体输入环节的原因。
内容的提问来源于stack exchange,提问作者Stephen Corvino
相关产品推荐
相关产品推荐

