problem with classes

This is a discussion on problem with classes within the C++ Programming forums, part of the General Programming Boards category; I've been trying to seewhat's going on with this program, but can't find anything at all. It compiles but when ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    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.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    Nevermind, I got it.
    Getline() function.

    Close topic please.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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>>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 05:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21