用c++实现做菜单,可以添加菜品

1.主要功能

能够添加菜品

能够点菜

能够查看一点菜品

能够保存菜单

2.代码区域

#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>

using namespace std;

// 定义菜单数据结构,使用 map 类型
map<string, int> menu;

// 定义已点菜品数据结构,使用 vector 类型
vector<string> ordered_dishes;

// 显示当前菜单
void show_menu() {
    cout << "当前菜单:" << endl;
    int i = 0;
    system("cls");
        for (auto it = menu.cbegin(); it != menu.cend(); ++it) {
            
            cout << it->first << ": " << it->second << "元" << endl;
            
        }
    system("pause");
}

// 添加新的菜品到菜单中
void add_dish() {
    string dish_name;
    int price;
    char i=0;
    system("cls");
    cout << "输入y继续,否则退出" << endl;
A:    cout << "请输入新的菜品名称:";
    cin >> dish_name;
    cout << "请输入新的菜品价格(元):";
    cin >> price;
    menu[dish_name] = price;
    cout << "已成功添加新的菜品:" << dish_name << endl;
    cout << "请选择y是继续,其他退出" << endl;
    cin >> i;
    if (i == 'y')
    {
        goto A;
    }
    else
    { }
    
}

// 保存菜单到文件
void save_menu(string file_path) {
    ofstream outfile(file_path);
    if (outfile.is_open()) {
        for (auto it = menu.cbegin(); it != menu.cend(); ++it) {
            outfile << it->first << "," << it->second << endl;
        }
        outfile.close();
        cout << "已成功保存菜单到文件:" << file_path << endl;
    }
    else {
        cout << "无法打开文件:" << file_path << endl;
    }
}

// 从文件加载菜单
void load_menu(string file_path) {
    ifstream infile(file_path);
    if (infile.is_open()) {
        menu.clear();
        string line;
        while (getline(infile, line)) {
            size_t pos = line.find(",");
            if (pos != string::npos) {
                string dish_name = line.substr(0, pos);
                int price = stoi(line.substr(pos + 1));
                menu[dish_name] = price;
            }
        }
        infile.close();
        cout << "已成功加载菜单:" << file_path << endl;
    }
    else {
        cout << "无法打开文件:" << file_path << endl;
    }
}

// 点菜
void order_dish() {
    show_menu();
    string dish_name;
 b:   cout << "请输入要点的菜品名称:";
    cin >> dish_name;
    if (dish_name == "ok")
    {

    }
    else
    {
        if (menu.count(dish_name) > 0) {
            ordered_dishes.push_back(dish_name);
            cout << "已成功点菜:" << dish_name << endl;
        }
        else {
            cout << "菜单中不存在该菜品。" << endl;
        }
        goto b;
    }
}

// 显示已点菜品
void show_ordered_dishes() {
    cout << "已点菜品:" << endl;
    for (const string& dish : ordered_dishes) {
        cout << dish << endl;
    }
    system("pause");
}

int main() {
    string file_path = "menu.txt";

    // 加载菜单
    load_menu(file_path);

    while (true) {
        cout << "请选择操作:" << endl;
        cout << "1. 查看菜单" << endl;
        cout << "2. 添加菜品" << endl;
        cout << "3. 点菜" << endl;
        cout << "4. 查看已点菜品" << endl;
        cout << "5. 保存菜单并退出" << endl;
        string choice;
        cin >> choice;

        if (choice == "1") {
            show_menu();
        }
        else if (choice == "2") {
            add_dish();
        }
        else if (choice == "3") {
            order_dish();
        }
        else if (choice == "4") {
            show_ordered_dishes();
        }
        else if (choice == "5") {
            save_menu(file_path);
            break;
        }
        else {
            cout << "无效选项,请重新输入。" << endl;
        }
    }
    return 0;
}
 

  • 21
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的C++添加菜品函数的实现: ``` #include <iostream> #include <string> #include <vector> using namespace std; // 菜品类 class Dish { public: string name; // 菜品名称 double price; // 菜品价格 string description; // 菜品描述 }; // 餐厅类 class Restaurant { public: string name; // 餐厅名称 string address; // 餐厅地址 string phone; // 餐厅电话 vector<Dish> menu; // 菜单 // 添加菜品 void addDish(string name, double price, string description) { Dish dish; dish.name = name; dish.price = price; dish.description = description; menu.push_back(dish); cout << "菜品添加成功!" << endl; } }; int main() { Restaurant restaurant; restaurant.name = "XXX餐厅"; restaurant.address = "XXX地址"; restaurant.phone = "XXX电话"; // 添加菜品 restaurant.addDish("宫保鸡丁", 28.0, "口感鲜美,麻辣适中"); restaurant.addDish("鱼香肉丝", 25.0, "肉质鲜嫩,口味独特"); restaurant.addDish("回锅肉", 32.0, "肉质松软,香辣可口"); return 0; } ``` 在上述代码中,我们定义了一个Dish类和一个Restaurant类。其中,Dish类表示菜品,包括菜品名称、价格、描述等属性;Restaurant类表示餐厅,包括餐厅名称、地址、电话、菜单等属性。 在Restaurant类中,我们定义了一个addDish方法,用于添加菜品菜单中。该方法将菜品的名称、价格、描述作为参数传入,创建一个Dish对象,并将其添加菜单中。在添加完成后,输出一条添加成功的信息。 在main函数中,我们创建了一个Restaurant对象,并调用addDish方法添加了三个菜品菜单中。 需要注意的是,上述代码只是一个简单的示例,具体的需求和实现方式还需要根据实际情况进行调整。例如,可以添加菜品编号、分类等属性,也可以将菜单保存到文件中,以便于后续的读取和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值