Print Multiple Data in C ++

3

Good morning, I have the following exercise in which I wish that at the end of the program I would throw a list with the names and ID of those who are foreigners. How can I do it?

#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main(int argc, char *argv[]) 
{
    /*3. Desarrolle un programa en C++ que lea el nombre y la nacionalidad 
    18 pasajeros, e imprima los datos de los pasajeros 
    con nacionalidad no venezolana. Utilice ciclo For*/

    char nom[10], nac;
    int ve=0,ex=0, n=0,c;

    system("color A");

    for (n=1;n<=2;n++)
    {
        system("cls");
        cout<<"Seleccione su Nacionalidad: \n";
        cout<<" A: Extranjero\n";
        cout<<endl;
        cout<<" B: Venezolano\n";
        cin>>nac;
        cout<<endl;

        switch(nac)
        {
        case 'a': case 'A':
            cout<<" "<<n<<") Ingrese su nombre: ";
            cin>>nom;
            ex++;
            cout<<"\nIngrese su Cedula: ";
            cin>>c;

            break;

        case 'b': case 'B' :
            cout<<" "<<n<<") Ingrese su nombre: ";
            cin>>nom;
            ve++;
            cout<<"\nIngrese su Cedula: ";
            cin>>c;

            break;

        default:
            cout<<" Opcion no valida";
            cout<<endl;
            n--;
        } 

    }

    system("cls");
    cout<<"\nNumero de Extranjeros: "<<ex;
    cout<<"\nNumero de Venezolanos: "<<ve;

    return 0;
}
    
asked by Carlos Agustin Guanipa Alvarez 10.04.2017 в 15:31
source

2 answers

2

Although they have already answered you here is one more way, something more basic than that of our partner PaperBirdMaster.

int main(int argc, char *argv[])
 {
 /*3. Desarrolle un programa en C++ que lea el nombre y la nacionalidad
 18 pasajeros, e imprima los datos de los pasajeros
 con nacionalidad no venezolana. Utilice ciclo For*/

I create three vectors, where I keep the name, nationality and identity card of each person in parallel.

 char nom[18][10];//Pese a esto, te recomiendo utilizar el tipo string
 para no limitar la cantidad de caracteres, codificando algo asi: string nom[18];

 char nac[18];
 int c[18];
 int ve=0,ex=0;


 for (int i=0;i<18;i++)
{
system("cls");
cout<<"Seleccione su Nacionalidad: \n";
cout<<" A: Extranjero";
cout<<endl;
cout<<" B: Venezolano\n";
cin>>nac[i];
cout<<endl;

switch(nac[i])
{
case 'A':case 'a':
    cout<<" "<<") Ingrese su nombre: ";
    cin>>nom[i];
    nac[i]='A';ex++;//Guarda nacionalidad en el espacio i de un array
    cout<<"\nIngrese su Cedula: ";
    cin>>c[i];//Guarda nacionalidad en el espacio i de un array

    break;

case 'B':case 'b':
    cout<<" "<<") Ingrese su nombre: ";
    cin>>nom[i];
    nac[i]='B';ve++;
    cout<<"\nIngrese su Cedula: ";
    cin>>c[i];

    break;

default:
    cout<<" Opcion no valida";
    cout<<endl;i--;
}


}

system("cls");
cout<<"\nNumero de Extranjeros: "<<ex;
cout<<"\nNumero de Venezolanos: "<<ve;

We use a loop to travel the nationality array and when the character in position i is A, it shows the content of the same position of the rest of the arrays.

for(int i=0;i<18;i++)
  {
if(nac[i]=='A')
{
    cout<<endl<<"Nombre: "<<nom[i]<<endl;
    cout<<"Cedula: "<<c[i]<<endl;
 }
}

return 0;
}
    
answered by 10.04.2017 / 16:35
source
5
  

I hope that at the end of the program I will be given a list with the names and ID of those who are foreigners. How can I do it?

Save the data in a list at the end of the data collection, go through the list printing only those that match the criteria you need to print.

For example, having a class persona :

enum class nacionalidad : int
{
    Venezuela,
    Extranjera,
};

struct persona
{
    nacionalidad nacionalidad;
    std::string nombre;
    int cedula;
};

And a list of people:

using personas = std::list<persona>;

personas p
{
    { nacionalidad::Venezuela, "A", 0 },
    { nacionalidad::Extranjera, "B", 1 },
    { nacionalidad::Venezuela, "C", 2 },
    { nacionalidad::Extranjera, "D", 3 },
    { nacionalidad::Venezuela, "E", 4 },
    { nacionalidad::Extranjera, "F", 5 },
    { nacionalidad::Venezuela, "G", 6 },
    { nacionalidad::Extranjera, "H", 7 },
    { nacionalidad::Venezuela, "I", 8 },
    { nacionalidad::Extranjera, "J", 9 },
    { nacionalidad::Venezuela, "K", 0 },
};

We can print the ones that are Extranjera in the following way:

std::for_each(p.begin(), p.end(), [](auto &p)
{
    if (p.nacionalidad == nacionalidad::Extranjera) std::cout << p.nombre << '\n';
});

You can see the code working in Wandbox 三 へ (へ ਊ) へ ハ ッ ハ ッ .

It is also possible to take advantage of the containers that group data by type to facilitate this operation, if we create a multi-map that classifies by nationality:

#include <map>

enum class nacionalidad : int
{
    Venezuela,
    Extranjera,
};

struct persona
{
    std::string nombre;
    int cedula;
};

using personas = std::multimap<nacionalidad, persona>;

We can select a specific nationality with equal_range :

personas p
{
    { nacionalidad::Venezuela, {"A", 0} },
    { nacionalidad::Extranjera, {"B", 1} },
    { nacionalidad::Venezuela, {"C", 2} },
    { nacionalidad::Extranjera, {"D", 3} },
    { nacionalidad::Venezuela, {"E", 4} },
    { nacionalidad::Extranjera, {"F", 5} },
    { nacionalidad::Venezuela, {"G", 6} },
    { nacionalidad::Extranjera, {"H", 7} },
    { nacionalidad::Venezuela, {"I", 8} },
    { nacionalidad::Extranjera, {"J", 9} },
    { nacionalidad::Venezuela, {"K", 0} },
};

const auto &personas_extranjeras = p.equal_range(nacionalidad::Extranjera);

for (auto p = personas_extranjeras.first; p != personas_extranjeras.second; ++p)
{
    std::cout << p->second.nombre << '\n';
}

You can see the code working in Wandbox 三 へ (へ ਊ) へ ハ ッ ハ ッ .

    
answered by 10.04.2017 в 16:21