Create an algorithm that saves the data in HD

1

I created a simple calendar to record a user's contacts, but the teacher asked for the data to be saved in HD to preserve the contacts even after turning off the computer.

I have to write all the code using the file properties I'm looking for on the internet, or can I just implement the functionality in the ready code?

Follow the code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <fstream>
using namespace std;
void incluir();
void alterar(); 
void excluir();
void recuperar();
void exibir();
int vet[10]={0,0,0,0,0,0,0,0,0,0};
struct agenda {
    string email;
    string nome;
    string telefone;
};
struct agenda pessoa[10];
int main () {
    int opc;    
    do{
        cout<<"[1] incluir contato"<<endl;
        cout<<"[2] Alterar contato"<<endl;
        cout<<"[3] Excluir contato"<<endl;
        cout<<"[4] Recuperar contato"<<endl;
        cout<<"[5] Exibir contato"<<endl;
        cout<<"[6] Sair"<<endl;
        cin>>opc;
        system("clear || cls");
        switch (opc){
            case 1:
                incluir();

            break;

            case 2:
                alterar();

            break;  

            case 3:
                excluir();

            break;

            case 4:
                recuperar();

            break;

            case 5:
                exibir();

            break;

            case 6:
            break;

            default:
                cout<<"Opção Invalida"<<endl;   
        }
    }while(opc != 6);

    return 0;
}

void incluir(){ 
    int i, cont=0;
    for(i=0;i<10;i++){
        if(vet[i] == 0){
            cout<<"Digite o Email"<<endl;
            cin.ignore();
            cin>>pessoa[i].email;
            cout<<"Digite o Nome"<<endl;
            cin.ignore();
            getline(cin, pessoa[i].nome);
            cout<<"Digite o Telefone"<<endl;
            cin.ignore();
            cin>>pessoa[i].telefone;
            vet[i]=1;
            system("clear || cls");
            break;

        }
        else{
            cont++;
        }

    }
        if(cont==10){
            cout<<"Agenda Cheia"<<endl;
        }

}


void alterar(){
    int i, escolha, cont2=0, escolha2;
    cout<<"Qual Contato Deseja Alterar?"<<endl;
    for(i=0;i<10;i++){
        if(vet[i] == 0){
            cout<<"["<<i+1<<"] Vazio"<<endl;
        }
        else{
            cout<<"["<<i+1<<"] "<<pessoa[i].nome<<endl;
        }
    }
    do{
        cin>>escolha;
        if((escolha<=10) && (escolha>0)){
            if(vet[escolha-1] == 1){
                system("clear || cls");
                cout<<"Qual dado deseja alterar?"<<endl;
                cout<<endl;
                cout<<"[1] Email"<<endl;
                cout<<"[2] Nome"<<endl;
                cout<<"[3] Telefone"<<endl;
                cin>>escolha2;
                if(escolha2 == 1){
                    system("clear || cls");
                    cout<<"Digite o Email"<<endl;
                    cin.ignore();
                    cin>>pessoa[escolha-1].email;
                    system("clear || cls");
                }
                else if (escolha2 == 2){
                    system("clear || cls");
                    cout<<"Digite o Nome"<<endl;
                    cin.ignore();
                    getline(cin, pessoa[escolha-1].nome);
                    system("clear || cls");
                }
                else if(escolha2==3){
                    system("clear || cls");
                    cout<<"Digite o Telefone"<<endl;
                    cin.ignore();
                    cin>>pessoa[escolha-1].telefone;
                    system("clear || cls");
                }
                cont2=1;
            }
            else{
                cout<<"Contato Vazio"<<endl;
            } 



        }

        else{
            cout<<"Contato não existente"<<endl;
        }
    }while(cont2!=1);
}

void excluir(){
    int i, escolha, cont2=0;
    system("clear || cls");
    cout<<"Qual Contato Deseja Excluir?"<<endl;
    for(i=0;i<10;i++){
        if(vet[i] == 0){
            cout<<"["<<i+1<<"] Vazio"<<endl;
        }
        else{
            cout<<"["<<i+1<<"] "<<pessoa[i].nome<<endl;
        }
    }
    do{
        cin>>escolha;
        if((escolha<=10) && (escolha>0)){
            vet[escolha-1]=0;
            system("clear || cls");
            cont2=1;
        }
        else{
            cout<<"Contato Inexistente"<<endl;
        }
    }while(cont2!=1);
}

void recuperar(){
    int i, escolha, cont2=0;
    system("clear || cls");
    cout<<"Atenção: Você so pode recuperar um usuário, se nao houver armazenado outra pessoa no mesmo campo após a exclusão."<<endl;
    cout<<endl;
    cout<<"Qual campo deseja recuperar?"<<endl;
    for(i=0;i<10;i++){
        if(vet[i] == 0){
            cout<<"["<<i+1<<"] Vazio"<<endl;
        }
        else{
            cout<<"["<<i+1<<"] "<<pessoa[i].nome<<endl;
        }
    }
    do{
        cin>>escolha;
        if((escolha<=10) && (escolha>0)){
            vet[escolha-1]=1;
            system("clear || cls");
            cont2=1;
        }
        else{
            cout<<"Contato Inexistente"<<endl;
        }
    }while(cont2!=1);
}

void exibir(){
    int i, escolha, cont2=0, escolha2=0;
    system("clear || cls");
    cout<<"Qual Contato Deseja Exibir?"<<endl;
    for(i=0;i<10;i++){
        if(vet[i] == 0){
            cout<<"["<<i+1<<"] Vazio"<<endl;
        }
        else{
            cout<<"["<<i+1<<"] "<<pessoa[i].nome<<endl;
        }
    }
    do{
        cin>>escolha;
        if((escolha<=10) && (escolha>0)){
            system("clear || cls");
            cout<<"Nome: "<<pessoa[escolha-1].nome<<endl;
            cout<<"Email: "<<pessoa[escolha-1].email<<endl;
            cout<<"Telefone: "<<pessoa[escolha-1].telefone<<endl;
            cout<<endl;
            cout<<"[1] Sair"<<endl; 
            do{
                cin>>escolha2;
                if(escolha2==1){
                    cont2=1;
                    system("clear || cls");
                }
                else{
                    cout<<"Opção Invalida"<<endl;
                }
            }while(escolha2!=1);
        }
        else{
            cout<<"Contato Inexistente"<<endl;
        }
    }while(cont2!=1);
}
    
asked by anonymous 30.11.2017 / 18:19

1 answer

2

Yes, it is possible, but it does not mean that it is the most organized. If the intention is only to solve the problem, you can enter in this code, if you want to do the right, the manipulation would be completely separated and the parties would only communicate, each with their responsibility.

There are several things that could be more organized or even optimized (it's not about speed, it's the code itself). And it could be more C ++, this code is actually essentially C, except for streams .

Just having a few bad things will be a little more difficult to integrate file manipulation, either together or separate. The logic used works very specifically. It's not that you will have to do it all over again, but several points will have to be changed.

Have another question, the requirement is just to record or manipulate all the data in the file? If you just record it gets much easier because you can keep the logic, even if it is bad, and only create routines that record and read the data of the file and play in memory, then the bulk of your code does not even need to know that something exists in the file . It can put option to read and write or it can leave automatic, that is, it reads everything and plays in the array when it starts and every time that it moves in the array writes everything again in the file . This is inefficient and can bring problems on a large scale, but for an exercise it can be an alternative since it will not manipulate millions of data.

Just do not think all this will always be like this, this is an extremely simplified form, in real codes, a lot needs to be different.

Documentation .

    
30.11.2017 / 18:45