我有一个简单的程序可以进行多次转换,但在每次操作后都会跳出循环。在每个函数完成执行后,如何调整程序返回主循环。
#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;
}
因此,我的问题是如何通过调用main循环来替换break语句。我不希望在将所有这些函数插入主循环的序列中运行函数。
答案 0 :(得分:2)
您的代码应该更像这样:
#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'如何退出程序。
答案 1 :(得分:0)
您可以使用exit(0);
退出整个程序。 (无论如何,打破循环将导致关闭过程)
答案 2 :(得分:0)
如果您希望继续使用主循环,则代码中不需要break语句。 Break means exit from the current control directive(for / switch)。您可以使用continue;
替换break语句,但在这种情况下,这相当于没有continue语句。