0
I have a little problem when I try to pass a string to a class. In short: in the main program I will read this string and move to the class:
cout << "----- Vamos as opcoes! -----" << endl;
cout << "(1) - Adicionar um funcionario" << endl;
.
.
.
cin >> opcao;
switch(opcao){
case 1: //Adicionar um funcionario
Funcionario funcionario_novo;
empresa1.adicionaFuncionario(funcionario_novo);
}
I have a class called Employee, which contains his name, salary and date of admission.
void
Empresa::adicionaFuncionario(Funcionario funcionario_novo){
m_totalFuncionarios++;
string _nome_, _data_;
float _salario_;
cout << "Nome: " << endl;
getline(cin,_nome_);
funcionario_novo.setNome(_nome_);
cout << "Salario: " << endl;
cin >> _salario_;
funcionario_novo.setSalario(_salario_);
cout << "Data de admissao: " << endl;
getline(cin,_data_);
funcionario_novo.setData(_data_);
m_funcionarios[m_totalFuncionarios] = funcionario_novo; //Guardo meu funcionário novo no vetor
}
When I spin, it works up m_totalFuncionarios++;
, but does not pass these strings to the employee.
void
Funcionario::setNome(const string nome_){
m_nome = nome_;
}
void
Funcionario::setSalario(float salario_){
m_salario = salario_;
}
You should keep it, but you don’t keep it.
My question is: what would be the best way to pass this string to class?
How do you know you didn’t keep the value? Have you checked if it wouldn’t be the function
getline
you’re not doing the job you should be doing?– Jefferson Quesado
Actually I can’t read. When I spin I don’t have the option to write, it jumps. I used separate getline and it worked.
– Patricia Fernandes
So I didn’t understand your problem or your solution o.O But I’m glad you solved it
– Jefferson Quesado
It didn’t work. I used it separately to be if it worked with other things, and it works. But in this case it didn’t. I wish I could submit the whole code, but it would be too big. I think I’ll delete, I won’t be able to show the error.
– Patricia Fernandes
Have you tried with
cin.ignore();
before thegetline
and after reading withcin>>
? I start by saying that these two forms of reading don’t mix very well– Isac