C++程序不会结束
C++ the program won't end
你能说出错误是什么吗?它编译并运行良好,但程序不会结束。
我使用的是dev c++。代码为:
#include <iostream>
using namespace std;
main()
{
char num;
again:
int usr;
float area;
cout << "nEnter 1 to calculate area of rectanglen";
cout << "Enter 2 to calculate area of trapezoidn";
cout << "nEnter your choice: ";
cin >> usr;
if (usr == 1)
{
double width, length;
cout << "Enter the width of rectangle: ";
cin >> width;
cout << "Enter the length of rectangle: ";
cin >> length;
area = length * width;
cout << "The area of rectangle is: " << area;
}
if (usr == 2) {
double base1, base2, height;
cout << "Enter the base 1 of trapezoid: ";
cin >> base1;
cout << "Enter the base 2 of trapezoid: ";
cin >> base2;
cout << "Enter the height of trapezoid: ";
cin >> height;
area = (((base1 + base2) / 2) * height);
cout << "The area of trapezoid is: " << area;
}
cout << "nndo you want to do another calculation?";
cin >> num;
{
goto again;
}
if (num == 'y')
{
goto again;
}
if (num == 'n') {
exit(0);
}
}
永远不要使用goto
,除非你有充分的理由(你没有)。
cin >> num;{
goto again;
}
我不知道你为什么写那个,但这就是问题所在。如果你用正确的格式写它,它会看起来像这个
cin >> num;
{
goto again;
}
括号{}
只是更改作用域(对变量等有用),但不做任何其他操作。goto again;
仍然会被执行,所以您会陷入一个无休止的循环。
之后的两个条件都很好,所以只需删除{ goto again; }
就可以"解决"您的问题。
删除代码中的第44行
您的代码
cin >> num;{
goto again;
}
解决方案删除
再次转到
cin >> num;{
}
这将解决您的问题
相关文章:
- 试图对缓存进行跨步测试,但程序并没有结束
- 为什么我的两个 cin 语句没有在程序结束时运行?
- C ++尝试并捕获未结束的程序
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 全局向量导致 C++ 程序结束时出现段错误
- 程序显示以退出代码 0; 结束
- C++程序从主程序开始执行并在主程序结束?
- 如何立即停止/结束程序?
- 在不释放所有动态分配的资源的情况下结束程序是否有风险
- 线程池如何结束程序
- 客户端发送消息,但服务器在客户端结束程序之前不接收消息
- 如果用户想要输入 x 数量的数字,我如何设法跟踪它然后结束程序?
- 如何从另一个函数中调用的函数结束程序 (C++)
- 如果用户输入 -1 作为动态数组中的数字,如何结束程序
- 引发异常并直接结束程序的自定义异常处理
- MPI在某个进程找到解决方案时使用广播结束程序
- 退出(0)未结束程序
- 通过函数结束程序
- 错误后如何结束C++程序
- 如何在不结束c++程序的情况下结束ncurses ?