c++ - 在是或否问题上需要帮助
问题描述
我正在做一个练习,我必须编写一个程序,该程序将根据用户对一系列是或否问题的回答来建议订阅计划。
该练习基于此图表:https ://imgur.com/a/AEMw5Ln
我的主要问题是:我怎样才能让 C++ “汇总”结果并制定计划?
这是我的代码,所以你可以看到我到目前为止所做的......
// Firstly, Welcome the client!
cout << " Welcome to the Wordpress subsciption sugesstion Tool!" << endl;
cout << " In order to find the right plan for you, we'll need to ask you a series of questions!" << endl;
cout << " Answer the following questions using 'y' or 'n' keys when prompted." << endl;
// secondly, Present the series of questions and prompt the user for input.
cout << 1.) Would you like to utilize free themes? (y/n)" << endl;
cin >> answer;
if(answer = 'y')
{
freeTheme = true;
}
else
{
freeTheme = false;
}
cout << "\t\n 2.) Would you like to customize your themes? (y/n)" << endl;
cin >> answer;
if(answer = 'y')
{
customDesign = true;
}
else
{
customDesign = false;
}
cout << "\t\n 3.) Will you be needing Search Engine Optimization? (y/n)" << endl;
cin >> answer;
if(answer = 'y')
{
seoTools = true;
}
else
{
seoTools = false;
}
cout << "\t\n 4.) Is live support important for you? (y/n)" << endl;
cin >> answer;
if(answer = 'y')
{
liveSupport = true;
}
else
{
liveSupport = false;
}
cout << "\t\n 5.) How much data will you need to host? 3GB, 6GB, 13GB, or unlimited(999GB)?" << endl;
cin >> storageCapacity;
if(storageCapacity = 3)
{
costPerMonth = 0.00;
}
else if (storageCapacity = 6)
{
costPerMonth = 5.00;
}
else if (storageCapacity = 13)
{
costPerMonth = 8.00;
}
else if (storageCapacity = 999)
{
costPerMonth = 25.00;
}
cout << 6.) How many years are you interest in hosting for?" << endl;
cin >> years;
它可能看起来有点混乱,但我正在努力解决它。你怎么看?我在正确的轨道上吗?我缺少哪些语句/运算符?我应该尝试合并 switch 语句吗?任何帮助都非常受欢迎。
解决方案
switch 语句可能会使最后的代码看起来更好,并且易于实现,但这只是个人偏好,对程序本身几乎没有好处。如注释所述,还将 if 语句内容从赋值运算符 (=) 更改为比较 (==)。要清理您的 if else 混乱,您应该使用:
bool_name = answer == desired_input;
例如:
freeTheme = answer == 'y';
否则,您可以简单地询问他们想要的图片中的哪个选项(免费、博客等),然后在 switch 语句中使用它。我会这样做,因为这意味着用户需要做的输入更少,可以在一行上看到所有选项,因此不需要考虑未来的其他选项可能是什么,而且它更容易编码。
推荐阅读
- cmake - 如何让 CMake 将我的测试程序链接到 netCDF 库?
- nuxt.js - 如何使用 vee-validate 本地化 Nuxt 的表单验证错误消息
- android - angular js 2.17 版的所有 mobiscroll 组件在 Android 10 上停止工作
- python - 如何使用 tensorflow 进行文本分类?
- php - 使用水印 codeigniter 加载字体时出错
- java - PostgreSQL 上的 FlywaySqlException:无法创建 PoolableConnectionFactory(错误:由于用户请求而取消语句
- javascript - Javascript addEventListener 在 Laravel 中无法与 querySelector 一起使用
- tsql - 获取正在运行的工作站客户端应用程序的 FQDN
- perl - 更高版本的 Perl 上的 JSON::encode_json 问题
- c# - Automapper ForMember MapFrom 似乎不起作用