-1

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.

  • 1
    Please put some effort in formatting your code and don't post pictures of text but post text. And please write english code, any other language is confusing to us. And please show us what output you expect. You can [edit] your question. – Jabberwocky Jan 17 '18 at 15:50

1 Answers1

0
  • It does not repeat the process the number of times I ask it to

The chunk of code that outputs the data to "base_de_datos_cli.txt" should be inside the loop, since every time you get a new input, the last input is lost.

  • The input fields are mixed up

Read this. You can't use std::getline after another input without calling cin.ignore() to clear the extra newline character left in the input stream.

Here is a new version of your program that works and is better organizated. I tried to keep the code in Spanish as much as possible. Please do not make a straight copy, but try to understand why your code wasn't working:

#include <iostream>
#include <direct.h> 
#include <fstream> 
#include <string>

using namespace std;

int main(void)
{
    _mkdir("C:\\FP");
    std::ofstream outfile;
    outfile.open("C:\\FP\\base_de_datos_cli.txt", std::ofstream::out | std::ofstream::app);

    string Nombre_usuario;
    int edad;
    int cerdula;
    int num;

    std::cout << "¿Cuántos clientes desea ingresar ahora?:";
    std::cin >> num;

    for (int x = 0; x < num; x++) {
        std::cout << "Ingrese su nombre y apellido:";
        std::cin.ignore();
        std::getline(std::cin, Nombre_usuario);
        std::cout << "Ingrese su edad:";
        std::cin >> edad;
        std::cout << "Ingrese su C.I.:";
        std::cin >> cerdula;

        outfile << "Nombre de usuario:" << Nombre_usuario << std::endl;
        outfile << "Edad:" << edad << std::endl;
        outfile << "C.I.:" << cerdula << std::endl;
    }

    outfile.close();

    return 0;
}
Nighteen
  • 731
  • 1
  • 7
  • 16