C ++ Calculation of separate rows Matrix

2

This is my code:

#include <iostream>

using namespace std;

int main()
{
    int c, floor, colum;
    float rent;
    cout<<"\n\n\t\t\t\t< Sistema de calculo de arriendos >"<<endl<<endl<<endl;
    cout<<"> Ingrese el valor de la renta: "<<char(36);
    cin>>rent;
    cout<<endl;
    cout<<"> Ingrese la cantidad de pisos: ";
    cin>>floor;
    cout<<endl<<"> Ingrese la cantidad de habitaciones por piso: ";
    cin>>colum;
    cout<<endl<<endl;
    string edificio[floor][colum];
    int vpiso[floor];
    for (int x=0, y=0; x<floor && y<=colum; y++)
    {
        if (y==colum)
        {
            y=0;
        }
        cout<<"> En el Piso "<<x+1<<" la habitacion "<<y+1<<" esta ocupada? (si/no): ";
        cin>>edificio[x][y];
        cout<<endl;
        if (edificio[x][y]=="si")
        {
            c++;
        }
        if (y==colum-1)
        {
            x++;
        }
    }
    cout<<"\n\n\t\t";
    for (int x=0, y=0; x<floor && y<=colum; y++)
    {
        if (y==colum)
        {
            y=0;
        }
        cout<<edificio[x][y]<<"\t";
        if (y==colum-1)
        {
            x++;
            cout<<"\n\n\t\t";
        }
    }
    cout<<endl<<"> Debe cobrar a "<<c<<" personas un valor de: "<<char(36)<<rent*c;
    cin.get();
    cin.get();
    return 0;
}

PS: I remade the program of 0, but what I want to show on the screen is the value to charge for each row

ex:

"On the 1st floor they must" x people "and they have to pay" x value

"On the 2nd floor they must" x people "and they have to pay" x value

    
asked by Malthael 21.09.2016 в 20:37
source

2 answers

5

This is badly worded, if you said "so many" or "so many" , it would be more understandable, but as you put the letter x it seems as if it were a variable .

"En el piso 1 deben" x personas "y tienen que pagar" x valor.

I would change it for this, since it is not likely that the number of people is the same as the value to pay. The magic is in changing the floor number to that of a variable.

I suspect that the variables should be ...

cantidadPisos = floor;
piso = x;
cantidadPersonas = c;
valor = rent*c;

The code should look something like this, below the if that verifies the column should go a cout .

    if (y==colum-1)
    {
        x++;
        cout<<"\n\n\t\t";
    }
    cout << "En el piso " << piso
    <<" deben " << cantidadPersonas << "personas"
    << "y tienen que pagar" << valor << "valor." <<endl;
    
answered by 22.09.2016 / 01:56
source
4

Problem.

Everything. The code you present does not seem to fit, in any way, what you intend.

  • vpiso is not used.
  • You use std::string without including <string> .
  • You use std::cin to make a binary question to your user, using a text variable to read and check the result.
    • To make it worse, use a variable-sized array (not supported in C ++ more than by compiler extensions) ) to save the answer.
  • Abuse of std::endl .
  • You want to show "En el piso 1 deben" x personas "y tienen que pagar" x valor at the end and that string is not even part of the code.
  • You do not initialize a variable.
    • And the names of the variables are not self-explanatory, do not use variables whose name is a single letter .
  • Point 3 is especially worrisome, all these responses will be considered negative:

    • Si .
    • SI .
    • Yes .
    • Da .
    • .

    While these negative responses will be considered positive:

    • si claro, a ti te lo voy a decir! .
    • si vuelves a preguntarme algo, me comeré tu pez de colores a la parrilla .
    • si me entero de que trabajas para Skynet usaré tu CPU de pisapapeles .

    Proposal.

    You do not need a two-dimensional arrangement to store each floor / room since you only need the sum of each floor, so use an arrangement only for the rooms in which you will count each occupied floor.

    unsigned floor{}, colum{}, *arrendados;
    float rent{};
    std::cout<<"\n\n\t\t\t\t< Sistema de calculo de arriendos >\n\n\n";
    std::cout<<"> Ingrese el valor de la renta: $";
    std::cin>>rent;
    std::cout<<'\n';
    std::cout<<"> Ingrese la cantidad de pisos: ";
    std::cin>>floor;
    std::cout<<"\n> Ingrese la cantidad de habitaciones por piso: ";
    std::cin>>colum;
    std::cout<<"\n\n";
    
    arrendados = new unsigned[floor]{};
    

    Unemployed apartments do not matter to us for the calculation, so we ignore them:

    for (unsigned piso{}; piso < floor; ++piso)
    {
        for (unsigned habitacion{}; habitacion < colum; ++habitacion)
        {
            std::string ocupada{};
            std::cout<<"\n> En el Piso "<<(piso+1)
                     <<" la habitacion "<<(habitacion+1)
                     <<" esta ocupada? (si/no): ";
            std::cin>>ocupada;
    
            arrendados[piso] += ocupada == "si" ? 0 : 1;
        }
    }
    

    To finish, we show the results and release the dynamic memory:

    for (unsigned piso{}; piso < floor; ++piso)
    {
        std::cout << "\nEn el piso " << piso
                  << " deben " << arrendados[piso]
                  << " personas y tienen que pagar "
                  << (arrendados[piso] * rent) << " valor";
    }
    
    delete[] arrendados;
    

    You can see the code working [here] .

        
    answered by 22.09.2016 в 09:58