Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- using namespace std;
- int INDICE = 0;
- int CAPACIDADE = 5;
- struct Contato {
- char nome[60];
- char telefone[15];
- char email[32];
- };
- void insereContatoNaAgenda(Contato novaContato, Contato *&agenda){
- agenda[INDICE] = novaContato;
- INDICE++;
- if (INDICE >= CAPACIDADE) {
- CAPACIDADE*=2;
- agenda = (Contato *) realloc(agenda, sizeof(Contato)*CAPACIDADE);
- }
- }
- void inicializarAgenda(Contato *&agenda){
- agenda = (Contato *) malloc(sizeof(Contato)*CAPACIDADE);
- }
- Contato criaNovoContato(){
- Contato contato;
- cout << "Nome: ";
- cin.ignore();
- cin.getline(contato.nome,sizeof(contato.nome));
- cout << "Telefone: ";
- cin.ignore();
- cin.getline(contato.telefone,sizeof(contato.telefone));
- cout << "E-Mail: ";
- cin >> contato.email;
- return contato;
- }
- void imprimeAgenda(Contato* agenda) {
- for (int i=0; i < INDICE; i++) {
- cout << "Entrada " << i << ":" << endl;
- cout << " Nome: " << agenda[i].nome << endl;
- cout << " Telefone: " << agenda[i].telefone << endl;
- cout << " E-Mail: " << agenda[i].email << endl;
- }
- }
- void imprimeMenu() {
- system("cls");
- cout << R"( _ __
- / \ | ]
- / _ \ .--./) .---. _ .--. .--.| | ,--. 1.0
- / ___ \ / /'`\;/ /__\\[ `.-. |/ /'`\' | `'_\ :
- _/ / \ \_ \ \._//| \__., | | | || \__/ | // | |,
- |____| |____|.',__` '.__.'[___||__]'.__.;__]\'-;__/
- ( ( __)) )" << endl << endl << flush;
- cout << "Menu:" << endl;
- cout << "1 - Inserir contato" << endl;
- cout << "2 - Listar contatos" << endl;
- cout << "3 - Estado do array" << endl;
- cout << "0 - Sair do programa" << endl;
- }
- int main()
- {
- char menu = ' ';
- Contato *agenda = NULL;
- inicializarAgenda(agenda);
- while (menu != 0) {
- imprimeMenu();
- cin >> menu;
- switch (menu) {
- case '0':
- break;
- case '1': {
- Contato novoContato = criaNovoContato();
- insereContatoNaAgenda(novoContato, agenda);
- break;
- }
- case '2':
- imprimeAgenda(agenda);
- break;
- case '3':
- cout << "Elementos inseridos: " << INDICE << endl;
- cout << "Capacidade do array: " << CAPACIDADE << endl;
- break;
- default:
- cout << "Opção inválida!" << endl;
- break;
- }
- }
- cout << agenda[0].nome;
- free(agenda);
- return 0;
- }
Add Comment
Please, Sign In to add comment