需要有关是或否问题的帮助
Need assistance with yes-or-no questions
我正在做一个练习,我必须编写一个程序,根据用户对一系列是或否问题的回答来建议订阅计划。
练习基于此图表:https://imgur.com/a/AEMw5Ln
我的主要问题是:如何让 C++ 获得 'tally up' 结果并制定计划?
这是我的代码,您可以看到我到目前为止所做的事情...
// 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 语句中使用它。我会这样做,因为这意味着用户需要做更少的输入,可以在一行中看到所有选项,因此不需要考虑其他未来可能的选项,而且它更容易编码。
我正在做一个练习,我必须编写一个程序,根据用户对一系列是或否问题的回答来建议订阅计划。
练习基于此图表:https://imgur.com/a/AEMw5Ln
我的主要问题是:如何让 C++ 获得 'tally up' 结果并制定计划?
这是我的代码,您可以看到我到目前为止所做的事情...
// 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 语句中使用它。我会这样做,因为这意味着用户需要做更少的输入,可以在一行中看到所有选项,因此不需要考虑其他未来可能的选项,而且它更容易编码。