I am beginning to code with C++. This is what I want my program to do: 1. The user inputs the number of persons he wants to write into the database (a simple txt file). 2. The user inputs the name of the fist person (Nombre_usuario), the age(edad) and the ID (C.I:). 3. The user repeats the same process until he reach the number he input on step 1.
This is my code:
#include<iostream>
#include<windows.h>
#include <direct.h>
#include <fstream>
using namespace std;
int main(){
string Nombre_usuario;
int edad;
int cerdula;
int num;
//----------LOOP----------
std::cout<<"¿Cuántos clientes desea ingresar ahora?:";
std::cin>>num;
for(int x=0; x<num;x++){
//-------------User Interface------------
std::cout<<"Ingrese su nombre y apellido:";
std::getline(cin, Nombre_usuario);
std::cout<<"Ingrese su edad:";
std::cin>>edad;
std::cout<<"Ingrese su C.I.:";
std::cin>>cerdula;
}
//----------------Data storage on txt-------------------------------
_mkdir("C:\\FP");
std::ofstream outfile;
outfile.open("base_de_datos_cli.txt", std::ofstream::out, std::ofstream::app);
outfile<<"Nombre de usuario:"<<Nombre_usuario<<std::endl;
outfile<<"Edad:"<<edad<<std::endl;
outfile<<"C.I.:"<<cerdula<<std::endl;
outfile.close();
return 0;
}
The result, however, looks like this:
So, the input fields are mixed up, it does not repeat the process the number of times I ask it to, etc...
Why is this happening?
Thank you very much.