How could I enter the structs in a list?

Asked

Viewed 257 times

0

#include "stdafx.h"
#include <iostream>

using namespace std;

struct FICHA_INSCRICAO
{
    char nome[50];
    char cpf[10];
    char logradouro[100];
    char bairro[20];
    char cidade[20];
    char estado[1];
    char email[50];
    char telefone[10];
    double salario_familiar = 0;
    int pessoas_quant = 0;
    double renda_percapita = 0;

    void INSERE() {

        cout << "Nome: ";
        cin >> nome;
        cout << "CPF: ";
        cin >> cpf;
        cout << "Logradouro: ";
        cin >> logradouro;
        cout << "Bairro: ";
        cin >> bairro;
        cout << "Cidade: ";
        cin >> cidade;
        cout << "Estado: ";
        cin >> estado;
        cout << "Email: ";
        cin >> email;
        cout << "Telefone: ";
        cin >> telefone;
        cout << "Salario Total da Familia: ";
        cin >> salario_familiar;
        cout << "Quantidade de pessoas na sua casa: ";
        cin >> pessoas_quant;

        renda_percapita = salario_familiar / pessoas_quant;
    }
};

int main()
{
    int op = 1, tamanho = 0;
    FICHA_INSCRICAO *ficha = new FICHA_INSCRICAO[tamanho];

    while (op == 1)
    {
        ficha[tamanho].INSERE();
        tamanho++;

        cout << "\nNovo cadastro?\n"
            << "1 - SIM\n"
            << "0 - NAO\n";
        cin >> op;
    }

    system("pause");
    return 0;
}
  • What kind of list? What’s your problem? Do you need to do it using C features? You can’t just do it using what’s specific to C++?

  • Only in c++, I want to assign the struct to a sequential linear list, organized by names in alphabetical order.

  • From what you’re saying I think you should wear one map.

2 answers

4

I will redo the code to C++ without using the C style.

The appropriate is to use a map, Which was described as a requirement. If the requirement is wrong there the solution would be another.

I switched to class because it is more idiomatic for this type of use in C++. I used string in place of array of char which is a C thing. I used better names.

I don’t like this one Inserir() in class, but for an exercise is good. Also shouldn’t wear double for monetary value.

Have other small problems that are not serious for an exercise.

I had it printed at the end and tested with data out of order to show that it is in alphabetical order.

There is probably a more complex solution that is better than this which has its own difficulties.

#include <iostream>
#include <string>
#include <map>
using namespace std;

class FichaInscricao {
public:
    string Nome;
    string Cpf;
    string Logradouro;
    string Bairro;
    string Cidade;
    string Estado;
    string Email;
    string Telefone;
    double SalarioFamiliar = 0;
    int QuantidadePessoas = 0;
    double RendaPerCapita = 0;

    void Inserir() {
        cout << "Nome: ";
        cin >> Nome;
        cout << "CPF: ";
        cin >> Cpf;
        cout << "Logradouro: ";
        cin >> Logradouro;
        cout << "Bairro: ";
        cin >> Bairro;
        cout << "Cidade: ";
        cin >> Cidade;
        cout << "Estado: ";
        cin >> Estado;
        cout << "Email: ";
        cin >> Email;
        cout << "Telefone: ";
        cin >> Telefone;
        cout << "Salario Total da Familia: ";
        cin >> SalarioFamiliar;
        cout << "Quantidade de pessoas na sua casa: ";
        cin >> QuantidadePessoas;
        RendaPerCapita = SalarioFamiliar / QuantidadePessoas;
    }
};

int main() {
    map<string, FichaInscricao> fichario;
    int op = 1;
    while (op == 1)  {
        FichaInscricao ficha;
        ficha.Inserir();
        fichario.emplace(ficha.Nome, ficha);
        cout << "\nNovo cadastro?\n"
             << "1 - SIM\n"
             << "0 - NAO\n";
        cin >> op;
    }
    for (const auto &ficha : fichario) std::cout << ficha.first << " => " << ficha.second.Cpf << '\n';
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thank you very much staff I will write down the tips and study the map and the smart Pointer still did not know the two, helped a lot.

2


Just to complement, this is the same source created by @Maniero, but with two things I think are cool: smart pointers, and use of "move" operation instead of copying the object.

#include <iostream>
#include <map>
#include <memory>
#include <string>

using namespace std;

class FichaInscricao
{
   public:
      string Nome;
      string Cpf;
      string Logradouro;
      string Bairro;
      string Cidade;
      string Estado;
      string Email;
      string Telefone;
      double SalarioFamiliar = 0;
      int QuantidadePessoas = 0;
      double RendaPerCapita = 0;
};

int main()
{
   // mapa de smart pointers
   map<string, unique_ptr<FichaInscricao>> fichario;

   int op;

   do
   {
      // cria no heap uma instancia da classe FichaInscricao,
      // e guarda o endereco em um smart pointer
      auto ficha = make_unique<FichaInscricao>(); // COOL!
      ficha->Inserir();

      // move o endereco de dentro do smart pointer para dentro do mapa
      fichario[ficha->Nome] = std::move(ficha); // COOL!

      cout << "\nNovo cadastro?\n"
           << "1 - SIM\n"
           << "0 - NAO\n";
      cin >> op;
   } while (op == 1);

   for (const auto& ficha: fichario)
   {
      cout << ficha.first << " => " << ficha.second->Cpf << '\n';
   }

}

void FichaInscricao::Inserir()
{
   cout << "Nome: ";
   cin >> Nome;
   cout << "CPF: ";
   cin >> Cpf;
   cout << "Logradouro: ";
   cin >> Logradouro;
   cout << "Bairro: ";
   cin >> Bairro;
   cout << "Cidade: ";
   cin >> Cidade;
   cout << "Estado: ";
   cin >> Estado;
   cout << "Email: ";
   cin >> Email;
   cout << "Telefone: ";
   cin >> Telefone;
   cout << "Salario Total da Familia: ";
   cin >> SalarioFamiliar;
   cout << "Quantidade de pessoas na sua casa: ";
   cin >> QuantidadePessoas;
   RendaPerCapita = SalarioFamiliar / QuantidadePessoas;
}

Browser other questions tagged

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