首页 > 解决方案 > 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

标签: c++

解决方案


首先,Y未初始化,因此您可能想要执行类似char Y = 'Y'.

然后在您的switch陈述中,您不涵盖条件(vegan == Y), (vegetarian == Y),(glutenFree == Y)不满足的情况。所以只有在用户回答的情况下你才会得到输出Y。如果他选择,则不会显示任何内容N

此外,如果您想存储chars,我认为没有理由将它们存储为ints。你应该做vegan,vegetarianglutenFree chars。


推荐阅读