Avoid repeated names in c ++ arrangement

-1

As part of my subject of data structure we have left the following exercise:

  

Create a data structure that allows to store the data of the total purchases of customers from a warehouse.   You want to store the following information: Number of the invoice, Date, Warehouse, Client, City, Sex, Value purchases.

     

Create an arrangement that allows you to fill in the information of N clients and then implement (you can use a menu) the following options:

     
  • Show the total value of purchases made by customers, and the average of purchases
  •   
  • Sort the arrangement and display it according to the following criteria:      
    • 2.1 For customers
    •   
    • 2.2 Por almacen
    •   
    • 2.3 By value purchases (descending)
    •   
  •   
  • Show a summary that says:      
    • 3.1 How many sales were made in each city
    •   
    • 3.2 How many customers are there in each city
    •   
  •   
  • Include an option that allows you to search the vector by city. The name of the city will be entered and the system must return all sales made in that city.
  •   

    The point is that we have already done most of the code, the program is able to read the necessary data, perform the operations, the orders, etc ...
    But we have not been able to create the algorithm to count the number of clients in each city, since the program must be able to recognize when a customer has repeated itself in the city so as not to register it as a different client; that is, if a client performs, for example, 2 purchases in the same city, the program registers it as a single client, not as 2.
      Here is the code we use to count sales (the simple part, since there are no exceptions) and the structure we created:

    struct venta{//la estructura que utilizamos
    string numfactura;//numero de factura 
    string fecha;
    string almacen;
    string cliente;
    int ciudad;//la ciudad tiene unos cuantos valores predefinidos
    float valorcompras;
    }vector[50];
    
    for (int i=0;i<N;i++){//Este ciclo recorre el vector en busca de las coincidencias
                    if (vector[i].ciudad==1){
                    ventasm+=1;//cuando ciudad es 1, se le suma 1 al contador de la ciudad 1.
                    clientesm+=1;//esto es lo que tenemos, si el cliente se repite varias veces, este algorito lo contará como clientes diferentes. esto es lo que necesitamos cambiar.
                    }
                    else if (vector[i].ciudad==2){//exactamente lo mismo pero con la segunda ciudad
                    ventabq+=1;
                    clientebq+=1;
                    }
                }
    

    I really hope I have been concise and that you can help me solve this problem. thanks in advance.

        
    asked by Robin quintero 03.09.2017 в 07:26
    source

    1 answer

    1

    If you have to count the number of customers that have bought in each city and also know if that customer has already bought there, you need something to store the customers who have already bought, for example a vector.

    This would be the idea:

    Your struct would now look like this:

    struct venta{       //la estructura que utilizamos
    string numfactura;  //numero de factura 
    string fecha;
    string almacen;
    string[] clientes;   //aquí guardáis todos los clientes distintos
    //El siguiente es el número de clientes distintos, esto os lo podríais
    //ahorrar si quisierais trabajando con vuestro vector cliente, ya que al  
    //final cuenta el número de elementos que hay en el vector
    int numClientes;    
    int ciudad;         //la ciudad tiene unos cuantos valores predefinidos
    float valorcompras;
    }vector[50];
    

    Your code would look something like this:

    for (int i=0;i<N;i++){//Este ciclo recorre el vector en busca de las coincidencias
                    if (vector[i].ciudad==1){
                    ventasm+=1;//cuando ciudad es 1, se le suma 1 al contador de la ciudad 1.
                    if(!clienteEnVectorClientes(cliente)) //esta función simplemente busca un elemento en un vector
                    numClientes+=1; //incrementamos nuestro contador de clientes diferentes
                    }
                    else if (vector[i].ciudad==2){//exactamente lo mismo pero con la segunda ciudad
                    ventabq+=1;
                    clientebq+=1;
                    }
                }
    

    Example of customerEnVectorClients

    bool clienteEnVectorClientes(string cliente)
    {
        for(int i=0; i<numClientes;i++)
        {
            if(cliente==clientes[i])
            {
                return true;
            }
        }
      return false;
    }
    
      

    Each time a client is saved, you should call this function to see if it is already there. If it is, nothing is done, if it is not added and numClients is increased.

    I hope it will help and help you. I think the best thing is that you try it yourself . However, it's not bad that you ask if you get stuck.

        
    answered by 03.09.2017 / 11:26
    source