C ++ error when making an input with cin or getline (std :: cin, var) returns a loop when writing with spaces or a v

0

When requesting data and placing a space, when using cin > > temp.name ;, it gives me an error that causes a loop of the 2 while at the same time or, when using getline (std: cin, temp.name);, the question jumps to me. The code is as follows.

#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <fstream>
#include <string> 
using namespace std;


struct temporal {
    string nombre;
    long edad;
    int conf;
    int num;
    int num2;
    int num3;
    int num4;
    int num5;
    int num6;
    int num7;
    int num8;
};
temporal temp;


int opt;
int op;
int id;
string nombre[100];
long edad[100];
int nDatos=-1;
int actDatos = -1;
int insertOn;
int actB;
int inported;
int lines;



int main() {
            cout << "Cuantas personas queres agregar:  "; cin >> temp.num;
            actB = nDatos;
            temp.num2 = temp.num;
            while(temp.num >= 1){
                if(op == 1) {
                    cout << "Introduce un nombre:  ";
                      std::getline( std::cin, temp.nombre );//o al utilizar cin >> temp.nombre;
                    cout << "Introduce una edad:  "; 
                    cin >> temp.edad;
                    actDatos ++;
                    temp.num --;
                    nDatos = actDatos;
                    insertOn = nDatos;
                    nombre[insertOn] = temp.nombre;
                    edad[insertOn] = temp.edad;
                    clear();        
                }
            }
                clear();
                cout << "Has agregado a estas persona(s)" << endl;
                while(actB +1 <= nDatos){
                actB = actB + 1;
                cout << nombre[actB] << "          " << edad[actB] << endl;

                }
                op = 0;
        }
    
asked by 15Beginer 12.12.2018 в 19:15
source

1 answer

1

Ok I solved it, the mistake was as simple as putting:

getline( cin.ignore(100, '\n'), temp.nombre);
    
answered by 12.12.2018 в 19:51