【问题标题】:Trouble with local restaurant automated breakfast billing (c++)当地餐厅自动早餐计费的问题(c++)
【发布时间】:2014-03-17 20:28:07
【问题描述】:

我是 C++ 新手,但我在分配作业时遇到了麻烦。我在编译时需要帮助解决错误,以及如何计算税款和应付总额。这是任务...

编写一个程序来帮助当地餐厅自动化其早餐计费系统。该程序应执行以下操作: 一个。向顾客展示餐厅提供的不同早餐项目。 湾。允许客户从菜单中选择多个项目 C。计算并打印账单 假设餐厅提供以下早餐项目(每个项目的价格显示在项目的右侧):

普通鸡蛋 $1.45 培根和鸡蛋 $2.45 松饼 $0.99 法式吐司 $1.99 水果篮 $2.44 麦片 $0.69 咖啡 $0.50 茶 $0.75

首先,定义一个结构体 menuItemType,它有两个组件:string 类型的 menuItem 和 double 类型的 menuPrice。 然后使用结构 menuItemType 的数组,称为 menuList。您的程序必须包含以下功能: • 函数getData:此函数将数据加载到数组菜单列表中。 • 功能showMenu:此功能显示餐厅提供的不同项目,并告诉用户如何选择项目。 • 函数 printCheck:此函数计算并打印支票。 (请注意,账单金额应包含 5% 的税)

样本输出:

欢迎来到强尼餐厅 培根和鸡蛋 $2.45 松饼 $0.99 咖啡 $0.50 税 0.20 美元 应付金额 $4.14

这是我目前的代码

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct menuItemType
{
    string menuItem;
    double menuPrice;
};

void getData(menuItemType menuList[8]);
void showMenu(menuItemType menuList[8], menuItemType orderList[], int orderCounter, int selection);
void printCheck(menuItemType menuList[8], int orderCounter, const double TAX);

const int MAX = 8;
const double TAX = 1.05;

int main()
{
    int orderCounter = 0;
    int selection = ' ';

    menuItemType menuList[8];
    menuItemType orderList[MAX];

    getData(menuList);
    showMenu(menuList, orderList, orderCounter, selection);
    printCheck(orderList, orderCounter, TAX);

return 0;
}

void getData(menuItemType menuList[8])
{

menuList[0].menuItem = "Plain Egg";
menuList[0].menuPrice = 1.45;
menuList[1].menuItem = "Bacon and Egg";
menuList[1].menuPrice = 2.45;
menuList[2].menuItem = "Muffin";
menuList[2].menuPrice = 0.99;
menuList[3].menuItem = "French Toast";
menuList[3].menuPrice = 1.99;
menuList[4].menuItem = "Fruit Basket";
menuList[4].menuPrice = 2.49;
menuList[5].menuItem = "Cereal";
menuList[5].menuPrice = 0.69;
menuList[6].menuItem = "Coffee";
menuList[6].menuPrice = 0.50;
menuList[7].menuItem = "Tea";
menuList[7].menuPrice = 0.75;

}

void showMenu(menuItemType menuList[8], menuItemType orderList[], int orderCounter, int selection)
{
cout << "1 - Plain Egg" << setw(14) << "$1.45" << endl;
cout << "2 - Bacon and Egg" << setw(10) << "$2.45" << endl;
cout << "3 - Muffin" << setw(17) << "$0.99" << endl;
cout << "4 - French Toast" << setw(11) << "$1.99" << endl;
cout << "5 - Fruit Basket" << setw(11) << "$2.49" << endl;
cout << "6 - Cereal" << setw(17) << "$0.69" << endl;
cout << "7 - Coffee" << setw(17) << "$0.50" << endl;
cout << "8 - Tea" << setw(21) << "$0.75\n" << endl;
}

