0
#include <iostream>
#include <cmath>
#include <array>

using namespace std;

int x;

class estudiante
{
    private:
        string  nombre,cedula,codigo,direccion;

    public:
       void getDetalles(void);
       void setDetalles(void);
};

void estudiante::getDetalles(void){
    x=0;
    cout << "Ingrese el nombre del estudiante "<<x+1<<":" ;
    cin >> nombre;`
    cout << "Ingrese el numero de cedula: ";
    cin >> cedula;
    cout << "Ingrese el codigo: ";
    cin >> codigo;
    cout << "Ingrese la direccion ";
    cin >> direccion;

}

void estudiante::setDetalles(void){
    cout << "La informacion de estudiante "<<x+1<<": \n";
    cout << "Nombre:"<< nombre<<"\n";
    cout << "Cedula:"<< cedula<<"\n";
    cout << "Codigo:"<< codigo<<"\n";
    cout << "Direccion:"<< direccion<<"\n";

 }

int main()
{
     estudiante std;

    std.getDetalles();
    std.setDetalles();

   return 0;
}

这是我拥有的代码,但我需要插入 3 组 estudiantes 然后它应该打印详细信息(detalles)

输入

输出

4

1 回答 1

0

您可以使用std::vector<>来实现这一点。下面是工作示例。3个学生一个一个。您可以将学生人数更改为所需值的输入,甚至可以从用户那里获取。

#include <iostream>
#include <cmath>
#include <array>
#include <vector>
using namespace std;

int x;

class estudiante
{
    private:
        string  nombre,cedula,codigo,direccion;

    public:
       void getDetalles(void);
       void setDetalles(void);
};

void estudiante::getDetalles(void){
    x=0;
    cout << "Ingrese el nombre del estudiante "<<x+1<<":" ;
    cin >> nombre;
    cout << "Ingrese el numero de cedula: ";
    cin >> cedula;
    cout << "Ingrese el codigo: ";
    cin >> codigo;
    cout << "Ingrese la direccion ";
    cin >> direccion;

}

void estudiante::setDetalles(void){
    cout << "La informacion de estudiante "<<x+1<<": \n";
    cout << "Nombre:"<< nombre<<"\n";
    cout << "Cedula:"<< cedula<<"\n";
    cout << "Codigo:"<< codigo<<"\n";
    cout << "Direccion:"<< direccion<<"\n";

 }

int main()
{
    std::vector<estudiante> studentList;//a vector of students 
    //this is the input loop
    for(int i = 0; i < 3; ++i)//this loop will create 3 students and push them onto the vector above. You can change the value 3
    {
       estudiante temporarysStdnt;

       temporarysStdnt.getDetalles();
      // temporarysStdnt.setDetalles(); 
       
       studentList.push_back(temporarysStdnt);
    }
    
    //now to output the details of all the students
    for(estudiante& Student: studentList)
    {
        Student.setDetalles();
    }
   return 0;
}

于 2021-09-06T05:33:46.320 回答