【问题标题】:Item Inventory Add/Edit/View questions项目库存添加/编辑/查看问题
【发布时间】:2015-03-25 05:44:30
【问题描述】:

好的,我对此有很多问题。我遇到了很多麻烦,但进展缓慢。到目前为止,我已经知道程序将读取和写入文件的位置,但它往往会重复很多次。就像从文件中读取时一样,它只会一遍又一遍地读取相同的信息。此外,它似乎两次显示相同的记录......我无法进一步测试它,因为它只让我一遍又一遍地写第一个记录而没有写第二个。任何帮助都会很棒。同样由于某种原因,编辑功能在选择 1 记录时似乎拉扯扯淡......所以我很难过。已经为此工作了几个小时,但没有运气

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

using namespace std;

void addRecord(fstream &);
void viewRecord(fstream &);
void changeRecord(fstream &);
int menu();

const int DESC_SIZE = 21;
const int DATE_SIZE = 11;

struct inventoryData

{
        char desc[DESC_SIZE]; //Desc up to 20 chars
        int quantity; //Item quantity
        double wholesale; //Item wholsale Cost
        double retail; //Item retail Cost
        char date[DATE_SIZE]; //Date able to hold info up to xx/xx/xxxx

};


int main()
{
    int selection;
    int recordNumber;

    fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);
    if (dataFile.fail())
    {
        // The file does not exist, so create it.
        dataFile.open("inventory.dat", ios::out);
    }


    selection = menu();

    while (selection != 4)
    {
        switch (selection)
            {
                case 1:
                    {
                         viewRecord(dataFile);
                         break;
                    }
                case 2:
                {
                          addRecord(dataFile);
                          break;
                }
                case 3:
                    {
                         changeRecord(dataFile);
                         break;
                    }
                default:
                    {
                        cout << "Invalid - Please use 1 to 4" << endl;
                        break;
                    }
                    selection = menu();
            }
    }

    dataFile.close();

    return 0;
}

void addRecord(fstream &file)
{
    fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);
    inventoryData item;

    cout << "Please enter the following data about the item" << endl;
    cout << "Description: ";
    cin.ignore();
    cin.getline(item.desc, DESC_SIZE);
    cout << "Quantity: ";
    cin >> item.quantity;
    cout << "Quantity: ";
    cin >> item.quantity;
    cout << "Wholesale cost: ";
    cin >> item.wholesale;
    cout << "Retail price: ";
    cin >> item.retail;
    cout << "Date (Please use MO/DA/YEAR format: ";
    cin >> item.date;
    dataFile.write(reinterpret_cast<char *>(&item), sizeof(item));

    return;
}

void viewRecord(fstream &file)
{
    string output;
    fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);

    inventoryData item;
    fstream items;
    char again;

    dataFile.read(reinterpret_cast<char *>(&item), sizeof(item));

    while (!items.eof())
    {
    // Display the record.
    cout << "Description: " << item.desc << endl;
    cout << "Quantity: " << item.quantity << endl;
    cout << "Wholesale Cost: " << item.wholesale << endl;
    cout << "Retail Cost: " << item.retail << endl;
    cout << "Date: " << item.date << endl;

    // Wait for the user to press the Enter key.
    cout << "\nPress the Enter key to see the next record.\n";

    cin.get(again);
    // Read the next record from the file.
    dataFile.read(reinterpret_cast<char *>(&item), sizeof(item));
    }
}


void changeRecord(fstream &file)
{
    fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);

    inventoryData item;
    int recordNumber;

    cout << "Please choose a record number you want to edit" << endl;
    cin >> recordNumber;
    dataFile.seekg(recordNumber * sizeof(item), ios::beg);
    dataFile.read(reinterpret_cast<char *>(&item), sizeof(item));
    cout << "Description: " << item.desc << endl;
    cout << "Quantity: " << item.quantity << endl;
    cout << "Wholesale cost: " << item.wholesale << endl;
    cout << "Retail price: " << item.retail << endl;
    cout << "Date: " << item.date << endl;
    cout << endl;

    // Get the new record data.
    cout << "Enter the new data:\n";
    cout << "Description: ";
    cin.ignore();
    cin.getline(item.desc, DESC_SIZE);
    cout << "Quantity: ";
    cin >> item.quantity;
    cout << "Quantity: ";
    cin >> item.quantity;
    cout << "Wholesale cost: ";
    cin >> item.wholesale;
    cout << "Retail price: ";
    cin >> item.retail;
    cout << "Date (Please use MO/DA/YEAR format: ";
    cin >> item.date;

    // Move back to the beginning of this record's position
    dataFile.seekp(recordNumber * sizeof(item), ios::beg);
    // Write new record over the current record
    dataFile.write(reinterpret_cast<char *>(&item), sizeof(item));

}

