如何让C++'tally up'结果并制定计划?

How can I get C++ to 'tally up' the results and produce a plan?

本文关键字:定计划 结果 up tally C++      更新时间:2023-10-16

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

该练习基于以下图表: https://i.stack.imgur.com/W2fL0.jpg

这是我的代码,所以你可以看到我到目前为止做了什么......

// 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 << "tn 2.) Would you like to customize your themes? (y/n)" << endl;
cin >> answer;
if(answer = 'y')
{
customDesign = true;
}
else
{
customDesign = false;
}

cout << "tn 3.) Will you be needing Search Engine Optimization? (y/n)" << endl;
cin >> answer;
if(answer = 'y')
{
seoTools = true;
}
else
{
seoTools = false;
}

cout << "tn 4.) Is live support important for you? (y/n)" << endl;
cin >> answer;
if(answer = 'y')
{
liveSupport = true;
}
else
{
liveSupport = false;
}
cout << "tn 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 语句可能会使末尾具有存储容量的代码看起来更好,并且易于实现,但这只是偏好问题,对程序本身几乎没有好处。还要将 if 语句内容从赋值运算符 (=( 更改为比较 (==(,如注释所述。要清理您的 if else 混乱,您应该使用:

bool_name = answer == desired_input;

例如:

freeTheme = answer == 'y';

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