Why does not my first cin.getline ();

0

I have the doubt because in my code the first cin.getline(); does not work, it is skipped, but the others do function.

#include <iostream>
#include <string>
#include <stdlib.h>
#include <new>
#include <ctype.h>

using namespace std;

struct Data {
    string ClientNo;
    string Name;
    string LastName1;
    string LastName2;
    string Street;
    int StreetAd;
    int ApartmentNo;
    string ZipCode;
    string FederalEntity;
    string Muni;
    string PhoneNo;
    string OCP;
    char Email[50];
    double ToPay;
    double debt;
};
int main()
{
    system("COLOR 3F");
    Data* DynArray;
    DynArray = new (nothrow) Data[50];
    int a, b=0;
    string d;
    bool an;
    char q[20],a0[20],a1[20],a2[20],a3[20],a4[20],a5[50],r;
   Muni m1;
    cout << "***Alta de Clientes***" << endl << endl << endl << endl;
    do
                {
    cout<<"******Bienvenido al menu******"<<endl;
    cout<<"Opcion 1: agregar cliente"<<endl;
    cout<<"Opcion 2: Mostrar Clientes del alma"<<endl;
        cin >> a;
        switch (a)
        {
        case 1:
           do
              {*/
                cout<<endl<<"***Solo numeros***";
                cout <<endl<< "Numero de Cliente: ";
                cin >> DynArray[b].ClientNo;
                cout << endl << "Nombre de Cliente: ";
                cin.getline(a0,10);
                string str0(a0);
                cout<<str0<<" "<<a0;
                DynArray[b].Name=str0;
                cout << endl << "Apellido Paterno: ";
                cin.getline(a1,20);
                string str1(a1);
                DynArray[b].LastName1=str1;
                cout << endl << "Apellido Materno: ";
                cin.getline(a2,20);
                string str2(a2);
                DynArray[b].LastName2=str2;
                cout << endl << "Calle: ";
                cin.getline(a3,20);
                string str3(a3);
                DynArray[b].Street=str3;
                cout << endl << "Numero Exterior: ";
                cin >> DynArray[b].StreetAd;
                cout << endl << "Numero Interior: ";
                cin >> DynArray[b].ApartmentNo;
                cout << endl << "Codigo Postal: ";
                cin >> DynArray[b].ZipCode;
                for(a;an==false;a+0){
                cout<<"***SOLO D.F., Estado de Mexico, e Hidalgo";
                cout << endl << "Entidad Federativa: ";
                cin.getline(a4,20);
                string str4(a4);
                DynArray[b].FederalEntity=str4;
                }}

The for is like this because with a while , or a do while I never closed the loop

    
asked by Dante Torobolino 07.08.2017 в 00:34
source

1 answer

1
cin >> DynArray[b].ClientNo;
// ...
cin.getline(a0,10);

The problem , to call it in some way, arises because the previous reading is reading an integer ... and this reading does not eliminate the final line break ... so, when you call getline this is a line break as the first character and there ends of reading.

To avoid this problem you can simply discard the first character of the buffer:

cin >> DynArray[b].ClientNo;
cin.ignore();
// ...
cin.getline(a0,10);
    
answered by 07.08.2017 в 09:10