Autor Tema: C++ programa que permite insertar, borrar y editar datos de lista productos  (Leído 6759 veces)

nathaliness@gmail.com

  • Visitante
  :P
Hola estoy tratando de hacer una base de datos se que aun no esta terminada pero quiero saber porque no compila, no puedo comprobar si esta bien lo que ya estas hecho y continuar.

Les agradezeco de antemano:

Código: [Seleccionar]
#include<iostream>
#include<iomanip>
#include <string.h>

using namespace std;

const int MAX_P = 999;            // max number of products in the database
const int L_ID = 999;             // max >= ID
const int L_NM  = 15;             // max length of name ... a-z i AZ 09
const int L_CAT = 15;             // max characters of each category ... """""
const int L_PR = 999;          // max price   >0 2 decimales
const int L_DAT = 8;              // date yyyy-mm 1-12-dd-1-31
const int L_QUA = 999;            // maximun quantity
const int L_SUPP = 999;            // maximun supply
const int L_ID_T = L_ID + 1;     // to be used in the definition of null-terminated string
const int L_CAT_T = L_CAT + 1;  //                 -- || --
const int L_NM_T  = L_NM + 1;  //                 -- || --
const int L_PR_T = L_PR + 1; 
const int L_DAT_T = L_DAT + 1;
const int L_QUA_T = L_QUA + 1; 
const int L_SUPP_T = L_SUPP + 1;


struct product{
    char ID[L_ID_T];
    char name[L_NM_T];
    char category[L_CAT_T];
    char price[L_PR_T];
char date[L_DAT_T];
char quantity[L_QUA_T];
char supply[L_SUPP_T];
};

void initialize ( product L[], int &prod_cnt );

void show_menu ( int opc );
void menu_main ( product L[], int prod_cnt );
void menu_search ( const product L[], const int prod_cnt );
void menu_remove ( product L[], int &prod_cnt );
void menu_modify ( product L[], int &prod_cnt );

void write_listing_header ();
void show_product ( const product );
void list_products ( const product L[], const int prod_cnt );
void list_products_ind ( const product L[], const int Ind[], const int ind_cnt );

void insert_product ( product L[], int &prod_cnt );

void remove_product_ID   ( product L[], int &prod_cnt );
void n_remove_product_ID ( product L[], int &prod_cnt );

void bsort ( product L[], int n );

int search_ID  ( const product L[], const int prod_cnt, char ID [] );
int search_cate ( const product L[], const int prod_cnt, char category[], int Ind[] );

void search_wrt_ID  ( const product L[], const int prod_cnt );
void search_wrt_cate ( const product L[], const int prod_cnt );


// ------------------------------------------------------
int main()
{
  product L[MAX_P];    // vector of structs of type product
  int prod_cnt;       // actual number of products in vector L
 
  initialize(L,prod_cnt);
  menu_main(L,prod_cnt);
}


// --------------------------------------------------------------- SEARCHING

// Notice: ID is unique
// -----------------------------------------------------------

int search_ID (const product L[], const int prod_cnt,  char ID[])
{
    for (int i = 0; i < prod_cnt; i++)
       if (strcmp( L[i].ID, ID ) == 0) return i;
    return -1 ;
}


// Searching wrt ID (ID is unique)
// -----------------------------------------------------------

void search_wrt_ID (const product L[], const int prod_cnt)
{
     char ID[L_ID_T];
     int ind;
     
     cout << "Enter ID: ";
     cin >> ID;
     if ((ind = search_ID(L,prod_cnt,ID)) != -1 )
     {
        write_listing_header();
        show_product(L[ind]);
     }   
     else
     {
       cout << "No product with ID " << ID << endl;
       system("pause");
     }   
}

// ---------------------------------------------------------------------------------------

int search_cate (const product L[], const int prod_cnt, char category[], int ind[])
{
    int cnt = 0;
    for (int i = 0; i < prod_cnt; i++)
       if ( strcmp(L[i].category, category ) == 0)  ind[cnt++] = i;
    return cnt;
}

// Searching for categories
// -------------------------------------------------------------------------
void search_wrt_cate (const product L[], const int prod_cnt)
{
     char category[L_CAT_T];
     int  Ind[MAX_P];
     int  ind_cnt;
     
     cout << "Enter category: ";
     cin >> category;
     if ( (ind_cnt = search_cate(L,prod_cnt,category,Ind)) != 0 )
          list_products_ind(L,Ind,ind_cnt);
     else
     {
       cout << "No product in category " << category << endl;
       system("pause");
     }   
}

// --------------------------------------------------------------- INSERTING


// Inserting a new product:
//   - checking maximum number of products MAX_P
//   - checking uniqueness of ID
// -----------------------------------------------------------

void insert_product (product L[], int &prod_cnt)
{
  char ID[L_ID_T];
  int ind;
 
  if (prod_cnt < MAX_P)
  {
    cout << "ID: ";
    cin >> ID;
    if ( (ind = search_ID(L,prod_cnt,ID)) == -1)
    {
      strcpy(L[prod_cnt].ID,ID);
      cout << "category: ";
      cin >> L[prod_cnt].category;
      cout << "Name: ";
      cin >> L[prod_cnt].name;
      cout << "Price: ";
      cin >> L[prod_cnt].price;
      cout << "Date: ";
      cin >> L[prod_cnt].date;
      cout << "Quantity: ";
      cin >> L[prod_cnt].quantity;
      cout << "Supply: ";
      cin >> L[prod_cnt].supply;
      prod_cnt ++;
    }
    else
    {
      cout << "product with ID " << ID
           << " already exists in the database " << endl;
      show_product( L[ind] );
      system("pause");
    }
  }
  else
  {
     cout << "Impossible to insert new product - out of storage ;)" << endl;
     system("pause");
  }
}


// ------------------------------------------------------------ DELETING

// Deleting the product with a given ID number

// -----------------------------------------------------
void remove_product_ID (product L[], int &prod_cnt)
{
    char ID[L_ID_T];
    int ind;
   
    cout << "Enter ID of a product to be removed: ";
    cin >> ID;
    if ( (ind = search_ID( L, prod_cnt, ID )) != -1 )  // found!
    {
       for( int i = ind; i < prod_cnt - 1; i++ )
          L[i] = L[i+1];
       prod_cnt--;
    }
    else
    {
      cout << "No product with ID " << ID << endl;
      system("pause");
    }
}

// Deleting product with a given ID

// -----------------------------------------------------
void n_remove_product_ID (product L[], int &prod_cnt)
{
    char ID[L_ID_T];
    int ind;
   
    cout << "Enter ID of a product to be removed: ";
    cin >> ID;
    if ( (ind = search_ID( L, prod_cnt, ID )) != -1 )
    {
       L[ind] = L[prod_cnt-1];
       prod_cnt--;
    }
}

// ------------------------------------------------------------- LISTINGS

void show_product (const product p)
{
  cout << setw(5)  << p.ID;
  cout << setw(11) << p.name;
  cout << setw(10) << p.category;
  cout << setw(10) << p.price;
  cout << setw(10) << p.date;
  cout << setw(10) << p.quantity;
  cout << setw(10) << p.supply;
    cout << endl;
}


// -----------------------------------------------------
void write_listing_header()
{
  cout << endl;
  cout << "  ID  |   Name   |  Category  |  Price  |  Date  |  Quantity  |  Supply  |" << endl;
  cout << "-------------------------------------------------------------------------" << endl;
}


// -----------------------------------------------------
void list_products (const product L[], const int prod_cnt)
{
  write_listing_header();
  for (int i = 0; i < prod_cnt; i++)
     show_product( L[i] );
  cout << "------------------------------------------------------------------------" << endl;
  cout << "Number of products = " << prod_cnt << endl;
  cout << endl;
}

// -----------------------------------------------------
void list_products_ind (const product L[], const int ind[], const int ind_cnt)
{
  write_listing_header();
  for (int i = 0; i < ind_cnt; i++)
     show_product( L[ind[i]] );
  cout << "---------------------------------------------------" << endl;
  cout << "Number of products = " << ind_cnt << endl;
  cout << endl;
}

// --------------------------------------------------------------- OTHER

// sorting nondecreasingly wrt category
// ------------------------------------------------------
void bsort (product L[], int n)
{
  product buf;

  for (int j=n-2; j>=0; j--)         
    for (int i=0; i<=j; i++)       
      if ( strcmp(L[i].category,L[i+1].category) > 0)
      {
        buf = L[i+1];
        L[i+1] = L[i];
        L[i] = buf;
      }
}

// ------------------------------------------------------
void initialize (product L[], int &prod_cnt)
{
  strcpy(L[0].ID,"001");     
  strcpy(L[0].name,"Hairband");
  strcpy(L[0].category,"Kids");
  strcpy(L[0].price,"10");
  strcpy(L[0].date,"2014/12/25");
  strcpy(L[0].quantity,"45");
  strcpy(L[0].supply,"32");
 

  strcpy(L[1].ID,"002");
  strcpy(L[1].name,"Skirt");
  strcpy(L[1].category,"Woman");
  strcpy(L[1].price,"220");
  strcpy(L[1].date,"2014/12/25");
  strcpy(L[1].quantity,"100");
  strcpy(L[1].supply,"32");
 

  strcpy(L[2].ID,"003");
  strcpy(L[2].name,"Blazer");
  strcpy(L[2].category,"Man");
  strcpy(L[2].price,"570");
  strcpy(L[2].date,"2015/1/01");
  strcpy(L[2].quantity,"75");
  strcpy(L[2].supply,"2");
 

  strcpy(L[3].ID,"004");     
  strcpy(L[3].name,"Minisocks");
  strcpy(L[3].category,"Kids");
  strcpy(L[3].price,"35");     
  strcpy(L[3].date,"2014/12/30");
  strcpy(L[3].quantity,"100");
  strcpy(L[3].supply,"30");
 

  strcpy(L[4].ID,"005");
  strcpy(L[4].name,"Dress");
  strcpy(L[4].category,"Woman");
  strcpy(L[4].price,"90");
  strcpy(L[4].date,"2014/12/25");
  strcpy(L[4].quantity,"80");
  strcpy(L[4].supply,"2");
 

  strcpy(L[5].ID,"006");
  strcpy(L[5].name,"Shrit");
  strcpy(L[5].category,"Man");
  strcpy(L[5].price,"120");
  strcpy(L[5].date,"2014/12/20");
  strcpy(L[5].quantity,"200");
  strcpy(L[5].supply,"20");
 
  strcpy(L[6].ID,"007");     
  strcpy(L[6].name,"Sweter");
  strcpy(L[6].category,"Man");
  strcpy(L[6].price,"210");
  strcpy(L[6].date,"2014/12/20");
  strcpy(L[6].quantity,"200");
  strcpy(L[6].supply,"71");
 
  prod_cnt = 6;
}


// --------------------------------------------------------------  MENU
void show_menu (int opc)
{
  switch(opc)
  {
    case 1: {
             cout << endl;
             cout << "\n\t\t DATABASE" << endl;
             cout << "\t ==========="  << endl;
             cout << "\n\t Please select an option:" <<endl<< endl;
             cout << "1 - Add a new product to the database"   << endl;
             cout << "2 - Remove a product"  << endl;
             cout << "3 - Display the list of all products"      << endl;
             cout << "4 - Sort products" << endl;
             cout << "5 - Search for a product" << endl;
             cout << "6 - Modify a product" << endl;
             cout << "0 - Exit the program"     << endl;
             cout << " \n Option: ";
             break;
            }
    case 2: {
             cout << endl;
             cout << "SEARCH menu" << endl;
             cout << "----------------------" << endl;
             cout << "1 - search wrt name"   << endl;
             cout << "2 - search wrt ID"  << endl;
             cout << "0 - main menu";
             cout << "> ";
             break;
            }
    case 3: {
             cout << endl;
             cout << "REMOVE menu" << endl;
             cout << "----------------------" << endl;
             cout << "1 - remove wrt category"   << endl;
             cout << "2 - remove wrt ID"  << endl;
             cout << "0 - main menu";
             cout << "> ";
             break;
            }       
  }
}


void menu_modify (product L[], int &prod_cnt)
{
            // ...
}


// --------------------------------------------------------
void menu_remove (product L[], int &prod_cnt)
{
     int choice;
     do {
         show_menu(3);
         cin >> choice;
         switch(choice)
         {
           case 1: // ..........
                   break;
           case 2: remove_product_ID(L,prod_cnt);
                   break;
         }
     } while (choice != 0);
}

// --------------------------------------------------------
void menu_search (const product L[], const int prod_cnt)
{
     int choice;
     do {
         show_menu(2);
         cin >> choice;
         switch(choice)
         {
           case 1: search_wrt_cate(L,prod_cnt);
                   break;
           case 2: search_wrt_ID(L,prod_cnt);
                   break;
         }
     } while (choice != 0);
}

// --------------------------------------------------------
void menu_main (product L[], int prod_cnt)

  int choice;
 
  do {
     show_menu(1);
     cin >> choice;
     switch(choice)
     {
       case 1: insert_product(L,prod_cnt);
               break;
       case 2: menu_remove(L,prod_cnt);
               break;
       case 3: list_products(L,prod_cnt);
               break;
       case 4: bsort(L,prod_cnt);
               break;
       case 5: menu_search(L,prod_cnt);
               break;
       case 6: menu_modify(L,prod_cnt);
               break;
     }
  } while(choice!=0);
}




« Última modificación: 26 de Enero 2015, 08:59 por Alex Rodríguez »

Mario R. Rancel

  • Administrador
  • Experto
  • ********
  • APR2.COM
  • Mensajes: 1978
    • Ver Perfil
Hola nathaliness, antes que nada pedirte que para insertar mensajes en los foros leas lo que se dice aquí, https://www.aprenderaprogramar.com/foros/index.php?topic=1460.0

Por ejemplo para poner el título de este tema hubiera sido más útil nombrar el lenguaje con el que estás trabajando, qué compilador o entorno de desarrollo usas, el objetivo del programa y cuál es el problema que tienes, que poner "maestros de la programación, échenle un vistazo a este código", que no es descriptivo y no permite localizar el tema si dentro de unos días quieres buscarlo por ejemplo.

Por otro lado, para ver por qué no compila un código debes ir analizándolo y probándolo poco a poco. Es decir, si este código tiene 500 líneas debería haberse ido probando con 20 líneas, 40 líneas, 60 líneas, etc.

Por tanto mi recomendación es que empieces a generar el programa desde cero y lo vayas probando poco a poco, ya que en 500 líneas puede haber un número de errores "indefinido", hasta el punto de requerir muchas horas su depuración.

Saludos

Mario R. Rancel

  • Administrador
  • Experto
  • ********
  • APR2.COM
  • Mensajes: 1978
    • Ver Perfil
Nota: en mi caso (en el tuyo puede que dependa de algún detalle del compilador que uses) he conseguido que compile introduciendo unos mínimos cambios, simplemente cambiar 999 por 99, y comentar las líneas con system("pause");

Saludos


 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".