I need help with my code. I want to show the employees with salaries greater than 1000 dollars in the function void MostrarDatosMmil();
. I've tried this way and it doesn't show me anything. All other functions work correctly.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define sueld 1000
//Estructura empleados
struct empleados
{
char nombre[30];
char apellido[30];
int edad;
float sueldo;
char genero [15];
};
empleados *Cantidad = new empleados[2000]; //Cantidad máxima de personas
// Funciones
void IngresarDatos();
void MostrarDatos();
void MostrarDatosMmil();
// Funcion principal
int i = 0;
int main()
{
int opc;
do {
printf("\t\tBienvenido a la base de datis de empleados\n");
printf("\n1. Ingresar datos de los empleados\n");
printf("2. Mostrar datos de los empleados\n");
printf("3. Mostrar empleados con sueldo superior a 1000\n");
printf(". Salir\n");
printf("Ingrese una opcion: ");
scanf("%d",&opc);
switch (opc)
{
case 1:
system("cls");
IngresarDatos();
getch();
system("cls");
break;
case 2:
system("cls");
MostrarDatos();
getch();
system("cls");
break;
case 3:
system("cls");
MostrarDatosMmil();
getch();
system("cls");
break;
}
} while (opc != 4);
return 0;
}
void IngresarDatos()
{
int numempl;
int repite = 0;
printf("Seleccione la cantidad de empleados a ingresar: ");
scanf("%d", &numempl);
do
{
printf("\nIngrese el nombre del empleado: ");
scanf("%s",&Cantidad[i].nombre);
printf("Ingrese el apellido del empleado: ");
scanf("%s",&Cantidad[i].apellido);
printf("Ingrese el edad del empleado: ");
scanf("%d",&Cantidad[i].edad);
printf("Ingrese el sueldo del empleado: ");
scanf("%f",&Cantidad[i].sueldo);
printf("Ingrese el genero del empleado: ");
scanf("%s",&Cantidad[i].genero);
i++;
repite++;
} while (repite != numempl);
}
void MostrarDatos()
{
for (int j = 0; j < i; j++) {
printf("\n\nNombre: %s \nApellido: %s \nEdad: %d \nSueldo: %0.2f \nGenero: %s",
Cantidad[j].nombre, Cantidad[j].apellido, Cantidad[j].edad, Cantidad[j].sueldo, Cantidad[j].genero);
}
}
void MostrarDatosMmil()
{
for (int k = 0; k < i; k++)
{
if (Cantidad[i].sueldo > sueld)
{
printf("\n\nNombre: %s \nApellido: %s \nEdad: %d \nSueldo: %0.2f \nGenero: %s",
Cantidad[k].nombre, Cantidad[k].apellido, Cantidad[k].edad, Cantidad[k].sueldo, Cantidad[k].genero);
}
}
}
Your error is in the function
MostarDatosMmil
, since the call toprintf
is fine, but the check is not.You're looping from the first element (0) to the last element (i - 1), but in all iterations you check if the element i (it's after the last one) has a salary less than 1000.
This is going to have undefined behavior, since variables are not initialized by default, and can hold any value, positive, negative, or zero. So sometimes the employee will show you k and sometimes not.
In any case, it's going to be wrong because showing employee k depends on employee i, which hasn't been put into the employee collection yet.
What's more, if you get to fill the vector of employees, it will try to access employee 2001 and you can skip a segment failure for trying to access an area that has not been reserved.
The check
if
should be:In any case, consider following C/C++ conventions (you tag your question as C++ but use C libraries, and don't use any C++ features except the
new empleados[2000]
): write function and variable names starting with lower case ( lower camel case for example), and those#define ALGO
in capital letters (without starting with hyphens, since these have a use reserved to the compiler).Also, I recommend that you don't declare a global variable named
i
because its name doesn't mean anything. Consider refactoring it likenumeroEmpleados
for example, and putting full and meaningful variable names.Also, a type variable
struct empleados
will not contain many employees, but one, so I recommend putting the name in the singular.For more order and correctness it would be even better to declare another structure, containing the collection (vector) of employees and the number of employees inserted so far.
You can even define methods within structs to work with them more comfortably and logically. For example:
So you don't have free methods that handle structures, instead structures provide methods to handle them properly.