do{
    cout << "Enter the number of your selection until you have completed your order." << endl;
    cout << "Enter 9 when you have finished." << endl;
    cin >> selection;

    Switch(selection)
        {
            case 9:
            break;
            case 1:
            cout << menuList[0].menuItem << setw(14) << "$1.45";
            break;
            case 2:
            cout << menuList[1].menuItem << setw(10) << "$2.45";
            break;
            case 3:
            cout << menuList[2].menuItem << setw(17) << "$0.99";
            break;
            case 4:
            cout << menuList[3].menuItem << setw(11) << "$1.99";
            break;
            case 5:
            cout << menuList[4].menuItem << setw(11) << "$2.49";
            break;
            case 6:
            cout << menuList[5].menuItem << setw(17) << "$0.69";
            break;
            case 7:
            cout << menuList[6].menuItem << setw(17) << "$0.50";
            break;
            case 8:
            cout << menuList[7].menuItem << setw(20) << "$0.75";
            break;
            default:
            cout << "Invalid Selection! Selections must be between 1 and 9\n";
    }
}
{
while (selection !=9);
    if((selection >=0) && (selection <= MAX))
    {
        orderList[orderCounter].menuItem = menuList[selection-1].menuItem;
        orderList[orderCounter].menuPrice = menuList[selection-1.menuPrice;
        orderCounter++;
    }
    else if (selection == 9)
        cout << "Exit" << endl;
    else
        cout << "Invalid Entry" << endl;
    }
}
void printCheck(menuItemType orderList[], int orderCounter, const double TAX)
{
    cout << "Welcome to the Breakfast Place!"<< endl;

    for (int corderCounter = 0; orderCounter < MAX; orderCounter++)
        cout << orderList[orderCounter].menuItem << " "
        << orderList[orderCounter].menuPrice<< endl;
        cout << "Tax" << setw(10) << endl;
        cout << "Amount Due" << setw(10) << endl;

}

这是我尝试调试时的错误

>  BreakfastBilling.cpp
1>breakfastbilling.cpp(69): error C2059: syntax error : 'do'

1>breakfastbilling.cpp(69): error C2143: syntax error : missing ';' before '{'

1>breakfastbilling.cpp(69): error C2447: '{' : missing function header (old-style formal list?)

1>breakfastbilling.cpp(106): error C2447: '{' : missing function header (old-style formal list?)

1>breakfastbilling.cpp(111): error C2059: syntax error : 'bad suffix on number'

1>breakfastbilling.cpp(119): error C2059: syntax error : '}'

1>breakfastbilling.cpp(119): error C2143: syntax error : missing ';' before '}'

1>breakfastbilling.cpp(119): error C2059: syntax error : '}'

1>breakfastbilling.cpp(121): error C2143: syntax error : missing ';' before '{'

1>breakfastbilling.cpp(121): error C2447: '{' : missing function header (old-style formal list?)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

【问题讨论】:

  • 看起来你有一个语法错误,更具体地说,你有一个方法之外的do 语句。适当的缩进应该让您了解问题所在。
  • 哪里是放置 do 语句的最佳位置?
  • 这里有很多很多的硬编码。如果鸡蛋的价格上涨,那么您将不得不重新编译您的程序。
  • -1 我投票结束,因为“过于宽泛”,因为代码中有太多设计缺陷和错误需要修复。
  • 问题是,在喝茶的 cout 之后,你有一个右花括号,它与 ShowMenu() 函数体的左花括号相对应。这是您在此源代码中遇到的几个大括号放置错误之一。

标签: c++


【解决方案1】:

您在 main 中初始化了 orderCounter,但您没有在任何函数中返回它,所以最终它仍然是 0。数组也是如此。 c++ 中的函数处理传递数据的副本,除非参数通过引用或指针传递。在您的情况下,您可能希望对 orderCounter 和结构数组使用全局声明。这不是最好的解决方案,但它有效。或者,如果您想坚持当前的代码,请阅读一些有关函数和传递参数的信息。 http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

