#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
// Definir la estructura de datos para un registro
struct Registro {
std::string nombre;
std::string direccion;
std::string telefono;
int estrato;
};
// Función para leer un registro de la base de datos
Registro leerRegistro(const std::string& linea) {
std::stringstream ss(linea);
std::string campo;
Registro registro;
// Leer y asignar valores a los campos
std::getline(ss, campo, ',');
registro.nombre = campo;
std::getline(ss, campo, ',');
registro.direccion = campo;
std::getline(ss, campo, ',');
registro.telefono = campo;
std::getline(ss, campo, ',');
registro.estrato = std::stoi(campo);
return registro;
}
// Función para imprimir la información de un registro
void imprimirRegistro(const Registro& registro) {
std::cout << "Nombre: " << registro.nombre << std::endl;
std::cout << "Dirección: " << registro.direccion << std::endl;
std::cout << "Teléfono: " << registro.telefono << std::endl;
std::cout << "Estrato: " << registro.estrato << std::endl;
std::cout << std::endl;
}
int main() {
std::ifstream archivo("basededatos.txt");
std::string linea;
char opcion;
// Verificar si se pudo abrir el archivo
if (!archivo.is_open()) {
std::cerr << "No se pudo abrir el archivo." << std::endl;
return 1;
}
// Bucle principal
do {
// Leer una línea de la base de datos
std::getline(archivo, linea);
// Verificar si se alcanzó el final del archivo
if (archivo.eof()) {
std::cout << "Fin de la base de datos." << std::endl;
break;
}
// Leer el registro
Registro registro = leerRegistro(linea);
// Verificar si el estrato es 1, 2 o 3
if (registro.estrato >= 1 && registro.estrato <= 3) {
// Imprimir la información del registro
imprimirRegistro(registro);
}
// Preguntar al usuario si desea continuar
std::cout << "¿Desea continuar? (S/N): ";
std::cin >> opcion;
std::cin.ignore(); // Limpiar el buffer del teclado
} while (opcion == 'S' || opcion == 's');
// Cerrar el archivo
archivo.close();
return 0;
}