Function problem

Discussion in 'C++' started by mmondal71, May 30, 2010.

  1. mmondal71

    mmondal71 New Member

    Joined:
    May 25, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    While reading from the file, the function reads everything but prints only the first snackcart and the third snackcart twice. I have no idea why.
    Code:
    int loadInfoCart(string filename, vector<SnackCart*> &cart, int &cartCount,SnackCart *carts) 
    {
      cart.push_back(new SnackCart);
      ifstream dataFile(filename.c_str());
      cartCount = 0;
      string type, owner;
      while (!dataFile.eof()){
    	  getline(dataFile, type);
      	  if (type == "Snackcart"){
    		  cart.push_back(carts);
    		  getline(dataFile,owner);
    		  cart[cartCount]->setOwner(owner);
    		  string location;
    		  getline(dataFile,location);      
    		  cart[cartCount]->setLocation(location);      
    		  cart[cartCount]->clearMeals();
    		  bool moreMeals = true;
    		  while (moreMeals) {
    		  string meal;
    		  getline(dataFile,meal);
    		  if (meal == "#") { 
    		  moreMeals = false;
    		  } else {
    				string numStr;
    				getline(dataFile,numStr);
    				float num = (float) atof(numStr.c_str());
    				string ratingStr;
    				getline(dataFile,ratingStr);
    				cart[cartCount]->loadaddMeal(meal,num,ratingStr);
    				}
    		  }
    	   	++cartCount;
      }
    	  if (dataFile.eof())
    		  break;
      }
      dataFile.close();
      return cartCount;
      }
    

    text file

    Snackcart
    nik
    city
    chips
    2
    Average
    coke
    1.8
    Good
    #
    Snackcart
    nikhil
    cairns
    roll
    2
    Good
    sandwich
    6
    Bad
    #
    Snackcart
    juan
    brisbane
    muffin
    2
    Bad
    crisps
    3
    Average
    #
    Restaurant
    Mark
    30, Sheridan Street
    40221141
    9am-10pm
    Pizza
    15
    Good
    Steak
    20
    Average
    #
    Restaurant
    Steve
    12,Lake Street
    12345678
    11am-11pm
    rice
    4
    Good
    noodles
    23
    Bad
    #

    function to display data
    Code:
    // tell cart objects to display themselves
    void displayCarts(vector<SnackCart*> cart, int count) {
      if (count == 0) {
        cout << "Error: no carts added or loaded yet" << endl;
        return;
      }
      
        cout << *cart[1];
        cout << endl;
      
    }
     
  2. pkbis28

    pkbis28 New Member

    Joined:
    May 25, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    void displayCards( const vector<SnackCart*>& carts ) {
       if( carts.empty() ) {
          std::cout << "Error: no cards added or loaded yet" << std::endl;
          return;
       }
    
       for( vector<SnackCart*>::const_iterator c = carts.begin(), c != carts.end(); ++c )
           std::cout << *c << std::endl;
    }


    (It could be simplified even further with boost::lambda, but I won't even go there).
     
  3. mmondal71

    mmondal71 New Member

    Joined:
    May 25, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    sorry this is how the function really is.
    the << has been overloaded to display details of the respective cart objects.

    sorry this is how the function really is.
    the << has been overloaded to display details of the respective cart objects.

    Code:
    void displayCarts(vector<SnackCart*> cart, int count) {
      if (count == 0) {
        cout << "Error: no carts added or loaded yet" << endl;
        return;
      }
      
      for (int i = 0; i < count; ++i){
        cout << *cart[i];
        cout << endl;
      }
    }
     
  4. mmondal71

    mmondal71 New Member

    Joined:
    May 25, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    dereferencing of c is still displaying the address and not the contents at the address and from that point on. also 3 of the addresses are still the same. so I'm guessing it hasn't worked. I have read through my code loads of times. but i still cant find the source of the error. :(
    appreciate the help though :)
     
  5. spoddar66

    spoddar66 New Member

    Joined:
    May 25, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    c is an iterator. To "get at" the element referenced by c, you just dereference c. That is the first dereference.
    The element is actually a pointer to SnackCart (ie, vector<SnackCart*>). We have to dereference that
    pointer also to get at the actual SnackCart instance.
     
  6. mmondal71

    mmondal71 New Member

    Joined:
    May 25, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    i figured out why it wasn't working. It was because on line 7 i wasn't creating new SnackCart type objects to the vector and hence it only displayed every snackcart it came across after a certain gap. Thanks again for all the help :)
     
  7. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    hey guys, please put C C++ program codes in a
    tag.
    :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice