I am trying to read a .txt file using C ++, in which the user enters (in this case a "user" and a "pass") two data, and when you compare them, if both, print on the screen a type "Login correct". The code is as follows:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
ifstream archivo("user.txt");
//Obtencion de datos
cout << "Usuario: ";
char user[25];
cin.getline(user,25,'\n');
cout << "Pass: ";
char pass[25];
cin.getline(pass,25,'\n');
//Creacion de la linea a buscar
char busqueda[100];
strcpy(busqueda, user);
strcat(busqueda, " ");
strcat(busqueda, pass);
char linea[100];
while(!archivo.eof() || busqueda != linea){
for(int i = 0; i!=archivo.eof(); i++){
archivo.getline(linea, 100);
}
}
cout << endl << endl << "LOGIN CORRECTO." << endl << linea;
return 0;
}
The problem is that once you enter the two data, it stays in an infinite loop in which it does nothing. You can be giving intro all the time that does not come out of there ...
Doing more tests, I have reached the following code. The only thing is that it only compares with the first line of the file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct registro{
string name;
string second;
string street;
string user;
string pass;
} usuario;
void registroUsuario(){
ofstream archivo("registro.txt");
archivo << "----------------------" << endl;
archivo << "Nombre: " << usuario.name << endl;
archivo << "Apellido: " << usuario.second << endl;
archivo << "Direccion: " << usuario.street << endl;
archivo << "Usuario: " << usuario.user << endl;
archivo << "Password: " << usuario.pass << endl;
archivo.close();
}
void registroLogin(){
ofstream archivo("usuarios.txt");
archivo << usuario.user;
archivo << " ";
archivo << usuario.pass;
archivo << "\n";
archivo.close();
}
bool login(string usuario, string password){
ifstream archivo("usuarios.txt");
string busqueda = usuario + " " + password;
string linea;
while(!archivo.eof()){
getline(archivo, linea);
if(linea == busqueda){
return true;
}
else{
return false;
}
}
}
int main(){
menu:
cout << "\t\t\t *** Base de datos ***" << endl << endl;
cout << "1. Registro de usuario" << endl;
cout << "2. Login" << endl;
cout << "0. Salir" << endl << endl;
cout << "Escoge una opcion: ";
int option;
cin >> option;
cin.ignore();
cout << endl << endl;
system("pause");
switch(option){
case 1:{
system("cls");
cout << "Nombre: ";
getline(cin, usuario.name);
cout << endl << "Apellido: ";
getline(cin, usuario.second);
cout << endl << "Direccion: ";
getline(cin, usuario.street);
cout << endl << endl << "Nombre de usuario: ";
getline(cin, usuario.user);
cout << endl << "Password: ";
getline(cin, usuario.pass);
registroUsuario();
registroLogin();
cout << endl << endl;
system("pause");
goto menu;
break;
}
case 2:{
system("cls");
cout << "Usuario: ";
getline(cin, usuario.user);
cout << endl << "Password: ";
getline(cin, usuario.pass);
if(login(usuario.user, usuario.pass) == true){
goto login;
}
else{
cout << endl << endl << "Login incorrecto.";
goto menu;
}
break;
}
}
login:
cout << endl << endl << endl << "LOGIN CORRECTO";
return 0;
}