我正在制作一个食谱分发程序,它将根据以下因素打印食谱:
- 食物的辣度
- 烹饪所需时间
以后可能会添加更多内容。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input = "";
string recipe1 = "A mild recipe that takes 10 mins";
string recipe2 = "A mild recipe that takes 20 mins";
string recipe3 = "A medium recipe that takes 10 mins";
string recipe4 = "a mild recipe that takes 20 mins";
cout << "Hello to the recipe dispenser 2000" << endl << "I will now begin with some questions to get the perfect recipe for you" << endl << "Do you like your food mild, medium, or hot?" << endl;
getline (cin, input);
if(input == "mild")
cout << "Would you like a recipe that takes 10 or 20 mins?" << endl;
getline (cin, input);
if(input == "10" or input == "10 mins")
cout << recipe1 << endl;
}
然而,我现在的代码似乎相当低效,因为我必须编写一共6个if语句才能完成代码。
有没有办法缩短这个过程呢?
例如,通过为每个食谱添加一些标签,如[10, mild]
到recipe1
,然后代码将基于标签输出响应。
欢迎提出任何想法。
(input == "10") or ("10 mins")
。你的意思是(input == "10" or input == "10 mins")
。 - adnan_e#define or ||
或者使用一个具有特定扩展的编译器? - JVApenmap
来提高可读性和可扩展性。 - adnan_e