简体   繁体   中英

Why doesn't my vending machine program work correctly?

Program should begin with asking , whether to restock or continue with the current stock. The case 1 ( restock ) works perfectly , however the second case , to continue with the previous stock , returns zeros always if any of the products is zeroed.

In the textfile I have:

  1. Milk: 10
  2. Eggs: 2
  3. Water: 7
  4. Burrito: 10
  5. Bread: 12
  6. exit

How can i fix that ?

    #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;


}

Maybe unless declaring one global fsteam productFile, try to declare the it inside each of both functions that are using it: 'loadFromFile()' and 'saveToFile()' respectively. At the beginning of them. It should be fine then.

Let me make a few additional suggestions about your code - because it's a bit difficult to follow:

  • Choose function names which reflect what the function does - and don't do anything in a function which exceeds what its name indicates. For example, if you wrote a function called ask_whether_to_restock() - that function should ask the question, and perhaps even get the answer , but not actually restock even if the answer was "yes" - nor write anything to files. Even reading information from a file is a bit excessive.
  • If you need to do more in a function than its name suggests - write another function for the extra work, and yet another function which calls each of the first two and combines what they do. For example, determine_whether_to_restock() could call read_current_stock_state() which reads from a file, and also print_stock_state() and, say, get_user_restocking_choice() .
  • Try to avoid global variables. Prefer passing each function those variables which it needs to use (or references/pointers to them if necessary).
  • Don't Repeat Yourself (DRI): Instead of your repetitive switch(produtPick) statement - try writing something using the following:

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

    with an additional array of strings with "bottles", "cans", "portions" etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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