Hello, I am making a program that manages the entry and printing of data of employees of a company
This is my code:
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#define TECLA_ARRIBA 72
#define TECLA_ABAJO 80
#define ENTER 13
using namespace std;
int cant;
class empleado;
class programa
{
public:
void cabecera();
void cantidad_de_empleados();
int constructordemenues(const char *titulo, const char *opciones[], int n);
void bibliotecademenues(char opcion);
void gotoxy(int x,int y);
}
cuerpo;
void programa::cabecera()
{
cout<<"\n\n\t\t\t\t";
cout<<char(201);
for(int i=0, x=36; i<=x; i++)
cout<<char(205);
cout<<char(187);
cout<<"\n\t\t\t\t";
cout<<char(186)<<" <:: Registro de empleados - XYZ ::> "<<char(186);
cout<<"\n\t\t\t\t";
cout<<char(200);
for(int i=0, x=36; i<=x; i++)
cout<<char(205);
cout<<char(188);
cout<<"\n\n\t\t\t\t";
}
void programa::cantidad_de_empleados()
{
cabecera();
cout<<"> Cantidad de empleados a manejar: ";
cin>>cant;
}
int programa::constructordemenues(const char *titulo, const char *opciones[],int n)
{
int opcionSeleccionada = 1;
int tecla;
bool repite = true;
do
{
system("cls");
cabecera();
gotoxy(32, 6);
cout << titulo;
gotoxy(28, 7 + opcionSeleccionada);
cout << ">";
for (int i = 0; i < n; i++)
{
gotoxy(32, 8 + i);
cout << i + 1 << ") " <<opciones[i];
}
do
{
tecla = getch();
}
while (tecla != TECLA_ARRIBA && tecla != TECLA_ABAJO && tecla != ENTER);
switch (tecla)
{
case TECLA_ARRIBA:
opcionSeleccionada--;
if (opcionSeleccionada < 1)
opcionSeleccionada = n;
break;
case TECLA_ABAJO:
opcionSeleccionada++;
if (opcionSeleccionada > n)
opcionSeleccionada = 1;
break;
case ENTER:
repite = false;
break;
}
}
while (repite);
return opcionSeleccionada;
}
void programa::bibliotecademenues(char opcion)
{
switch(opcion)
{
case 'p':
{
cantidad_de_empleados();
bool repite = true;
int opcion;
const char *titulo = "> Elija una opcion:";
const char *opciones[] = {"Ingresar un nuevo empleado","Verificar datos de uno existente","Salir"};
int n = 3;
while(repite)
{
opcion = constructordemenues(titulo,opciones,n);
switch (opcion)
{
case 1:
break;
case 2:
break;
case 3:
bibliotecademenues('s');
break;
}
}
break;
}
case 's': //menu de salida
{
bool repite = true;
int opcion;
const char *titulo = "> Desea salir del sistema? ";
const char *opciones[] =
{
"Si","No"
};
int n = 2;
while (repite)
{
opcion = constructordemenues(titulo,opciones,n);
switch (opcion)
{
case 1:
exit(0);
break;
case 2:
repite = false;
break;
}
}
break;
}
}
}
void programa::gotoxy(int x,int y)
{
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y= y;
SetConsoleCursorPosition(hcon,dwPos);
}
int main()
{
cuerpo.bibliotecademenues('p');
system("pause>nul");
return 0;
}
class empleado
{
private:
string nombres;
string telefono;
public:
void ingresar_datos();
void imprimir_datos();
}
persona[cant];
My problem is that I would like to assign a value to the cant variable, but when I do so the compiler throws me this error " array bound is not an integer constant before ']' token "
Extra: I would also like to be able to declare the program class and put it after the main but it does not recognize the body object when doing it. How do I do it?
Your problem can be reduced to the following:
With
persona[cant];
we understand that your intention is to define an array of 5 elements and that is when the compiler shows you the errorWhat it is telling you is that at the time the application is COMPILED , the variable
cant
has no value, so it is impossible for it to know how big it should be.What it asks you is to define this variable as
const
, so that the compiler knows what value this variable has at compile time:However, this is not going to help you, since I understand that what you need is to define a dynamic array of objects of type
empleado
.What I recommend is that you use a simple vector of employees:
UPDATE 1
Using the design of your application, I give you an example of using a vector.
However, I make some important considerations in my opinion:
I would change the design of your employee class, creating an initialization constructor for the variable name and phone:
empleado( std::string nombre, std::string telefono ) : nombre(nombre), telefono(telefono) {};
I would do the introduction of each field outside the class and use the constructor instead of asking the object to request to "fill" it
As for the rest of your code, it's too broad and I think it falls outside of your original question.
To fix this problem (at least in my case) create the variable that contains the size of the array of objects and then declare the array inside the main, so it recognizes me without problems eg: