Advertisement
CamolaZ

Leitura de Clientes crasha

May 16th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1.  
  2. vector<Cliente> readclientetxt();
  3. // files reading
  4. vector<Cliente> clientes = readclientetxt();
  5.  
  6. vector<Cliente> readclientetxt() //save the file clientes.txt in one vector of clientes (int ; string ; double)
  7. {
  8.     vector<Cliente> clienteslista;//vector that is returned in the end (which is going to have Clients.txt info)
  9.     ifstream fichClientes;//ifstream to store the text file, and then extract the information from it
  10.     fichClientes.open("clientes.txt");//store the text file in the ifstream
  11.     if (!fichClientes)//error message in case of non-existence of the file
  12.         cerr << "ERROR 404 - Clients file not found";
  13.  
  14.     while (!fichClientes.eof())//cicle to create the vector (ends in end of file)
  15.     {
  16.         Cliente clienteauxdata(fichClientes);//create the cliente which is going to store the info of one line
  17.         string clienteinfo; //string to store the line
  18.         int numCliRead; // number of clients to read
  19.         string numClientesR;
  20.         getline(fichClientes, numClientesR); // Define conditions if a user don't digit a number
  21.         numCliRead = stoi(numClientesR, nullptr, 10);
  22.         while (numCliRead)
  23.         {
  24.             getline(fichClientes, clienteinfo);//store the line in clientinfo, next time its called, reads the next line
  25.             string idRead;//string which stores id information
  26.             string nameRead;//string which stores name information
  27.             string amountRead; //string which stores amount information
  28.             int stringcounter = 0; //store string character place
  29.             for (int i = 0; clienteinfo[i] != ' '; i++)//cicle to read id , lê até encontrar o primeiro espaço char(20) é o espaço!
  30.             {
  31.                 idRead = idRead + clienteinfo[i];
  32.                 stringcounter = i;
  33.             }
  34.             for (int k = stringcounter + 3; clienteinfo[k] != ';'; k++)//cicle to read name
  35.             {
  36.                 nameRead = nameRead + clienteinfo[k];
  37.                 stringcounter = k;
  38.             }
  39.             nameRead = nameRead.substr(1, nameRead.size() - 2);
  40.             for (int j = stringcounter + 2; clienteinfo[j]; j++)//cicle to read amount
  41.             {
  42.                 amountRead = amountRead + clienteinfo[j];
  43.             }
  44.             clienteauxdata.setId(idRead);//convert string to int
  45.             clienteauxdata.setNome(nameRead);
  46.             clienteauxdata.setVolCompras(amountRead);//convert string to double
  47.             //create the client
  48.             clienteslista.push_back(clienteauxdata);//store client in vector next line
  49.  
  50.         }
  51.     }
  52.     return clienteslista;//return the vector
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement