-1
I have a code on main that works normally. This code says that the user must enter the data and asks the user to enter the name of the employee and the following instruction starts object I1. The I1 object calls its constructor and the rest of the code follows until the program (in the main) asks the user to enter the employee’s CPF and the following instruction starts the I1 object. What I don’t understand is what happens when I change the main code. When I enter 4 lines before the line that says the user should inform the data, requesting the user to inform how many employees will be registered, I declare a variable amount, read the user-informed quantity variable and print the value of the quantity variable, the program arrives to print that the user must inform the data and asks the user to enter the name of the employee but then the cursor already goes to the line where the user must inform the CPF not allowing him to enter the name of the employee. Why does this happen?
//código da main que não está funcionando corretamente
int main()
{
ofstream saidaArquivoCadastro( "cadastro.dat", ios::out );
if ( !saidaArquivoCadastro ) // operador ! sobrecarregado
{
cerr << "Arquivo nao pode ser aberto" << endl;
exit( 1 );
}
cout << "Informe a quantidade de funcionarios a serem cadastrados: ";
int quantidade;
cin >> quantidade;
cout << quantidade;
cout << "\nInforme os dados: \n\n";
cout << "Nome do funcionario: ";
info i1;
cout << "\nCPF: ";
info i2;
//código da main que está funcionando corretamente
int main()
{
ofstream saidaArquivoCadastro( "cadastro.dat", ios::out );
if ( !saidaArquivoCadastro ) // operador ! sobrecarregado
{
cerr << "Arquivo nao pode ser aberto" << endl;
exit( 1 );
}
cout << "\nInforme os dados: \n\n";
cout << "Nome do funcionario: ";
info i1;
cout << "\nCPF: ";
info i2;
//descrição da classe info
#include <string>
using std::string;
#ifndef INFO_H
#define INFO_H
class info
{
public:
info (string = "");
void setInfoDado (string);
void setInfo ();
void print () const;
private:
string infoDado;
};
#endif
//descrição dos métodos info
#include <iostream>
using std::cout;
using std::cin;
#include <string>
using std::string;
using std::getline;
#include "info.h"
info::info (string info)
{
setInfoDado (info);
}
void info::setInfoDado (string info)
{
infoDado = info;
setInfo();
}
void info::setInfo ()
{
string nome;
getline (cin, nome);
infoDado = nome;
}
void info::print () const
{
cout << infoDado << "\n";
}
I noticed that this problem only happens when I put the Cin >quantity; instruction. When I do not put this statement the cursor stops at Cout << "Employee name: "; so that I can type the user name (since when instantiating the I1 object the method of the class info void info::setInfo () causes the program to wait for the user to type the name). When I place the Cin >> quantity statement; something causes that method or assignment statement in the scope of that method to not be executed. The interesting thing is that when the I2 object is instantiated the code works normally (with or without the Cin >> quantity instruction; before the I1 object).
Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.
– Bacco