【讨论】:

    【解决方案2】:

    现在终于成功了.....

    它有错误,例如 -
    1。尝试通过赋值运算符复制字符串。您应该为此使用strcpy()
    2。在本地声明变量并在全局范围内使用它们。
    3。有打字错误。
    4。该代码不包含最后要打印的净金额的声明。
    5。从未使用过 TAX,然后我最后在显示最终结果的地方使用了它。
    6。 while 和 switch 在函数/方法之外声明。

    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <string>
    
    using namespace::std;    
    
    struct menuItemType
    {
        char menuItem[20];
        double menuPrice;
    };
    
    void getData(menuItemType menuList[8]);
    void showMenu(menuItemType menuList[8], menuItemType orderList[], int orderCounter, int selection);
    void printCheck(menuItemType menuList[8], int orderCounter, const double TAX);
    
    const int MAX = 8;
    const double TAX = 1.05;
    
    menuItemType menuList[8];
    menuItemType orderList[MAX];
    
    int main()
    {
        int orderCounter = 0;
        int selection = ' ';
    
    
    
        getData(menuList);
        showMenu(menuList, orderList, orderCounter, selection);
        printCheck(orderList, orderCounter, TAX);
    
    return 0;
    }
    
    void getData(menuItemType menuList[8])
    {
    
    strcpy(menuList[0].menuItem,"Plain Egg");
    menuList[0].menuPrice = 1.45;
    strcpy(menuList[1].menuItem,"Bacon and Egg");
    menuList[1].menuPrice = 2.45;
    strcpy(menuList[2].menuItem,"Muffin");
    menuList[2].menuPrice = 0.99;
    strcpy(menuList[3].menuItem,"French Toast");
    menuList[3].menuPrice = 1.99;
    strcpy(menuList[4].menuItem,"Fruit Basket");
    menuList[4].menuPrice = 2.49;
    strcpy(menuList[5].menuItem,"Cereal");
    menuList[5].menuPrice = 0.69;
    strcpy(menuList[6].menuItem,"Coffee");
    menuList[6].menuPrice = 0.50;
    strcpy(menuList[7].menuItem,"Tea");
    menuList[7].menuPrice = 0.75;
    
    }
    
    void showMenu(menuItemType menuList[8], menuItemType orderList[], int orderCounter, int selection)
    {
    cout << "1 - Plain Egg" << setw(14) << "$1.45" << endl;
    cout << "2 - Bacon and Egg" << setw(10) << "$2.45" << endl;
    cout << "3 - Muffin" << setw(17) << "$0.99" << endl;
    cout << "4 - French Toast" << setw(11) << "$1.99" << endl;
    cout << "5 - Fruit Basket" << setw(11) << "$2.49" << endl;
    cout << "6 - Cereal" << setw(17) << "$0.69" << endl;
    cout << "7 - Coffee" << setw(17) << "$0.50" << endl;
    cout << "8 - Tea" << setw(21) << "$0.75\n" << endl;
    do{
        cout << "Enter the number of your selection until you have completed your order." << endl;
        cout << "Enter 9 when you have finished." << endl;
        cin >> selection;
    
        switch(selection)
        {
            case 9:
            break;
            case 1:
            cout << menuList[0].menuItem << setw(14) << "$1.45";
            break;
            case 2:
            cout << menuList[1].menuItem << setw(10) << "$2.45";
            break;
            case 3:
            cout << menuList[2].menuItem << setw(17) << "$0.99";
            break;
            case 4:
            cout << menuList[3].menuItem << setw(11) << "$1.99";
            break;
            case 5:
            cout << menuList[4].menuItem << setw(11) << "$2.49";
            break;
            case 6:
            cout << menuList[5].menuItem << setw(17) << "$0.69";
            break;
            case 7:
            cout << menuList[6].menuItem << setw(17) << "$0.50";
            break;
            case 8:
            cout << menuList[7].menuItem << setw(20) << "$0.75";
            break;
            default:
            cout << "Invalid Selection! Selections must be between 1 and 9\n";
        }
    }while (selection !=9);
        if((selection >=0) && (selection <= MAX))
        {
        strcpy(orderList[orderCounter].menuItem, menuList[selection-1].menuItem);
        orderList[orderCounter].menuPrice = menuList[selection-1].menuPrice;
        orderCounter++;
        }
        else if (selection == 9)
        cout << "Exit" << endl;
        else
        cout << "Invalid Entry" << endl;
    
    }
    
    
    void printCheck(menuItemType orderList[], int orderCounter, const double TAX)
    {
        cout << "Welcome to the Breakfast Place!"<< endl;
    
        for (orderCounter = 0; orderCounter < MAX; orderCounter++)
        cout << orderList[orderCounter].menuItem << " "<< orderList[orderCounter].menuPrice<< endl;
        cout << "Tax" << setw(10) << TAX<<endl;
        cout << "Amount Due" << setw(10) << endl;
    
    }
    

    上面的代码已经改正了,但是逻辑错误还是存在的,你应该看看他们,因为你的代码中没有提到你的逻辑。 amount 仍未显示,因为您尚未为此添加代码!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多