Structure matrix c ++

1

Good, first to put them in contexts the function of the code is to add X products of a store and then if the client wants to add another store with And products the problem occurs when it is going to print because the rows can have different number of columns.

#include <iostream>
#include <vector>

using namespace std;

struct comida{
  int cant;
  string name;
  float price;
}f;

vector <comida> food1; //Vector que guarda la estructura de productos de una tenda
vector <vector<comida>> food;//Vector hecho a base del vector anterior para añadir las diferentes tiendas

int main ()
{
  string ans,ans2;//Vectores de verificacion 
  do
  {
    do
    {
      cout << "\nIngrese nombre: ";
      cin >> f.name;
      cout << "\nIngrese cantidad: ";
      cin >> f.cant;
      cout << "\nIngrese precio: ";
      cin >> f.price;
      food1.push_back(f);//Al vector de los productos se le van agregando gracias al do while
      cout << "\nDesea seguir 0<-no otro numero si: "; 
      cin >> ans;//Pidiedo variable verificadora
    }while(ans!="0");//Verificador, en caso de que el cliente quiera seguir o no

    food.push_back(food1);//Se guarda el vector de productos en el vector de tiendas
    food1.clear();//Limpiamos en vector de productos

    cout << "\nAgregar otra tienda? 0<-no otro numero si: ";
    cin >> ans2;//Verificamos si quiere agregar otra tienda

  }while(ans2!="0");

  cout << "\nNombre\tCantidad   Precio" << endl;
  //Aqui surge el problema
  for (int i=0;i<food.size();i++)
  {
    int j=0,r=1;
    do 
    {
      if(food[i][j].name!="
#include <iostream>
#include <vector>

using namespace std;

struct comida{
  int cant;
  string name;
  float price;
}f;

vector <comida> food1; //Vector que guarda la estructura de productos de una tenda
vector <vector<comida>> food;//Vector hecho a base del vector anterior para añadir las diferentes tiendas

int main ()
{
  string ans,ans2;//Vectores de verificacion 
  do
  {
    do
    {
      cout << "\nIngrese nombre: ";
      cin >> f.name;
      cout << "\nIngrese cantidad: ";
      cin >> f.cant;
      cout << "\nIngrese precio: ";
      cin >> f.price;
      food1.push_back(f);//Al vector de los productos se le van agregando gracias al do while
      cout << "\nDesea seguir 0<-no otro numero si: "; 
      cin >> ans;//Pidiedo variable verificadora
    }while(ans!="0");//Verificador, en caso de que el cliente quiera seguir o no

    food.push_back(food1);//Se guarda el vector de productos en el vector de tiendas
    food1.clear();//Limpiamos en vector de productos

    cout << "\nAgregar otra tienda? 0<-no otro numero si: ";
    cin >> ans2;//Verificamos si quiere agregar otra tienda

  }while(ans2!="0");

  cout << "\nNombre\tCantidad   Precio" << endl;
  //Aqui surge el problema
  for (int i=0;i<food.size();i++)
  {
    int j=0,r=1;
    do 
    {
      if(food[i][j].name!="%pre%"){
        cout << food[i][j].name << "\t" << food[i][j].cant << "\t   "  << food[i][j].price << "\t" << endl;}
      else
      {
        cout << "\t";
        r=0;
      }
      j++;
    }while(r!=0);
  }
}
"){ cout << food[i][j].name << "\t" << food[i][j].cant << "\t " << food[i][j].price << "\t" << endl;} else { cout << "\t"; r=0; } j++; }while(r!=0); } }

The data that I put in input are:

cafe 120 10 1 red 200 20 1 train 100 2000 0 1 ponque 20 200 1 cake 50 400 0 0

    
asked by Giovanni Palencia 29.10.2017 в 20:30
source

1 answer

0

You are expressing yourself badly and that means you have not gotten answers yet:

  

The problem occurs when it is going to print because the rows can have different number of columns.

What you say is totally wrong. The number of columns is fixed and their value is always 3 (name, quantity and price).

The problem you really have is that each store can have a different number of items ... so what is really variable is the number of rows (rows => Y axis, columns => X axis).

Let's see:

for (int i=0;i<food.size();i++)
{
  int j=0,r=1;
  do 
  {
    if(food[i][j].name!="
for (int i=0;i<food.size();i++)
{
  for( int j=0;<food[i].size();j++)
  {
    cout << food[i][j].name << '\t' << food[i][j].cant << '\t'  << food[i][j].price << '\n';
  }
}
"){ cout << food[i][j].name << "\t" << food[i][j].cant << "\t " << food[i][j].price << "\t" << endl;} else { cout << "\t"; r=0; } j++; }while(r!=0); }

Well, in this portion of code you try to dump the list of each store in the console ... you have food.size() stores, so it seems logical to iterate from 0 to food.size() ... now, if it turns out that for each store ( food[i] ) you have a vector with the list of articles ... Why do not you create a second loop that iterates that list?

for (int i=0;i<food.size();i++)
{
  vector<comida> & lista = food[i];
  for( int j=0;<lista.size();j++)
  {
    cout << lista[j].name << '\t' << lista[j].cant << '\t'  << lista[j].price << '\n';
  }
}

Although posts to avoid redundancy ...

for (int i=0;i<food.size();i++)
{
  int j=0,r=1;
  do 
  {
    if(food[i][j].name!="
for (int i=0;i<food.size();i++)
{
  for( int j=0;<food[i].size();j++)
  {
    cout << food[i][j].name << '\t' << food[i][j].cant << '\t'  << food[i][j].price << '\n';
  }
}
"){ cout << food[i][j].name << "\t" << food[i][j].cant << "\t " << food[i][j].price << "\t" << endl;} else { cout << "\t"; r=0; } j++; }while(r!=0); }

The STL containers differ from the raw arrays ( int* lista ), in that they are able to tell you how many elements they contain. Take advantage of this feature and stop inserting empty elements at the end of the list ... it is an unnecessary burden to deal with that last element.

    
answered by 30.10.2017 / 07:50
source