helps with the phone calendar

Discussion in 'C++' started by juance, Sep 20, 2010.

  1. juance

    juance New Member

    Joined:
    Sep 18, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello, I am new in this forum, I am making a phone calendar in c++ and the truth is that I am having some problems.
    The programming paradigm to be used for the development of the calendar is the Programming to Modulate and Structured.
    Some of the errors are when I try to show the entered registrations, me the sample but he/she repeats again the I finish entered registration.
    And the other problem is when I try to erase a registration, it asks me him to enter the I number from registration to erase, when I enter him an I number of registration that non existe,el programs me he/she says that I erase it with success.
    if they can help me with these problems from already thank you.
    greetings.

    ***************************************************
    #include <iostream>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>

    using namespace std;
    void agregar(struct datos d);
    void modificar(struct datos d, int nm);
    void eliminar(struct datos d, int ne);
    void buscar(struct datos d);
    void mostrar(struct datos d);
    int menu();


    struct datos
    {
    char nombre[50];
    int telefono;
    char direccion[50];
    char email[50];
    int valido; //valido=1->si existe valido=0->no existe
    int valido2; //checara si el registro es correcto... siempre sera igual a 1706887114
    };

    int main()
    {
    struct datos d;
    int x=0;
    FILE *Fichero;
    Fichero = fopen("fichero", "wb");
    //fstream ("registros.dat", ios::eek:ut | ios::in | ios::binary); //creamos el archivo
    int n=0,band=0;
    while (band==0)
    {
    n=menu();
    switch (n)
    {
    case 1: agregar(d); break;
    case 2: buscar(d); break;
    case 3: mostrar(d); break;
    case 4: band=1; break;
    }
    }
    system("PAUSE");
    return 0;
    }


    void agregar(struct datos d)
    {

    char na='s'; //na=preguntar si kiere nuevo registro
    while (na=='s')
    {
    char nn='x'; //nn=verificacion para agregar el registro
    system ("cls"); //borramos pantalla
    //guardamos los datos temporalmente en el struct
    cin.ignore();
    cout<<"Nombre: ";cin.getline(d.nombre,50);
    cout<<"Telefono: "; cin>> d.telefono;
    cin.ignore();
    cout<<"Direccion: ";cin.getline(d.direccion,50) ;
    cout<<"Email: "; cin>> d.email; cout<<endl;
    d.valido=1; //decimos ke el registro si existe
    d.valido2=1706887114; //con esto garantizamos ke es uno de nuestros registros

    //verificamos si kiere agregar el nuevo registro
    while ((nn!='s') && (nn!='n'))
    { cout<<endl<<"Deseas agregar el nuevo registro? [s-n]: "; cin>>nn; }

    //agregamos
    if (nn=='s')
    {
    FILE *Fichero= fopen("fichero", "ab+");
    //activamos el archivo en modo de escritura tomando en cuenta el parametro "app" para agregar en la ultima posicion del archivo
    if(!Fichero )
    // if(!reg) //si no se pudo abrir el archivo
    { cout <<"No se puede abrir el fichero."; }
    fseek(Fichero,0,2);
    fwrite(&d, sizeof(struct datos), 1, Fichero);
    fclose(Fichero);
    // Colocar el cursor al final del archivo
    //escribimos el nuevo registro
    //cerramos el archivo
    cout<<endl<<"Registro agregado exitosamente"<<endl;
    system("pause");
    }
    else
    {
    //borramos los datos temporales del struct
    strcpy(d.nombre,"");
    d.telefono=0;
    strcpy(d.direccion,"");
    strcpy(d.email,"");
    d.valido=0;
    d.valido2=0;
    cout<<endl<<"El Registro NO fue agregado"<<endl;
    system("pause");
    }
    na='x';
    while ((na!='s') && (na!='n'))
    { cout<<endl<<endl<<"Deseas agregar un registro nuevo? [s-n]: "; cin>>na; } //la validacion se hace en el while inicial
    }//endwhile
    }


    void modificar(struct datos d, int nm)
    {
    char nnm='x';
    system ("cls"); //borramos pantalla
    //guardamos los datos temporalmente en el struct
    cin.ignore();
    cout<<"Nombre: ";cin.getline(d.nombre,50);
    cout<<"Telefono: "; cin>> d.telefono;
    cin.ignore();
    cout<<"Direccion: ";cin.getline(d.direccion,50) ;
    cout<<"Email: "; cin>> d.email; cout<<endl;
    d.valido=1; //decimos ke si existe
    d.valido2=1706887114;

    //verificamos si kiere modificar el registro
    while ((nnm!='s') && (nnm!='n'))
    { cout<<endl<<"Deseas modificar este registro? [s-n]: "; cin>>nnm; }

    //agregamos
    if (nnm=='s')
    {
    FILE *Fichero = fopen("fichero","rb+");
    //ofstream reg("registros.dat", ios::eek:ut | ios::binary); //activamos el archivo en modo de escritura (creamos el objeto 'reg')
    if(!Fichero) //si no se pudo abrir el archivo
    { cout <<"No se puede abrir el fichero."; }
    fseek(Fichero,(nm-1)*sizeof(d),0);
    fwrite(&d,sizeof(d),1,Fichero);
    fclose(Fichero);
    //colocamos el apuntador en la posicion inicial del registro a modificar
    //escribimos el nuevo registro
    //cerramos el archivo
    cout<<endl<<"Registro modificado exitosamente"<<endl;
    }
    else
    {
    //borramos los datos temporales del struct
    strcpy(d.nombre,"");
    d.telefono=0;
    strcpy(d.direccion,"");
    strcpy(d.email,"");
    d.valido=1;
    d.valido2=0;
    cout<<endl<<"El Registro NO fue modificado"<<endl;
    }
    system("pause");
    }


    void eliminar(struct datos d, int ne)
    {
    system ("cls"); //borramos pantalla
    d.valido=0; //modificamos "valid0" del struct para decir ke ese registro no sera tomado en cuenta
    d.valido2=1706887114; //agregamos nuestra garantia de registro

    //ahora actualizamos el registro
    FILE *Fichero = fopen("fichero","rb+");
    //activamos el archivo en modo de escritura (creamos el objeto 'reg')
    if(!Fichero) //si no se pudo abrir el archivo
    { cout <<"No se puede abrir el fichero."<<endl; system("pause");}
    fseek(Fichero,(ne-1)*sizeof(d),0);
    fwrite(&d,sizeof(d),1,Fichero);
    //colocamos el apuntador en la posicion inicial del registro a modificar
    //escribimos el nuevo registro
    fclose(Fichero); //cerramos el archivo
    cout<<endl<<"Registro eliminado"<<endl;
    system("pause");
    }


    void buscar(struct datos d)
    {
    int nb1=0, nb=-2; //nb1=variable auxiliar para saber si kiere volver a buskar o salir nb=variable ke guarda el #de registro buskado
    while (nb1<4)
    {
    if (nb1==2)
    { modificar(d, nb); }
    else
    {
    if (nb1==3)
    { eliminar(d, nb); }
    }
    system("cls"); //borramos pantalla
    int nb=-2;
    while (nb<0)
    { cout<<"Introduce el registro a buscar [0 para salir]: "; cin>>nb; }
    if (nb!=0) //si kiere buskar...
    {
    FILE *Fichero = fopen("fichero", "rb");
    fseek(Fichero,(nb-1)*sizeof(d),0);
    fread(&d, sizeof(d), 1, Fichero);
    //activa el archivo en modo de lectura (creamos el objeto 'reg')
    //colocamos el apuntador en la posicion inicial del registro encontrado
    //leemos el registro encontrado
    //verificamos que el registro es correcto
    //*fclose(Fichero);
    if ((d.valido==1) && (d.valido2==1706887114))
    {
    cout<<endl<<endl<<"--------Registro encontrado--------"<<endl;
    cout<<"Nombre: "<<d.nombre<<endl<<"Telefono: "<<d.telefono<<endl<<"Direccion: "<<d.direccion<<endl<<"Email: "<<d.email<<endl<<endl;
    cout<<endl; //imprimimos menu de opciones
    cout<<" ================================"<<endl;
    cout<<" || OPCIONES ||"<<endl;
    cout<<" ================================"<<endl;
    cout<<" || 1. NUEVA BUSQUEDA ||"<<endl;
    cout<<" || 2. MODIFICAR REGISTRO ||"<<endl;
    cout<<" || 3. ELIMINAR REGISTRO ||"<<endl;
    cout<<" || 4. SALIR ||"<<endl;
    cout<<" ================================"<<endl<<endl;
    nb1=0;
    while ((nb1<1) || (nb1>4))
    { cout<<" Introduce un numero de las opciones listadas arriba [1-4]: "; cin>>nb1; }
    cout<<endl;
    }//endif ((d.valido==1) && (d.valido2==1706887114))
    else //si el registro encontrado no es correcto
    {
    cout<<endl<<"El registro buscado no existe"<<endl;
    system("pause");
    nb1=1;
    }
    }//endif (nb!=0)
    else //si ya se kiere salir
    { nb1=4; } //igualamos la bandera a 4 para ke se salga
    } //endwhile

    }


    void mostrar(struct datos d)
    {
    int nb11=0,con=0,nme=0,bandera=0; //nb1=variable auxiliar para saber si kiere volver a buskar o salir nb=variable ke guarda el #de registro buskado
    while (nb11<3)
    {
    if (nb11==1)
    { modificar(d, nme); }
    else
    {
    if (nb11==2)
    { eliminar(d, nme); }
    }
    system("cls"); //borramos pantalla
    FILE *Fichero = fopen("fichero", "rb");
    //ifstream reg("registros.dat", ios::in | ios::binary); //activa el archivo en modo de lectura (creamos el objeto 'reg')
    con=0;
    bandera=0;
    //while(!feof(Fichero))
    while (Fichero && !feof(Fichero))
    //while (reg && !reg.eof())
    {
    fseek(Fichero,(con)*sizeof(d),0);
    fread(&d, sizeof(d), 1, Fichero);
    //colocamos el apuntador en la posicion inicial del registro encontrado
    //leemos el registro encontrado
    con++; //incrementamos contador del registro
    //verificamos que el registro es correcto
    if ((d.valido==1) && (d.valido2==1706887114))
    {
    bandera=1;
    cout<<endl<<"--------Registro "<<con<<"--------"<<endl;
    cout<<"Nombre: "<<d.nombre<<endl<<"Telefono: "<<d.telefono<<endl<<"Direccion: "<<d.direccion<<endl<<"Email: "<<d.email<<endl<<endl;
    }//endif
    }//endwhile
    //*fclose(Fichero);
    if (bandera==1)
    {
    cout<<endl<<endl; //imprimimos menu de opciones
    cout<<" ================================"<<endl;
    cout<<" || OPCIONES ||"<<endl;
    cout<<" ================================"<<endl;
    cout<<" || 1. MODIFICAR REGISTRO ||"<<endl;
    cout<<" || 2. ELIMINAR REGISTRO ||"<<endl;
    cout<<" || 3. SALIR ||"<<endl;
    cout<<" ================================"<<endl<<endl;
    nb11=0;
    //preguntamos por ke opcion kiere realizar
    while ((nb11<1) || (nb11>3))
    { cout<<" Introduce un numero de las opciones listadas arriba [1-4]: "; cin>>nb11; }
    nme=0;
    while ((nme<1) && (nb11<3))
    { cout<<" Introduce el numero del registro: "; cin>>nme; }
    }//endif bandera=1
    else
    { cout<<"No existen registros!"<<endl; system("pause"); nb11=3;}
    } //endwhile

    }


    int menu()
    {
    int n=0;
    system("cls");
    cout<<endl<<endl;
    cout<<" ==========================================="<<endl;
    cout<<" || AGENDA PERSONAL ||"<<endl;
    cout<<" ==========================================="<<endl;
    cout<<" || 1. AGREGAR NUEVO REGISTRO ||"<<endl;
    cout<<" || 2. BUSCAR REGISTRO ||"<<endl;
    cout<<" || 3. MOSTRAR TODOS LOS REGISTROS ||"<<endl;
    cout<<" || 4. SALIR ||"<<endl;
    cout<<" ==========================================="<<endl<<endl;
    while ((n<1) || (n>4))
    { cout<<" Introduce un numero de las opciones listadas arriba [1-4]: "; cin>>n; }
    cout<<endl;
    return n;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice