Greetings from Portugal

I've been stuck for some hours now, since I can't get my n00b code to work as I want. Could someone give me a hint of what to do or what am I doing wrong?

I have a file that writes something like this:

1001,Loja1,10
1002,Loja2,20

And I want to separatedely get the values separated by the ',' so here's what I've done so far

if(!fileLoja)
        cout<<"Deu Asneira";
    while(!fileLoja.eof())
       {
           getline(fileLoja,str,'\n');
           if(str.size()>0)
           {
               int inicio = 0;
               int pos = str.find(str,inicio);
               string chave = str.substr(inicio, pos-inicio);
               char *aux1= &chave[0];
               int key = atoi(aux1);
             
               pos++;
               inicio = pos;
               pos = str.find(',',inicio);
               string descr = str.substr(inicio, pos-inicio);
               
               inicio = pos;
               pos = str.find(',',inicio);
               string qenc = str.substr(inicio, pos-inicio);
               char *aux = &qenc[0];
               int quenc = atoi(aux);
               pos++;
           }
    }

Now, how can I get it to write to some array out "coutIng" something to the screen?

Cheers,

João

i don't really understand the question, if you're asking how to output an array via stream (or anything i suppose) then you need to make a loop for each element

something i noticed:

lines 11 / 22 are unnecessary because strings have a c_str() member which returns const char*

Sorry for the missunderstanding. I want to be able to pick the values that the substr gets from the line string and output them through an array or direct cout with of course, some kind of loop

Edited 6 Years Ago by fantasma: n/a

you can have the function return a string vector/array, or create multiple functions which return a string

Thank you for your reply ixmike88, its defenitly a solution, but, if I wanted to have that code in the main function whitout having it in a function how would I do it??

Working... but not as I expected

If I order to print "key" it will print to the console something like

"00010010001002000"

Any idea why?

Thanks in advance for your patience ;)

Edited 6 Years Ago by fantasma: n/a

Since we can't see what you tried, and, to be honest, I have no idea what you're trying to do since cout is so easy to use, back up and give us a clear, precice explanation with an clear example.

So let me try to be a little more specific. Printing to the terminal is just a mean to an end.

I Want to be able to read 2 files I've created and then, put them in a linked list.

The thing is, the values that are beeing outputed by the read are wrong.

I have 2 files like this

(They're a bit diferent from the first post, so I have worked on them a little bit)

Loja.txt:
2002,Loja 1,Quantidade Encomendada:20
2003,Loja 2,Quantidade Encomendada:30

Armazemns.txt
100002,Armazem 1,24-BC-23,8500,24-EV-51,10000
100003,Armazem 2,24-EV-51,10000

I want to be able to read them and then put them in the following List

#pragma once


#include "No.h"

using namespace std;


template<class T>
class Lista {
private:

    No<T> *cabeca;

    void destroilista();

public:
    Lista();
    Lista(const Lista<T>& l);
    ~Lista();
    bool vazia() const;
    void insere(int k, const T & elem);
    void remove(int k, T &x);
    void encontra(int k, T &x) const;
    int comprimento() const;

    const Lista<T> & operator =(const Lista<T> & l);

    void escreve(ostream& out) const;

};

template <class T>
Lista<T>::Lista() {
    cabeca = NULL;
}

template <class T>
Lista<T>::Lista(const Lista<T>& l) {
    No<T> * apno = new No<T>;

    if (l.cabeca)
        cabeca = apno;
    else
        cabeca = NULL;

    No<T> * apaux = l.cabeca;

    while (apaux) {
        apno->info = apaux->info;
        if (apaux->prox == NULL) {
            apno->prox = NULL;
            apaux = apaux->prox;
        } else {
            apno->prox = new No<T>;
            apno = apno->prox;
            apaux = apaux->prox;
        }
    }
}

template <class T>
Lista<T>::~Lista() {
    destroilista();
}

template <class T>
void Lista<T>::destroilista() {
    No<T> * temp;

    while (cabeca) {
        temp = cabeca->prox;
        delete cabeca;
        cabeca = temp;
    }
    cabeca = NULL;
}

template<class T>
bool Lista<T>::vazia() const {
    return (cabeca == NULL);
}

template<class T>
void Lista<T>::insere(int k, const T & elem) {
    if (k <= 0)
        cout << "Posicao invalida ! \n";
    else {
        No<T>* apno = new No<T>;
        apno->info = elem;

        No<T> *temp = cabeca;

        if (k == 1) {
            apno->prox = temp;
            cabeca = apno;
        } else {
            int i = 1;
            while (i < k - 1 && temp) {
                temp = temp->prox;
                i++;
            }
            if (temp) {
                apno->prox = temp->prox;
                temp->prox = apno;
            }
        }
    }
}

template<class T>
int Lista<T>::comprimento() const {
    No<T> *temp;

    int comp = 0;

    temp = cabeca;
    while (temp) {
        comp++;
        temp = temp->prox;
    }
    return comp;
}

template<class T>
void Lista<T>::encontra(int k, T &x) const {
    int comp = comprimento();

    if (k > 0 && k <= comp) {
        No<T> * apt = cabeca;

        for (int i = 1; i < k; i++)
            apt = apt->prox;

        if (apt)
            x = apt->info;
    }
}

template<class T>
void Lista<T>::remove(int k, T &x) {
    if (k <= 0 || k > comprimento())
        cout << "Posicao invalida ! \n";
    else {
        No<T> * act = cabeca;
        No<T> * ant = NULL;

        for (int i = 1; i < k && act; i++) {
            ant = act;
            act = act->prox;
        }

        if (ant)
            ant->prox = act->prox;
        else
            cabeca = cabeca->prox;

        x = act->info;
        delete act;
    }
}

template <class T>
const Lista<T>& Lista<T>::operator =(const Lista<T> &l) {

    if (this == &l)
        return *this;

    destroilista();

    No<T> * apaux = l.cabeca;
    int i = 1;

    while (apaux != NULL) {
        insere(i, apaux->info);
        i++;
        apaux = apaux->prox;
    }
    return *this;
}

template <class T>
void Lista<T>::escreve(ostream& out) const {
    No<T> *temp = cabeca;

    if (cabeca)
        do {
            out << temp->info << " ";
            temp = temp->prox;

        } while (temp);
    else
        out << "Lista Vazia";

    out << endl;
}


// overload <<

template <class T>
ostream & operator <<(ostream& out, const Lista<T>& l) {
    l.escreve(out);
    return out;
}

So far, what I have in the main.ccp is this:

int main(int argc, char** argv) {

  
    ifstream fileLoja("Loja.txt");
    ifstream fileArm("Armazens.txt");
    string linha;
    Lista <Edificio> L;
    Loja L1;

    if(!fileLoja)
        cout<<"Deu Asneira";
    while(!fileLoja.eof())
       {
          if(!getline(fileLoja, linha, '\n' ))
          {
              if (linha.size() > 0)
		{
                    int inic=0;
                    //while(linha.find(',',inic))
                    //{

			int pos = linha.find(',',inic);
			string key_l(linha.substr(inic, pos-inic));
			
			int key_loj = atoi(key_l.c_str());
                        cout << key_loj;
			pos++;
                        
			inic = pos;
                        pos= linha.find(',',inic);
			string descri_loj(linha.substr(inic,  pos-inic));
			pos++;
                 
                       
			inic = pos;
			pos = linha.find(',',inic);
			string quant_e(linha.substr(inic,  pos-inic));
			
			int quant_enc = atoi(quant_e.c_str());
                        pos++;
              

                        L1.setChave(key_loj);
                        L1.setDesig(descri_loj);
                        L1.setQt(quant_enc);

                       }
                      
		}
    }

//    cout << L1;

    cout << L1;

    L.insere(1,L1);
    cout << L;
    return 0;

If you need any more explanation please, feel free to shoot :)

Thanks in advance

This article has been dead for over six months. Start a new discussion instead.