int menu()
{
    int menuSelection;

    cout << fixed << showpoint << setprecision(2);
    cout << "----------Inventory----------" << endl;
    cout << "1 - View inventory" << endl;
    cout << "2 - Add an item" << endl;
    cout << "3 - Edit an item" << endl;
    cout << "4 - End Program" << endl;
    cin >> menuSelection;

    if (!cin)
    {
        cout << "Invalid - Please use 1 to 4" << endl;
        cin >> menuSelection;
    }

    if (menuSelection < 1 || menuSelection > 4)
    {
        cout << "Invalid - Please use 1 to 4" << endl;
        cin >> menuSelection;
    }

    return menuSelection;
}

【问题讨论】:

    标签: c++ inventory repeat


    【解决方案1】:
    int menu()
    {
        int menuSelection = 0;//initialize
    
        cout << fixed << showpoint << setprecision(2);
        cout << "----------Inventory----------" << endl;
        cout << "1 - View inventory" << endl;
        cout << "2 - Add an item" << endl;
        cout << "3 - Edit an item" << endl;
        cout << "4 - End Program" << endl;
    
        //*** flush cin, this is inside a loop, it can cause problems
        cin.clear();
        fflush(stdin);
        cin >> menuSelection;
    
        return menuSelection;
    }
    
    int main()
    {
        fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);
        if (dataFile.fail())
        {
            // The file does not exist, so create it.
            dataFile.open("inventory.dat", ios::out);
            dataFile.close();
        }
    
        for (;;)
        {
            int selection = menu();
            if (selection == 4)
                break;
    
            switch (selection)
            {
            case 1:
                viewRecord(dataFile);
                break;
            case 2:
                addRecord(dataFile);
                break;
            case 3:
                changeRecord(dataFile);
                break;
            default:
                cout << "Invalid - Please use 1 to 4" << endl;
                break;
            }
        }
    
        return 0;
    }
    
    void viewRecord(fstream &notused)
    {
        fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);
        inventoryData item;
        while (dataFile)
        {
            dataFile.read(reinterpret_cast<char*>(&item), sizeof(item));
            // Display the record.
            cout << "Description: " << item.desc << endl;
            cout << "Quantity: " << item.quantity << endl;
            cout << "Wholesale Cost: " << item.wholesale << endl;
            cout << "Retail Cost: " << item.retail << endl;
            cout << "Date: " << item.date << endl;
            cout << endl;
        }
    }
    
    void addRecord(fstream &notused)
    {
        fstream file("inventory.dat", ios::in | ios::out | ios::app | ios::binary);
        inventoryData item;
    
        cout << "Please enter the following data about the item" << endl;
        cout << "Description: ";
        cin.ignore();
        cin.getline(item.desc, DESC_SIZE);
    
        cout << "Quantity: ";
        cin >> item.quantity;
    
        cout << "Wholesale cost: ";
        cin >> item.wholesale;
    
        cout << "Retail price: ";
        cin >> item.retail;
    
        cout << "Date (Please use MO/DA/YEAR format: ";
        cin.getline(item.desc, DATE_SIZE);
    
        file.write(reinterpret_cast<char *>(&item), sizeof(item));
    
        return;
    }
    

    【讨论】:

    • 非常感谢。这使得程序将添加一个项目然后返回菜单。但是查看似乎显示该项目两次然后陷入无限循环,并且添加似乎只是添加一个项目然后不会再添加。此外,当它试图显示选定的项目时,编辑似乎显示出乱码......然后也不会写入它。 >.
    • 我做了更多的改变,现在它有点工作了。 menu() 中的 cin 引起了问题,它需要刷新。您需要使用编辑功能。
    • 顺便说一下,当你添加到文件时,fstream 应该有::app 标志。我更新了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多