Problem when Printing Data from a C ++ Matrix

1

Good afternoon, I have the following exercise in which I ask for Name, Cedula, and also the amount of liners produced daily, but we must also take into account that it must be done per week (assuming that the month has 4 weeks).

The program works as follows:

I ask for the name and the worker's cedula, then for each day of the week (5 days), I ask how many linings it has produced, when the first week ends, it should print the weekly report , then It would happen to do the same but with week 2 and so on, when it ends with that worker who goes to the next one.

As it is currently the program, it does everything right until it gets to print the weekly report, it is there where I print the name and the cedula, but the other data does not print them and in passing the message "It has stopped working ... ".

int main(int argc, char const *argv[])
{
char nombre[9][30];
int cedula[9];
//int i = 0, a = 0, b = 0, c = 0;
int i, a, b, c;
const float sueldo = 15000, bono = 8250;
float forro_dia[5][5];
float total_semana[5];

for (int i = 1; i <= 9; i++)
{
    system("cls");
    cout<<"< = = = = = F A B R I C A de F O R R O S para V E H I C U L O S = = = = = >";
    cout<<"\n\nIngrese el Nombre del Trabajador: "<<i<<". ";
    cin.getline(nombre[i], sizeof(nombre));
    cout<<"\nCedula del Trabajador: ";
    cin>>cedula[c];

    for (int a = 1; a <= 5; a++)
    {              
        for (int b = 1; b <= 1; b++)
        {
            cout<<"\nCantidad de Forros Producidos en el Dia: "<<a<<". "<<"de la Semana: "<<b<<". "; 
            cin>>forro_dia[a][b];

            total_semana[b] = total_semana[b] + forro_dia[a][b];

            if (forro_dia[a][b] >= 80)
            {
                total_semana[b] = sueldo + bono;
            }else

                total_semana[b] = sueldo;

        }

    }

        system("cls");
        cout<<fixed<<setprecision(2);
        cout<<"< = = = = = F A B R I C A de F O R R O S para V E H I C U L O S = = = = = >";
        cout<<"\n\n< = = R E C I B O  S E M A N A L = = >";
        cout<<"\n\nTrabajador: "<<nombre[i];
        cout<<"\nCedula: "<<cedula[c];
        cout<<"\nForros Producidos en la Semana: "<<forro_dia[a][b];
        cout<<"\n\nTotal a Pagar: Bs. "<<total_semana[b];

        getchar();
        getchar();

}

return 0;
}
    
asked by Carlos Agustin Guanipa Alvarez 26.04.2017 в 21:12
source

3 answers

3

Complementing gbianchi's response:

-When a form is written in the form

   for (int a = 1; a <= 5; a++)

The variable is being declared inside the For, so it is not necessary to declare it at the top of your code. On the other hand, you should iterate in this way

for(int a = 0; a < 5; a++)

Start from zero to the number you intend to reach, since arrays and arrays in programming languages begin with a zero index. So you're leaving an empty space when you start at position 1.

I share the opinion of gbianch, Print the weekly values should be inside a new for to move inside the matrix and corroborating that the value of the indexes is consistent.

    
answered by 26.04.2017 в 21:46
2

Problem.

We are facing a clear example of the problems that can arise from having a badly organized code. In your case, the main function has so much information that it makes it more complicated than necessary to analyze it.

The problem seems already solved by the researches of gbianchi and federhico but I would like to share an additional approach.

Proposal.

I advise you to follow the Principle of Sole responsibility : separates the painting of headers from the data collection and the presentation of them.

For this we will begin by defining a data structure that contains the worker's information:

struct Trabajador
{
    std::string nombre;
    int cedula;
};

I have chosen std::string as a type to store the worker's name because in this way we avoid having a fixed length; in your case you were limiting the worker's name to 9 characters (8 if we count the null character of string end) so a worker named "Teodoredo" would no longer fit 1 .

We continue with a function that, given a worker, read your data:

void leer_datos(Trabajador &trabajador)
{
    std::cout << "\n\nIngrese el Nombre del Trabajador: ";
    std::getline(std::cin, trabajador.nombre);
    std::cout << "\nCedula del Trabajador: ";
    std:: cin >> trabajador.cedula;
}

Next, a structure that stores the production of a week and returns us data on it:

struct Semana
{
    float produccion[5u]{};

    float forros() const
    {
        return std::accumulate(std::begin(produccion), std::end(produccion), 0.f);
    }

    float total_a_pagar(float sueldo, float bono) const
    {
        float resultado = 0.f;
        for (const auto &produccion_dia : produccion)
        {
            resultado += (sueldo + (produccion_dia < 80.f ? 0.f : bono));
        }
        return resultado;
    }
};

I have assigned the responsibility to calculate the salaries based on data from the week to the week itself, but you can choose another approach.

With this structure we can create a function that, given a week, read your data:

void leer_datos(Semana &semana)
{
    for (auto dia = 0u; dia < 5u; ++dia)
    {
        std::cout << "\nCantidad de Forros Producidos en el Dia: " << (dia + 1) << ". "; 
        std::cin >> semana.produccion[dia];
    }
}

To finish, it seems that we need a structure Recibo :

struct Recibo
{
    Trabajador trabajador;
    Semana periodo[5u];
};

What we can read with this function:

void leer_datos(Recibo &recibo)
{
    leer_datos(recibo.trabajador);
    for (auto semana = 0u; semana < 5u; ++semana)
    {
        std::cout << "\n\nSemana " << (semana + 1)<< ".\n";
        leer_datos(recibo.periodo[semana]);
    }
}

This way you can have a main as clean as this:

int main()
{
    Recibo recibos[9u];

    std:: cout << "< = = = = = F A B R I C A de F O R R O S para V E H I C U L O S = = = = = >";

    for (auto &recibo : recibos)
        leer_datos(recibo);

    std::cout << "\n\n< = = = = = F A B R I C A de F O R R O S para V E H I C U L O S = = = = = >";
    std::cout << "\n< = = R E C I B O  S E M A N A L = = >";

    for (const auto &recibo : recibos)
    {
        std::cout << "\n\nTrabajador: " << recibo.trabajador.nombre
                  << "\nCedula: "       << recibo.trabajador.cedula;

        for (auto semana = 0u; semana < 5u; ++semana)
        {
            std::cout << "\nForros Producidos en la Semana " << (semana + 1) << ": " << recibo.periodo[semana].forros()
                      << "\n\nTotal a Pagar: Bs. " << recibo.periodo[semana].total_a_pagar(15'000.f, 8'250.f);
        }
    }

    return 0;
}

1 And if we add the surnames, our friend Teodoredo Atanagildo-Gundemaro de las Mercedes is out of the picture.

    
answered by 27.04.2017 в 11:10
1

Notice that the whole print cycle is not initialized or is not going through the items in the matrix

    cout<<"\n\n< = = R E C I B O  S E M A N A L = = >";
    cout<<"\n\nTrabajador: "<<nombre[i];
    cout<<"\nCedula: "<<cedula[c];
    cout<<"\nForros Producidos en la Semana: "<<forro_dia[a][b];
    cout<<"\n\nTotal a Pagar: Bs. "<<total_semana[b];

This whole part is outside the cycles for where you loaded the data. which will only print the data of a person.

Even though that was your intention, a and b you used them in the for cycles, and when you get to those lines, they have values outside of the matrix intervals (because they were left by the for cycles).

Your intention is not well understood, but you must declare the correct values of a and b or pass positions of valid matrices.

    
answered by 26.04.2017 в 21:37