Code does not execute instruction correctly in C++

Asked

Viewed 49 times

-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.

1 answer

0

I have a code on main that works normally.

Maybe it doesn’t work that normally.

Why does this happen?

The short answer is that you missed the statement of the builders of info and familia and invalidated what you might think was the default constructor for a class that only has one variable.

This code says that the user must inform the data and asks that the user enter the employee name and the following instruction starts the object I1. Object I1 calls its constructor and the rest of the code follow until the program (in main) request the user to enter the CPF of official and the next instruction starts object I1

What you wrote doesn’t make much sense. In practice

cout << "Nome do funcionario: ";
info i1;
cout << "\nCPF: ";
info i2;

you wrote those cout as prompts to guide the reading that happens within the constructor, after you call 3 (!) functions. It is not a good idea at all. For someone (including yourself in a day or two) to understand what the program is doing would have to have the constructor code available at all times. It makes no sense. The message should be next to the reading or passed as parameter.

The variable statement is that it triggers the construction of it. Only it misspelled. Probably intended to write this

    familia() : Dado("") {};

Setting the default constructor and placing a "" in the only variable of its class. And it would have worked.

Only you wrote that

class info
{
    public:
        info (string = "");
        void setInfoDado (string);
        void setInfo ();
        void print () const;
    private:
        string infoDado;
};

And ended up creating a meaningless constructor that has a parameter string that nothing means.

I could use something simple like

class info
{
public:

    info() : Dado(""){};
    info(string prompt) { setDado(prompt); };
    void setDado(string);
    void print() const;

private:

    string Dado;
};

and setDado() would receive the message each time:

void info::setDado(string prompt)
{
    cout << prompt << ": ";
    getline(cin, Dado);
}

And used just like that:

    info i1("Nome");
    info i2("CPF");
    i1.print();
    i2.print();

Browser other questions tagged

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