In this program I want to add elements to a static array, and then through the dni to be able to search for them to later eliminate them. I have all the functions, but when loading patients over and over again, the new patient "covers" the previous one, and when searching for it, it does not appear.
I can't figure out if the problem is in the struct, in the functions or what. They recommended me to do it in functions so the main program does not have so much load. These functions will be inside a function called menu, so the main program will simply be menu();
Recommendations??
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
const long int MAX = 10000;
struct paciente{
string nombre;
string apellido;
int dni;
int telefono;
};
int eliminar(paciente arreglo[], int tamaño, int elem){
for (int i = 0; i < tamaño; i++){
if (arreglo[i].dni == elem){
for (int k = i; k < (tamaño-1); k++){
arreglo[k].dni = arreglo[k+1].dni;
}
tamaño--;
break;
}
}
return tamaño;
}
int cargarpac(paciente arreglo[], int tamaño){
int cuantos;
cout << "Cuantos pacientes desea ingresar: " << endl; cin >> cuantos;
for (int i = 0; i < cuantos; i++){
cout << "Ingrese su nombre: "; getline(cin >> ws, arreglo[i].nombre);
cout << "Ingrese su apellido: "; getline(cin >> ws, arreglo[i].apellido);
cout << "Ingrese su DNI: "; cin >> arreglo[i].dni;
cout << "Ingrese su numero de telefono: "; cin >> arreglo[i].telefono;
arreglo[tamaño].nombre = arreglo[i].nombre;
arreglo[tamaño].apellido = arreglo[i].apellido;
arreglo[tamaño].telefono = arreglo[i].telefono;
arreglo[tamaño].dni = arreglo[i].dni;
tamaño++;
}
return tamaño;
}
bool buscar(paciente arreglo[], int tamaño, int elim){
for (int i = 0; i < tamaño; i++)
if (arreglo[i].dni == elim)
return true;
return false;
}
void mostrar(paciente datosPaciente[]){
int i = 0;
int dimlog = 1;
while (i < dimlog){
cout << "Nombre: " << datosPaciente[i].nombrepac << endl;
cout << "Apellido: " << datosPaciente[i].apellido << endl;
cout << "DNI:" << datosPaciente[i].dni << endl;
cout << "Numero de telefono: " << datosPaciente[i].telefono << endl;
i++;
}
}
void admPacient();
paciente datosPaciente[MAX];
int dimlog = 0;
void altaPac(){
cargarpac(datosPaciente, dimlog);
}
void bajaPac(){
int elim;
int deseo;
cout << "Ingrese su DNI para darse de baja: "; cin >> elim;
if (buscar(datosPaciente, dimlog, elim)){
mostrar(datosPaciente);
cout << "Si quiere darse de baja del sistema, presione 1. De lo contrario, presione 2" << endl; cin >> deseo;
if (deseo == 1){
eliminar(datosPaciente, dimlog, elim);
}
else{
getch();
}
}
else{
cout << "El paciente no se encuentra en el sistema" << endl;
}
}
void admPacient(){
char opcionpac;
do{
cout << "MENU DE ADMINISTRACION DE PACIENTES" << endl;
cout << "[a] Alta de Pacientes." << endl;
cout << "[b] Modificacion de Pacientes." << endl;
cout << "[c] Baja de Pacientes." << endl;
cout << "[d] Listado de Pacientes." << endl;
cout << "[e] Volver al Menu Principal." << endl;
cout << "Seleccione el menu al que desea acceder: "; cin >> opcionpac; opcionpac = tolower(opcionpac);
switch(opcionpac)
{
case 'a':
{
system("cls");
altaPac();
break;
}
case 'b':
{
system("cls");
modifPac();
break;
}
case 'c':
{
system("cls");
bajaPac();
break;
}
case 'd':
{
system("cls");
listaPac();
break;
}
case 'e':
{
system("cls");
break;
}
default:
{
cout << "La opcion ingresada es inexistente. Presione ENTER para regresar al menu anterior.";
getch();
system("cls");
break;
}
}
} while (opcionpac != 'e');
}
void menu(){
int opcion;
do{
cout << "BIENVENIDO AL MENU DE SALUD+." << endl;
cout << "[1] Administrar Especialidades Medicas." <<endl;
cout << "[2] Administrar Pacientes." << endl;
cout << "[3] Administrar Turnos." << endl;
cout << "[4] Salir." << endl;
cout << "Seleccione el menu al que desea acceder: "; cin >> opcion;
switch (opcion)
{
case 1:
{
system ("cls");
admEspMed();
break;
}
case 2:
{
system ("cls");
admPacient();
break;
}
case 3:
{
system ("cls");
admTurnos();
break;
}
case 4:
{
exit(0);
}
default:
{
cout << "La opcion ingresada es inexistente. Presione ENTER para regresar al menu anterior.";
getch();
system("cls");
break;
}
}
}while (opcion != 4);
}
//PROGRAMA PRINCIPAL
int main()
{
menu();
return 0;
}
Thank you!!
The
tamaño
function variablecargarpac
is a copy of the global variabledimlog
, when you increment the variabletamaño
you are not affecting the data ofdimlog
.You probably wanted to use a reference:
With a reference the referenced variable is automatically updated. Or maybe you meant to save the value after the call using the return value?