Create an algorithm that saves data in HD

Asked

Viewed 49 times

1

I created a simple schedule to record a user’s contacts, but the teacher asked for the data to be saved in hard drive to preserve contacts even after shutting down the computer.

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

Follows 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);
}
  • We have no way of knowing what this is about: "the archive properties I’m looking for on the internet"

  • The teacher just passed the work and did not explain anything about file, the file properties I refer to and take the data, phone, name and email that the user is entering and save them in HD so when to open again the data continue there. I can do this on top of the already written code or I must rewrite the entire agenda again?

  • You can vote for everything too, see the [tour].

1 answer

2


Yes, it’s possible, but that doesn’t mean it’s the most organized. If the intention is only to solve the problem, you can enter this code, if you want to do the right thing, the manipulation would be completely separate and the parties would only communicate, each with its own responsibility.

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

Precisely because it has some bad things it will be a little more difficult to integrate the manipulation of the file, either together or separately. The logic used works very specifically. It is 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 to only record or manipulate all the data in the file? If it’s just recording, it’s much easier because you can keep the logic, even if it’s bad, and just create routines that record and read the data from the file and play in memory, then the bulk of your code doesn’t even need to know that there’s something in the file. You can put option to read and record or you can leave automatic, IE, read everything and play on array when it starts and every time it touches array records everything back into the file. This is inefficient and may bring problems on a large scale, but for an exercise it may be an alternative as it will not manipulate millions of data.

Just don’t think all this will always be like this, this is an extremely simplified way, in real codes, a lot of things need to be different.

Documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.