使用 while 循环切换语句
Switch statements with while loops
do{
cout << "Select an option from the Menu: ";
cin >> choice;
// Validate the menu selection
while ((choice < 1) || (choice > 3)){
cout << "Incorrect input!, please enter an option from 1 to 3."<<endl;
cout<<"Enter your choice: ";
cin >> choice;
}
// Processing the users choice
if (choice != 3){
// Compute conversions
switch (choice){
case 1:
cout<<""<<endl;
cout<<"You have selected to convert Fijian Dollars to Vanuatuan Vatu."<<endl;
cout<<"Enter the amount you wish to convert: ";
cin >> amount;
conversion = amount * FJD_to_Vatu_Rate;
break;
case 2:
cout<<""<<endl;
cout<<"You have selected to convert Fijian Dollars to Samoan Tala."<<endl;
cout<<"Enter the amount you wish to convert: ";
cin >> amount;
conversion = amount * FJD_to_Tala_Rate;
break;
case 3:
cout<<"Here the history will be shown"<<endl;
cout<<""<<endl;
cout<<"Do You want to perform another conversion? (Y/N) ";
cin >> repeat;
if (repeat == 'Y'){
(This is what i want to know)
}
cout<<"Thank you for using this program, goodbye!"<<endl;
return 0;
}
// Display the monthly charges
cout << fixed << showpoint << setprecision(2);
cout << "The converted amount is: " << conversion << endl;
cout <<""<<endl;
}
} while (choice != 3);
cout<<"Thank you for using this program, goodbye!"<<endl;
return 0;
}
以上是我的货币转换器代码的一部分,任何人都可以告诉我是否在它说"这是我想知道的"的部分中做什么,以便程序要求用户输入一个选项,以便它可以处理它并使用选择使用其他情况之一进行计算。
谢谢
在switch
之前删除if
语句,因为在这种情况下case 3:
永远不会执行。
do{
cout << "Select an option from the Menu: ";
cin >> choice;
// Validate the menu selection
while ((choice < 1) || (choice > 3)){
cout << "Incorrect input!, please enter an option from 1 to 3."<<endl;
cout<<"Enter your choice: ";
cin >> choice;
}
// Processing the users choice
// Compute conversions
switch (choice){
case 1:
cout<<""<<endl;
cout<<"You have selected to convert Fijian Dollars to Vanuatuan Vatu."<<endl;
cout<<"Enter the amount you wish to convert: ";
cin >> amount;
conversion = amount * FJD_to_Vatu_Rate;
break;
case 2:
cout<<""<<endl;
cout<<"You have selected to convert Fijian Dollars to Samoan Tala."<<endl;
cout<<"Enter the amount you wish to convert: ";
cin >> amount;
conversion = amount * FJD_to_Tala_Rate;
break;
case 3:
cout<<"Here the history will be shown"<<endl;
cout<<""<<endl;
cout<<"Do You want to perform another conversion? (Y/N) ";
cin >> repeat;
if (repeat == 'Y'){
(This is what i want to know)
}
cout<<"Thank you for using this program, goodbye!"<<endl;
return 0;
// Display the monthly charges
cout << fixed << showpoint << setprecision(2);
cout << "The converted amount is: " << conversion << endl;
cout <<""<<endl;
}
} while (choice != 3);
cout<<"Thank you for using this program, goodbye!"<<endl;
return 0;
}
要重新启动 do-while 循环,请使用 continue
。从逻辑上讲,您应该首先打印历史记录,然后再进行问题\检查。
相关文章:
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 如果 else 语句不断循环
- do while 循环中的 if 语句以 yes 或 no 结尾
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- 如果语句不会在 do - while 循环中运行
- 有没有办法在循环中的条件至少满足一次时运行语句?
- 什么可以用来替换代码中的循环和 if 语句?
- C++ 访问 if 语句(如果它们在 for 循环中)
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 如何仅在父循环的所有迭代都为真时才执行嵌套的 if 语句?
- 有没有办法制作一个 c++ 宏(或类似的东西),它使用户语句被 while 循环的预定义函数包围?
- 为什么这个 IF 语句在循环内不打印
- 当我的 if 语句在字符串中找到空格时,在无限循环中切换
- 我的 if 语句打印在我的 while 循环中的每个 if 语句上,无论它是否为假
- C++-循环中的If语句工作不正常
- C++切换语句循环回菜单
- 如果 else 语句循环或重复,直到 true
- C++:使用嵌套的 if/else 语句循环进行故障排除
- 有没有办法使c++ Switch语句循环回到第一种情况?
- 为什么这个'if'语句循环?