Because the line is skipped - cout "Registration:"; cin.getline (students [i] .matricula, 15); - and he does not ask me for the registration? [duplicate]

0
#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

struct alumnos {
    char matricula[15];
    double calificaciones;
}alumnos[35];


int main(){
    int n_alumnos; char resp; double prom_alumnos[35];
    do {
        cout << "Cuantos # numeros de alumnos desea ingresar: "; cin >> n_alumnos;
        for( int i = 0; i < n_alumnos; i++) {
            cout << "Matricula: "; cin.getline(alumnos[i].matricula, 15);
            for( int j = 0, x = 1; j < 5; j++, x++) {
                cout << "\n\tCalificacion " << x << ": "; cin >> alumnos[j].calificaciones;
                prom_alumnos[i] += alumnos[j].calificaciones;
            }
        }

        for (int i = 0; i < n_alumnos; i++ ) {
            cout << "\n------------------";
            cout << "\nMatricula: " << alumnos[i].matricula;
            cout << "\nPromedio: "<<  prom_alumnos[i]/5;
        }

    getch();
    return 0;
}
    
asked by Jonathan jesus 10.10.2018 в 00:15
source

2 answers

0

Taking already a previous answer that already had given in Data entry via std :: getline ...

The error occurs when you read before with cin >> variable

How to fix it?

setting cin.ignore(); before reading with getline , your code would be:

cin.ignore(); //despues de leer con cualquier cin >> variable

cout << "Digita tal cosa" << endl;
getline(cin,nombre);
cout << "Digita otra cosa" << endl;
getline(cin,apellido);
    
answered by 10.10.2018 / 01:07
source
0

This happens because the read buffer is filled with the \n character corresponding to the line break. Then, when doing the second reading, the cin reads the \n that is in the buffer and interprets that as the input.

    
answered by 10.10.2018 в 00:59