繁体   English   中英

为什么我的自动售货机程序不能正常工作?

[英]Why doesn't my vending machine program work correctly?

程序应该从询问是否重新进货或继续当前库存开始。 案例 1 (restock) 工作得很好,但是第二个案例,继续之前的库存,如果任何产品归零,总是返回零。

在文本文件中,我有:

  1. 牛奶:10
  2. 鸡蛋:2
  3. 水:7
  4. 墨西哥卷饼:10
  5. 面包:12
  6. 出口

我该如何解决?

    #include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
#include<sstream>

using namespace std;

string productName[5] = { "Milk", "Eggs", "Water", "Burrito", "Bread" };
//int productAmount[5] = { 5,12,10,4,7};
int productAmount[5];
int productPick;
int defaultPick;
int productBuy;
fstream productFile; //we create file 

void loadFromFile()
{
    productFile.open("productsfile.txt", ios::in);

    if (productFile.good() == false)
    {
        cout << "Unable to load the file. Try again later." << endl;
        productFile.close();
        exit(0);
    }
    else
    {
        ifstream productFile("productsfile.txt");

        if (productFile.is_open())
        {
            cout << "How may I help you?" << endl;
            string line;
            while (getline(productFile, line))
            {
                // using printf() in all tests for consistency
                cout << line.c_str() << endl;
            }
            productFile.close();
        }

    }

}

void saveToFile() //this function saves in the text file the data we've globally declared. It is used only if you want to declare new variables.
{
    productFile.open("productsfile.txt", ios::out);

    for (int i = 0; i < 5; i++)
    {
        productFile << i + 1 << ". " << productName[i] << ": " << productAmount[i] << endl;
    }
    productFile << "6. Exit" << endl;
    productFile.close();
}

void askIfDefault()
{
    cout << "Do you want to come back to default stock?" << endl;
    cout << "1. Yes " << "2. No " << endl;
    cin >> defaultPick;
    switch (defaultPick)
    {
    case 1:
        for (int i = 0;i < 5;i++)
        {
            productAmount[i] = 10;
        }
        saveToFile();
        loadFromFile();
        break;
    case 2:
        loadFromFile();
        break;
    default:
        cout << "I don't understand." << endl;
        exit(0);
        break;
    }
}

void productCheck()
{
    if (productAmount[productPick - 1] <= 0 || productAmount[productPick - 1] < productBuy)
    {
        cout << "Unfortunately we have no more " << productName[productPick - 1] << " in stock. Please choose other product from the list below: " << endl;
        productAmount[productPick - 1] = 0;
    }
    else
    {
        productAmount[productPick - 1] -= productBuy;
    }
}


void listOfProducts()
{
    cout << "How may I help you?" << endl;

    for (int i = 0; i < 5; i++)
    {
        cout << i + 1 << ". " << productName[i] << ": " << productAmount[i] << endl;
    }
    cout << "6. Exit" << endl;
}

void order()
{
    cin >> productPick;
    switch (productPick)
    {
    case 1:
        cout << "How many bottles?" << endl;
        cin >> productBuy;
        {
            productCheck();
            saveToFile();
        }
        break;

    case 2:
        cout << "How many cartons?" << endl;
        cin >> productBuy;
        {
            productCheck();
            saveToFile();
        }
        break;

    case 3:
        cout << "How many multi-packs?" << endl;
        cin >> productBuy;
        {
            productCheck();
            saveToFile();
        }
        break;

    case 4:
        cout << "How many portions?" << endl;
        cin >> productBuy;
        {
            productCheck();
            saveToFile();
        }
        break;

    case 5:
        cout << "How many batches?" << endl;
        cin >> productBuy;
        {
            productCheck();
            saveToFile();
        }
        break;

    case 6:
        cout << "See you soon!" << endl;
        saveToFile();
        system("pause");
        break;

    case 666:
        cout << "You cannot use the secret magic spells here." << endl;
        saveToFile();
        exit(0);
        break;

    default:
        cout << "Please pick the existing product: " << endl;
        saveToFile();
        order();
        break;
    }
}


int main()
{
    askIfDefault();
    order();
    cout << endl;

    while (true && productPick != 6)
    {
        listOfProducts();
        order();
        saveToFile();
        cout << endl;
    }

    return 0;


}

也许除非声明一个全局 fsteam productFile,否则尝试在使用它的两个函数中的每一个中声明它:分别是“loadFromFile()”和“saveToFile()”。 在他们的开始。 那时应该没问题。

让我对您的代码提出一些额外的建议 - 因为它有点难以遵循:

  • 选择反映函数功能的函数名称 - 并且不要在函数中执行超出其名称指示的任何操作。 例如,如果您编写了一个名为ask_whether_to_restock()函数 - 该函数应该提出问题,甚至可能得到答案,但即使答案为“是”,也不会实际补货 - 也不向文件写入任何内容。 即使从文件中读取信息也有点过分。
  • 如果你需要在一个函数中做比它名字暗示的更多的事情 - 为额外的工作编写另一个函数,另一个函数调用前两个中的每一个并结合它们的作用。 例如, determine_whether_to_restock()可以调用read_current_stock_state()从文件中读取,也可以调用print_stock_state()get_user_restocking_choice()
  • 尽量避免使用全局变量。 更喜欢向每个函数传递它需要使用的变量(或在必要时引用/指向它们的指针)。
  • 不要重复自己 (DRI):不要重复使用switch(produtPick)语句 - 尝试使用以下内容编写一些内容:

     cout << "How many " << unit_name_plural[productPick] << "?" << endl;

    带有带有“瓶子”、“罐”、“部分”等的附加字符串数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM