C ++ array bound is not an integer

0

Hello, I'm doing 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 variable cant, but in doing 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 I do not recognize the body object when doing it. How do I do it?

    
asked by Malthael 03.01.2017 в 20:17
source

2 answers

2

Your problem can be reduced to the following:

#include <iostream>

int cant;

class empleado;

int main()
{
    cant = 5;
    std::cout << "Hola!";
    return 0;
}

class empleado
{
private:
    std::string nombres;
    std::string telefono;
public:
    void ingresar_datos();
    void imprimir_datos();
}
persona[cant];

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 error

  

main.cpp: 23: 13: error: array bound is not an integer constant before ']' token    person [cant];

What you are saying is that at the time of COMPILING the application, the variable cant has no value, so it is impossible to know the size you should have.

What it asks you is that you define this variable as const , so that the compiler knows what value this variable has at the moment of its compilation:

int const cant = 5;

However, this is not going to help you, because I understand that what you need is to define a dynamic array of objects of type empleado . p>

What I recommend is that you use a simple vector of employees:

#include <vector>

std::vector<empleado> personas;

UPDATE 1

Using the design of your application, I give you an example of using a vector.

#include <iostream>
#include <vector>

class empleado
{
private:
    std::string nombre;
    std::string telefono;
public:

    void ingresar_datos();
    void imprimir_datos();
};

void empleado::ingresar_datos()
{
    std::cout << "Introduce nombre: ";
    std::cin >> nombre;
    std::cout << "Introduce teléfono: ";
    std::cin >> telefono;
}

void empleado::imprimir_datos()
{
    std::cout << "Nombre: " << nombre << std::endl;
    std::cout << "Teléfono: " << telefono << std::endl;
}

int main()
{
    std::cout << "Creando datos\n";

    empleado e1;
    std::vector<empleado> listaEmpleados;

    // Solicitas el ingreso de datos
    e1.ingresar_datos();

    // Lo agregas al vector de empleados
    listaEmpleados.push_back(e1);

    return 0;
}

However, I give you some important considerations in my opinion:

  • I would change the design of your employee class, creating an initialization constructor of the variable name and phone:

    empleado( std::string nombre, std::string telefono ) : nombre(nombre), telefono(telefono) {};

  • Would make the introduction of each field out of the class and would use the constructor instead of asking the object to request "fill in" the same

About the rest of your code, it's too broad and I think it's out of your original doubt.

    
answered by 03.01.2017 / 20:43
source
0

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 I can recognize it without problems eg:

int main()
{
   int con;
   cout<<"Ingrese el tamaño del arreglo: ";
   cin>>con;
   cout<<"\n";
   empleado persona[con];
   return 0;
}
    
answered by 07.01.2017 в 06:54