Read and print struct values

Asked

Viewed 246 times

0

I have an algorithm that reads name, sector and number of 10 employees, I’m trying to read in a function but I’m not succeeding, when I try to print only out numbers. This is a piece of content that I didn’t understand at all, so it may be that they have blatant errors.

#include <bits/stdc++.h>

using namespace std;

typedef struct
{
string nome;
string setor;
int cartao;
}funcionario;


void inserir(funcionario func)
{
cin >> func.nome;
cin >> func.setor;
cin >> func.cartao; 
}

int main()
{
setlocale(LC_ALL,"Portuguese");
funcionario func[10];
int opcao=0, p=0;

do{
    cout << "O que você deseja fazer?\nDigite \"1\" para inserir.\nDigite\"2\" para buscar.\n";
    cin >> opcao;

    if(opcao != 1 && opcao != 2){
        cout << "Valor digitado inválido, digite novamente.\n";
    }
    else if(opcao == 1){
        inserir(func[p]);
        p++;
    }

  }while(p < 10);
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

1

At no time does this code attempt to print something. Its main problem is that it is not passing the element by reference, then when you finish executing the insert function the data is lost. I strongly suggest study the subject before continuing, because I gave the solution, but without understanding the whole functioning of the C++ memory model will only be solving the problem without knowing how to apply again. See more.

I improved a few more things, mainly because it is programming in C++ and not in C, but I didn’t improve everything, I also recommend seeing the idiomatic form for do the code in C++ and do not use C.

Logic can also be improved.

#include <iostream>
using namespace std;

struct Funcionario {
    string nome;
    string setor;
    int cartao;
};

void inserir(Funcionario& func) {
    cin >> func.nome;
    cin >> func.setor;
    cin >> func.cartao; 
}

int main() {
    setlocale(LC_ALL,"Portuguese");
    Funcionario funcionarios[10];
    int p = 0;
    do {
        cout << "O que você deseja fazer?\nDigite \"1\" para inserir.\nDigite\"2\" para buscar.\n";
        int opcao = 0;
        cin >> opcao;
        if (opcao != 1 && opcao != 2) cout << "Valor digitado inválido, digite novamente.\n";
        else if (opcao == 1) {
            inserir(funcionarios[p]);
            p++;
        }
    } while (p < 10);
}

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

0

In the "insert" function declaration the "employee" parameter needs to be declared "by reference" with a "&" next to the employee type:

void inserir(funcionario& func) // <--------- parametro por referencia
{
  cin >> func.nome;
  cin >> func.setor;
  cin >> func.cartao; 
}

This way the function works with the original "func" object. Without the "&" the function receives (attention here) a copy of the original object, then everything that is changed in the object within the function is lost when the function returns, because the copy object used by the function is destroyed in the return of the function.

0

You can implement a registration function that is able to register an amount n of employees, check it out:

void inserir( funcionario * func, int qtd )
{
    for( int i = 0; i < qtd; i++ )
    {
        cout << "Cadastro do funcionario (id=" << i+1 << "):" << endl;
        cout << "  Nome: ";
        cin >> func[i].nome;
        cout << "  Setor: ";
        cin >> func[i].setor;
        cout << "  Cartao: ";
        cin >> func[i].cartao;
        cout << endl;
    }
}

Put it all together according to your thinking, we’ll have something like:

#include <iostream>
#include <string>

using namespace std;

typedef struct
{
    string nome;
    string setor;
    int cartao;
} funcionario;


void inserir( funcionario * func, int qtd )
{
    for( int i = 0; i < qtd; i++ )
    {
        cout << "Cadastro do funcionario (id=" << i+1 << "):" << endl;
        cout << "  Nome: ";
        cin >> func[i].nome;
        cout << "  Setor: ";
        cin >> func[i].setor;
        cout << "  Cartao: ";
        cin >> func[i].cartao;
        cout << endl;
    }
}

int main()
{
    setlocale(LC_ALL,"Portuguese");
    funcionario func[10];
    int opcao=0;

    while(1) {
        cout << "Selecione uma opcao:" << endl;
        cout << "1) Inserir." << endl;
        cout << "2) Buscar." << endl;
        cout << "> ";
        cin >> opcao;

        if(opcao == 1){
            inserir( func, 10 );
        }
        else if(opcao == 2){
        }
        else {
            cout << "Valor digitado inválido, digite novamente.\n";
        }

    }

    return 0;
}

Browser other questions tagged

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