getline problem in c ++

0

I have a problem with my code, what happens is that when I compile it does not send me any error, but in this part of my code, the gertLine I use to enter the values (USERNAME, MAIL, PASSWORD AND NAME) , the first Characters or numbers do not capture me in the file and in the password option it is skipped, my code is like this:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>


using namespace std;
//DEFINIENDO LAS VARIABLES NECESARIAS
string nombre, auxnombre, username, correo, contrasena, usernameaux;
char opca;
bool encontrado = false;


void altas()
{
    ///Variables de la biblioteca fstream para el manejo de archivos
    ofstream escritura;
    ifstream consulta;
    do{
        escritura.open("usuarios.txt", ios::out | ios::app);//crea y escribe, si ya tiene texto une al final del archivo
        consulta.open("usuarios.txt", ios::in);//solamente consulta o lee usando la variable sobre el archivo físico alumnos.txt

        if (escritura.is_open() && consulta.is_open()){


            bool repetido=false;

            cout<<"\n";
            cout<<"\tIngresa el username del usuario:    ";
            getline (cin, usernameaux);
            cin.ignore();

            ///A continuación se aplica el tipo de lectura de archivos secuencial
            consulta>>username;
            while (!consulta.eof()){
                consulta>>nombre>>username>>correo>>contrasena;
                if (usernameaux==username){
                    cout<<"\t\tYa existe ese username...\n";
                    repetido=true;
                    break;
                }
                consulta>>username;
            }

            if (repetido==false){
                cout<<"\tIngresa tu nombre:   ";
                getline(cin,nombre);
                cin.ignore();
                cout<<"\tIngresa tu correo: ";
                getline(cin,correo);
                cin.ignore();
                cout<<"\tIngresa tu contraseña:    ";
                getline(cin,contrasena,'\n');




                //ESCRIBIENDO LOS DATOS CAPTURADOS POR EL USUARIO EN EL ARCHIVO
                escritura<<usernameaux<<" "<<nombre<<" "<<correo<<" "<<contrasena<<" "<<endl;

                cout<<"\n\tRegistro agregado...\n";

            }

            cout<<"\n\tDeseas ingresar otro username? (S/N): ";
            cin>>opca;


        }else{
            cout<<"El archivo no se pudo abrir \n";
        }

        escritura.close();
        consulta.close();

    }while (opca=='S' or opca=='s');

}
    
asked by antonio 02.09.2018 в 01:31
source

1 answer

1

You must remove the cin.ignore() command from your code since it deletes or ignores the characters you have previously entered.

Based on this documentation: methods cin c ++

    
answered by 02.09.2018 в 04:24