c++ - Display restaurant options based on dietary restrictions
问题描述
This is a fairly basic question but for some reason, I can't put it together. What I am trying to do is have a user input Y/N about dietary restrictions. For example:
Is anyone in your party a vegetarian? Y Is anyone in your party a vegan? N Is anyone in your party gluten-free? Y
Here are your restaurant choices: Main Street Pizza Company Corner Café The Chef’s Kitchen
I tried the following: (I'm still pretty new at C++ so please forgive this nooby question.
char Y;
int vegan;
int vegetarian;
int glutenFree;
int restrictions;
cout << "Is anyone in your party vegan? (Y/N)" << endl;
cin >> vegan;
cout << "Is anyone in your party vegetarian? (Y/N)" << endl;
cin >> vegetarian;
cout << "Is anyone in your party gluten free? (Y/N)" << endl;
cin >> glutenFree;
switch (restrictions) {
case 'a':
if(vegan == Y)
cout << "The following resturants offer vegan options" << endl << "Corner Café, The Chef’s Kitchen" << endl;
break;
case 'b':
cout << "Is anyone in your party vegetarian? (Y/N)" << endl;
if(vegetarian == Y)
cout << "The following resturants offer vegetarian options" << endl << "Main Street Pizza Company, Corner Café, Mama’s Fine Italian, The Chef’s Kitchen" << endl;
break;
case 'c':
cout << "Is anyone in your party gluten free? (Y/N)" << endl;
if(glutenFree == Y)
cout << "The following resturants offer gluten-free options" << endl << "Main Street Pizza Company,The Chef’s Kitchen, Corner Café" << endl;
break;
Thank you in advance
解决方案
首先,Y
未初始化,因此您可能想要执行类似char Y = 'Y'
.
然后在您的switch
陈述中,您不涵盖条件(vegan == Y)
, (vegetarian == Y)
,(glutenFree == Y)
不满足的情况。所以只有在用户回答的情况下你才会得到输出Y
。如果他选择,则不会显示任何内容N
。
此外,如果您想存储char
s,我认为没有理由将它们存储为int
s。你应该做vegan
,vegetarian
和glutenFree
char
s。
推荐阅读
- node.js - 静态文件 express/react 的相对路径
- excel - 将 '=IF(ISNUMBER(SEARCH')' 答案转换为数字而不是文本(使用自动求和)
- javascript - 检查笑话和酶中的按钮文本
- django - 使用 'get' 表单将值传递给 Django 中的下一个视图
- neo4j - Neo4j - CASE DETACH DELETE 错误:输入“S”无效
- php - 我需要知道如何使用给定命令使用 api 键参数调用 curl
- typescript - “参数”类型上不存在属性“id”
- html - 在子 div 上创建叠加层
- angular - 如何禁用动画
- python - Pycharm终端在输入时显示输入