首页 > 解决方案 > 在是或否问题上需要帮助

问题描述

我正在做一个练习,我必须编写一个程序,该程序将根据用户对一系列是或否问题的回答来建议订阅计划。

该练习基于此图表: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 语句吗?任何帮助都非常受欢迎。

标签: c++if-statementproject-planning

解决方案


switch 语句可能会使最后的代码看起来更好,并且易于实现,但这只是个人偏好,对程序本身几乎没有好处。如注释所述,还将 if 语句内容从赋值运算符 (=) 更改为比较 (==)。要清理您的 if else 混乱,您应该使用:

bool_name = answer == desired_input;

例如:

freeTheme = answer == 'y';

否则,您可以简单地询问他们想要的图片中的哪个选项(免费、博客等),然后在 switch 语句中使用它。我会这样做,因为这意味着用户需要做的输入更少,可以在一行上看到所有选项,因此不需要考虑未来的其他选项可能是什么,而且它更容易编码。


推荐阅读