In progress

Arquivos C+

Published on the July 01, 2017 in IT & Programming

About this project

Open

Adicione a opcão ao programa de gravar um novo contato telefônico na agenda, inclusive atualizando o arquivo agenda.txt


#include <iostream>
#include <string>
#include <fstream>
#define MAX_AGENDA 50

using namespace std;

struct Contato{
    string nome;
    int telefone;
    };

//funcao que busca nome na agenda e retorna numero de telefone
//se nao encontrar o nome, retorna -1
int buscaNumero(Contato lista[], int n, string nome){
    for (int i = 0; i < n; i++) {
        if(nome == lista[i].nome)
            return lista[i].telefone; //encerra imediatamente e retorna numero
    }
    return -1; //encerrou laco sem encontrar -> retorna -1
}

int main(){

    Contato agenda[MAX_AGENDA];
    int i,n, resultado;
    string buscar;
    ifstream db;
    
    db.open("agenda.txt");
        
    n = 0; //leitura do numero de contatos incluso na agenda
    if(db.is_open()){    
        while(!db.eof()){ //leitura do nome do i-esimo contato
            getline(db, agenda[n].nome);
            db >> agenda[n].telefone; //leitura do telefone do i-esimo contato
            db.ignore(); //ignore \n logo apos o numero de telefone
            n++;
        }
    }
    else
        return 1;

    cout << "Digite o nome a pesquisar ou s para sair: " << endl;
    getline(cin,buscar); //leitura do nome a ser procurado na agenda
    while(buscar!="s"){
        resultado  = buscaNumero(agenda, n, buscar);
        if(resultado<0)
            cout << "Contato nao encontrado\n";
        else
            cout << "Telefone: " << resultado << endl;
        cout << "\nDigite o nome a pesquisar ou s para sair: ";
        getline(cin, buscar); //leitura do nome a ser procurado na agenda
    }
    db.close();
    return 0;
}

Category IT & Programming
Subcategory Web development
What is the scope of the project? Medium-sized change
Is this a project or a position? Project
I currently have I have specifications
Required availability As needed
Experience in this type of projects Yes (I have managed this kind of project before)
Roles needed Other
Other roles needed Adicionar uma pequena opção num programa C++ de implementação simples de Arquivos

Delivery term: Not specified

Skills needed

C++

Other projects posted by H. L.