-
problem with classes
I've been trying to seewhat's going on with this program, but can't find anything at all. It compiles but when I execute it nothing appears on the screen.
This is CCentroComercial.h
Code:
#include <vector>
#include <string>
using namespace std;
class LOJA {
public:
string nome;
string tipo;
int renda;
};
class CCentroComercial {
private:
vector<LOJA> lojas;
public:
void ler_lojas(const char *nome);
void imprimir_lojas(ostream &output, const string& tipo);
void imprimir_lojas(ostream &output);
void ordenarLojas();
};
and this my main program
Code:
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "CCentroComercial.h"
using namespace std;
void CCentroComercial::ler_lojas(const char *nome) {
ifstream ficheiro;
ficheiro.open(nome);
while(!ficheiro.get()) {
LOJA x;
ficheiro >> x.nome >> x.tipo >> x.renda;
lojas.push_back(x);
}
}
void CCentroComercial::imprimir_lojas(ostream &output, const string& tipo) {
for (int i=0; i<lojas.size(); i++) {
if (lojas[i].tipo == tipo) {
output << lojas[i].nome << " ; " << lojas[i].tipo << " ; " << lojas[i].renda << endl; }
}
}
void CCentroComercial::imprimir_lojas(ostream &output) {
for (int i=0; i<lojas.size(); i++) {
output << lojas[i].nome << " ; " << lojas[i].tipo << " ; " << lojas[i].renda << endl; }
}
int main(int argc, char *argv[])
{
char *s = "teste.txt";
CCentroComercial X;
X.ler_lojas(s);
X.imprimir_lojas(cout);
system("PAUSE");
}
The content of teste.txt:
MacDonalds
FastFood
1000
Zara
Roupa
5000
As I said, nothing comes up in the screen when I run it. :S
-
>> while(!ficheiro.get())
This will run while get() returns 0, but get() will not return 0, it will return the character code of the next character in the stream. So the first time through, it returns the character code for 'M' which is not 0. This will break the loop and nothing will be read in and added to the vector.
You should probably use
Code:
while (ficheiro >> x.nome >> x.tipo >> x.renda)
Either that or read into temporary variables and then assign those values to the LOJA object before pushing it on to the vector.
-
Yeah you were right, it works now. Thanks. :)
One more thing... If I change the 2nd line of teste.txt, FastFood to Fast Food it won't work.
Well I know that it skips the spaces, tabs, line changes..
How can I do this with the space between Fast and Food to make it work, in order to have in nome variable McDonalds, tipo is Fast Food and renda is 1000.
-
Nevermind, I got it.
Getline() function.
Close topic please.
-
Be careful with mixing operator>> (which you use for the "renda") and getline. Your second time through the loop you might not get a value for the first line because operator>> leaves the newline in the stream and getline stops when it reads it.
Your first code used only operator>> which is fine because operator>> skips initial whitespace like newlines. Also, if you used only getline it would be fine as well because getline discards the newline after it reads a line. Only when you use operator>> which leaves the newline there, and then getline which fails to ignore it, do you have a problem.
A common solution is to use ignore() to ignore the newline after you use operator>>.