Inventory Management in C ++

2

The program is an inventory control that captures product information, such as quantity, wholesale and retail prices, date.

When the user chooses the Buy option, the program asks for the new product information to be entered, when it reaches the Fecha section, it jumps, it does not let me enter the date .

Code:

#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]; //Hasta  20 chars
    int quantity; //Cantidad del Producto
    double wholesale; //Costo Mayoreo
    double retail; //Costo Menudeo
    char date[DATE_SIZE]; //Fecha xx/xx/xxxx
};
int main()
{
    fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);
    if (dataFile.fail())
    {
        // El archivo no existe se tiene que crear.
        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 << "Invalido - Favor de usar 1 a 4" << endl;
                break;
        }
    }
    return 0;
}
void addRecord(fstream &notused)
{
    fstream file("inventory.dat", ios::in | ios::out | ios::app | ios::binary);
    inventoryData item;

    cout << "Porfavor Ingrese la siguiente informacion del producto" << endl;
    cout << "Descripcion: ";
    cin.ignore();
    cin.getline(item.desc, DESC_SIZE);

    cout << "Cantidad: ";
    cin >> item.quantity;

    cout << "Precio Mayoreo: ";
    cin >> item.wholesale;

    cout << "Precio Menudeo: ";
    cin >> item.retail;

    cout << "Fecha (Favor de usar MES/DIA/ANO formato: ";
    cin.getline(item.desc, DATE_SIZE);

    file.write(reinterpret_cast<char *>(&item), sizeof(item));

    return;
}
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 << "Descripcion: " << item.desc << endl;
        cout << "Cantidad: " << item.quantity << endl;
        cout << "Precio Mayoreo: " << item.wholesale << endl;
        cout << "Precio Menudeo: " << item.retail << endl;
        cout << "Fecha: " << item.date << endl;
        cout << endl;
    }
}
void changeRecord(fstream &file)
{
    fstream dataFile("inventory.dat", ios::in | ios::out | ios::binary);

    inventoryData item;
    int recordNumber;

    cout << "Porfavor escoge el numero de record para modificarlo" << endl;
    cin >> recordNumber;
    dataFile.seekg(recordNumber * sizeof(item), ios::beg);
    dataFile.read(reinterpret_cast<char *>(&item), sizeof(item));
    cout << "Descripcion: " << item.desc << endl;
    cout << "Cantidad: " << item.quantity << endl;
    cout << "Precio Mayoreo: " << item.wholesale << endl;
    cout << "Precio Menudeo: " << item.retail << endl;
    cout << "Fecha: " << item.date << endl;
    cout << endl;

    // Agregar datos nuevos.
    cout << "Ingresa Nuevos Datos:\n";
    cout << "Descripcion: ";
    cin.ignore();
    cin.getline(item.desc, DESC_SIZE);
    cout << "Cantidad: ";
    cin >> item.quantity;
    cout << "Precio Mayoreo: ";
    cin >> item.wholesale;
    cout << "Precio Menudeo: ";
    cin >> item.retail;
    cout << "Fecha (Porfavor ingresa fecha MES/DIA/ANO formato: ";
    cin >> item.date;

    // Regresa al principio
    dataFile.seekp(recordNumber * sizeof(item), ios::beg);
    // Escribir record nuevo encima de uno existente
    dataFile.write(reinterpret_cast<char *>(&item), sizeof(item));
}
int menu()
{
    int menuSelection = 0;//initialize

    cout << fixed << showpoint << setprecision(2);
    cout << "----------Inventario----------" << endl;
    cout << "1 - Ver Inventario" << endl;
    cout << "2 - Comprar" << endl;
    cout << "3 - Vender" << endl;
    cout << "4 - Terminar Programa" << endl;

    //*** flush cin, es un inside loop, creo que aqui esta causando problemas\
    cin.clear();
    fflush(stdin);
    cin >> menuSelection;

    return menuSelection;
}
    
asked by Guille San 05.05.2017 в 03:03
source

1 answer

2
cin >> item.retail;
cin.getline(item.desc, DATE_SIZE);

Calls to cin , by default, do not eliminate the final line break. It is when you call cin again when this line break is detected and discarded. The problem is that after that call you make another one to getline . This function reads until a line break is found.

final result? The date is blank.

The solution is simple, discard the last cin character (or empty it directly):

cin >> item.retail;
cin.ignore(); //descartar 1 carácter
cin.ignore(std::numeric_limits<int>::max()); // vaciar buffer
cin.getline(item.desc, DATE_SIZE);

That said, fflush is only safe to use in output buffers. never input. To empty the input buffer you have to use ignore .

    
answered by 05.05.2017 в 07:34