0

This is my funcoes.h where i keep my fuctions, my problem is at this line cout << "Digite o tipo de EPI a ser cadastrado: \n"; My program just skips the next getline(); and i cannot read user input. looks like this

The next getline() is working fine, so i've no ideia where the problem is.

#ifndef FUNCOES_H_INCLUDED
#define FUNCOES_H_INCLUDED
#include "Estruturas.h"
#include <string.h>

using namespace std;

void cadastroInsumos(tVacina vacinas[], tMedicamento medicamento[], tEPI EPIs[])
{
    int casee, i;

    cout << "Qual insumo voce deseja cadastrar?\n" << endl;
    cout << "[1] - Vacina" << endl;
    cout << "[2] - Medicamento" << endl;
    cout << "[3] - EPI" << endl;
    cout << "-----------------------------------------" << endl;

    cin >> casee;

    system("CLS");

    if (casee == 3)
    {
        tEPI epi;
        cout << "Digite o tipo de EPI a ser cadastrado: \n";
        getline(cin, epi.tipo);
        cout << "Digite o nome do EPI a ser cadastrado: \n";
        getline(cin, epi.dadosEPI.nome);
        cout << "Digite o valor unitario: \n";
        cin >> epi.dadosEPI.valorUnitario;
        cout << "Digite a quantidade de itens: \n";
        cin >> epi.dadosEPI.qItens;
        cout << "Digite a data de vencimento: \n";
        cin >> epi.dadosEPI.dataVencimento.dia;
        cin >> epi.dadosEPI.dataVencimento.mes;
        cin >> epi.dadosEPI.dataVencimento.ano;
        cout << "Digite o nome do fabricante: \n";
        cin >> epi.dadosEPI.nomeFabricante;
        cout << "Digite informacoes detalhadas sobre o EPIs: \n";
        cin >> epi.detalhesEPI;

        EPIs[1] = epi;

        system("CLS");
    }
}

/*void criarVacinas(tVacina vacinas[])
{

}

void criarMedicamentos(tMedicamento medicamento[])
{

}

void criarEPIs(tEPI EPIs[])
{

}*/

#endif // FUNCOES_H_INCLUDED

My main looks like this

#include <iostream>
#include <string>
#include "Estruturas.h"
#include "Funcoes.h"

using namespace std;

int main()
{
    int casee;
    tVacina vacinas[10];
    tMedicamento medicamentos[10];
    tEPI EPIs[10];

    cout << "------UFPB 2021 - ENGENHARIA DE COMPUTACAO - PROJETO POO------" << endl;
    cout << "Grupo: Leonardo Chianca, Savio Nazario e Yuri Fernandes\n" << endl;
    cout << "------Bem-vindo ao Sistema de Gerenciamento de insumos------\n" << endl;

    while(true)
    {
        cout << "Digite o numero correspondente a operacao que deseja executar: \n" << endl;

        cout << "[0] - Fechar Sistema" << endl;
        cout << "[1] - Cadastro de Insumos no estoque do MS" << endl; //vacinas, medicamentos e EPIs
        cout << "[2] - Consulta de Insumos disponiveis no estoque do MS" << endl;
        cout << "[3] - Consulta da descricao de Insumos disponiveis no estoque do MS" << endl; //Informacoes sobre seus atributos
        cout << "[4] - Consulta de Insumos disponiveis no estoque do MS por tipo" << endl; //vacina, medicamentos e EPIs
        cout << "[5] - Consulta de Insumos distribuidos para os estados" << endl;
        cout << "[6] - Consulta da descricao de Insumos disponiveis nos Estados" << endl; //Informacoes sobre seus atributos
        cout << "[7] - Consulta de Insumos disponiveis no estoque dos Estados por tipo" << endl; //Vacinas, medicamentos e EPIs
        cout << "[8] - Consulta de Insumos disponiveis no estoque por Estado" << endl; //Estado passado como paramentro
        cout << "[9] - Distribuir Insumos entre Estados\n" << endl; //O parametro deve ser o estado + o tipo de insumo que saira do estoque do MS
        cout << "---------------------------------------------------------------------------" << endl;

        cin >> casee;

        system("CLS");

        switch(casee)
        {
            default: return 0;
                break;

            case 1: cadastroInsumos(vacinas, medicamentos, EPIs);
                break;

            case 2:
                break;

            case 3:
                break;

            case 4:
                break;

            case 5:
                break;

            case 6:
                break;

            case 7:
                break;

            case 8:
                break;

            case 9:
                break;
        }

    }
}
  • 2
    See this: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) . (fyi, found literally instantly by searching for `[cpp] getline skipped` in any search box on this site. – WhozCraig Apr 03 '21 at 05:51
  • thank you, gonna have a look. and sorry for that – yuri fernandes Apr 03 '21 at 05:56
  • Nothing to be sorry for, just telling you how I found it. The search features of this site are (a) amazing, and unfortunately (b) heavily under utilized. Knowing some simple ways to target your searches helps. The language tag in `[ ]` followed by simple one word terms of your problem is a pretty good way to get there. Hope it helped. – WhozCraig Apr 03 '21 at 05:59
  • thank you for the help, already solved my problem. – yuri fernandes Apr 03 '21 at 06:03
  • Hopefully with [std::basic_istream::ignore](https://en.cppreference.com/w/cpp/io/basic_istream/ignore)? – David C. Rankin Apr 03 '21 at 08:48
  • yep, i used getline(cin.ignore(), ) – yuri fernandes Apr 07 '21 at 17:48

1 Answers1

1

It is caused because of this line:

cin >> casee;

This line reads a number into the variable "casee".

However, the newline '\n' entered after the number is not read.

Then, the pending newline is read by the following getline:

getline(cin, epi.tipo);

Then, epi.tipo is assigned a "\n" value.

Nimantha
  • 4,731
  • 5
  • 15
  • 38