保持程序处于主循环
Keeping the program on the main loop
我有一个简单的程序,它可以进行多次转换,但每次操作后都会脱离循环。如何在每个函数完成执行后调整程序以返回主循环。
#include <iostream>
#define TRUE 1
using namespace std;
int main(){
char *str = new char[25];
cout<< "What do you want to convert?" <<endl;
cin>> str;
while (TRUE){
if (strcasecmp(str, "TEMPERATURE") == 0)
{
convert_temp(str);
}
if (strcasecmp(str, "WEIGHT") == 0)
{
convert_weight(str);
}
if (strcasecmp(str, "DISTANCE") == 0)
{
convert_distance(str);
}
}
return EXIT_SUCCESS;
}
void convert_temp(char* str){
cout<< "Press 1 for C to F | Press 2 for F to C" <<endl;
int temp = 0;
cin >> temp;
if(temp == 1){
int c, f = 0;
cout<< "Enter the value" <<endl;
cin >> c;
f = (9/5)*c - 32;
cout<< "The temperature is " << f << " degree F"<< endl;
}else if (temp == 2){
int c, f = 0;
cout << "Enter the value"<<endl;
cin >> f;
c = (f-32)*(5/9);
cout << "The temperature is " << c << " degree C"<<endl;
}
break;
}
void convert_weight(char* str){
cout<< "Press 1 for POUNDS to KGS | Press 2 for KGS to POUNDS" <<endl;
int temp = 0;
cin >> temp;
if(temp == 1){
int k, p = 0;
cout<< "Enter the value" <<endl;
cin >> p;
k = p/2.2046;
cout<< "Your weight is " << k << " Kgs"<< endl;
}else if (temp == 2){
int k, p = 0;
cout << "Enter the value"<<endl;
cin >> k;
p = k*2.2046;
cout << "Your weight is " << p << " Pounds"<<endl;
}
break;
}
void convert_distance(char* str){
cout<< "Press 1 for KM to MILES | Press 2 for MILES to KM" <<endl;
int temp = 0;
cin >> temp;
if(temp == 1){
int k, m = 0;
cout<< "Enter the value" <<endl;
cin >> k;
m = 32*k;
cout<< "The distance is " << k << " Miles"<< endl;
}else if (temp == 2){
int k, m = 0;
cout << "Enter the value"<<endl;
cin >> m;
k = 10*m;
cout << "The distance is " << k << " Kms"<<endl;
}
break;
}
因此,我的问题是如何用对主循环的调用来替换break语句。我不想按顺序运行函数,将所有函数插入主循环。
您的代码应该更像这样:
#include <iostream>
#include <string>
void convert_temp();
// Other prototypes elided
int main() {
std::string str;
for(;;) { // I use this in my coding standard to mean 'forever' instead of while(true)
std::cout << "What do you want to convert?" << std::endl;
std::cin >> str;
if (str == "TEMPERATURE") {
convert_temp();
}
if (str == "WEIGHT") {
convert_weight();
}
if (str == "DISTANCE") {
convert_distance();
}
if (str == "EXIT") {
break;
}
}
return 0;
}
void convert_temp() {
std::cout << "Press 1 for C to F | Press 2 for F to C" << std::endl;
int temp = 0;
std::cin >> temp;
std::cout << "Enter the value" << std::endl;
int c, f = 0;
if (temp == 1) {
cin >> c;
f = (9/5)*c - 32;
std::cout << "The temperature is " << f << " degree F" << std::endl;
} else if (temp == 2) {
std::cin >> f;
c = (f-32)*(5/9);
std::cout << "The temperature is " << c << " degree C" << std::endl;
}
}
// Other function definitions elided
注意,作为C++,我已经将std::string
用于主选择的输入。
main()
函数现在循环询问"您想转换什么?"每次。
该参数不再传递给任何函数,因为它们知道自己在做什么,不需要使用该值。
示例convert_temp()
函数已被稍微简化,并且break;
已被移除,因为这在这样的普通函数中没有任何作用。
还要注意输入"EXIT"是如何退出程序的。
您可以使用exit(0);
退出整个程序。(无论如何,中断循环都会导致进程关闭)
如果您希望继续主循环,则不需要在代码中使用break语句。中断表示退出当前控制指令(对于/开关)。您可以用continue;
替换break语句,但在这种情况下,这相当于没有continue语句。
相关文章:
- 如何在不停滞主循环的情况下实现对话系统?
- 如何在 SFML C++游戏中将资产加载与主循环分开
- 在主循环外部多个文件上定义全局变量
- 如何使特定的部分功能运行并返回主循环
- 即使我将班级声明置于主循环之上,也没有宣布班级成员
- 为什么这个逻辑在我的主循环上有效,但在方法上不起作用
- glGenVertexArrays在主循环中不创建唯一的VAO,但在其他情况下有效
- QT(4.8.2)GUI-自己的主循环,而不是qapplication :: exec()
- C++主循环抽象
- 当主循环正在侦听消息时,如何在特定时间退出程序
- 注入的DLL主循环使进程崩溃
- "继续"使用标志作用于哪个循环?
- 为什么malloc_trim()只适用于主竞技场
- 删除元素并将其添加到主循环内的矢量
- 我正在做减法游戏(NIM)程序在我的一个练习中,它要求我在主循环周围进行另一个循环
- 保持程序处于主循环
- Visual Studio 2008 C++ - 远程调试 - "Could not load symbols"仅适用于主项目
- 如何在不关闭服务器套接字的情况下在C++客户端的主循环中接收数据?
- 什么是主循环抽象概念
- win32主循环间隔问题c++