0

I am trying to learn more about vectors and store objects in it. I am reading data from the txt file. I cannot see what mistake I'm making that it does not work.

Here are my main methods

void Reader::readFoodFile() {
    string name;
    int cal, cost;
    ifstream file;
    file.open("food.txt");
    while (file >> name >> cal >> cost) {
        Food f(name, cal, cost);
        foodStorage.push_back(f); 

    } 
}
void Reader::getStorage() {
    for (unsigned int i = 0; i < foodStorage.size(); i++) {
        cout << foodStorage[i].getName();
    }
}

And here is my Food constructor:

Food::Food(string newName, int newCalories, int newCost) {
    this->name = newName;
    this->calories = newCalories;
    this->cost = newCost;
}

In my main.cpp file I'm just creating the object Reader(no constructor now) and call the methods.

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

        Reader reader;
        reader.readFoodFile();
        reader.getStorage();
}

I want to populate the Vector with objects that take data from the txt file and then print it out(for now).

Any suggestions?

edit; my .txt file layout is

apple 4 2
strawberry 2 3
carrot 2 2

Here is my Food.h and Reader.h

#ifndef FOOD_H
#define FOOD_H

#include <string> 
#include <fstream>
#include <iostream>

using namespace std;

class Food {
public:
    Food();
    Food(string, int, int);
    Food(const Food& orig);
    virtual ~Food();
    string getName();
    int getCalories();
    int getCost();
    void setCost(int);
    void setCalories(int);
    void setName(string);
    int calories, cost;
    string name;
private:

};

#endif  /* FOOD_H */`

and Reader.h
`#ifndef READER_H
#define READER_H
#include <string> 
#include <fstream>
#include <iostream>
#include <vector>
#include "Food.h"

using namespace std;

class Reader {
public:
    Reader();
    Reader(const Reader& orig);
    virtual ~Reader();

    void readFoodFile();
    void getStorage();
    vector<Food> foodStorage;

private:

};

#endif  /* READER_H */
ChrisA
  • 25
  • 1
  • 7

2 Answers2

0

I'm guessing your exe is running with a different relative path to the one you expect. Check that file.good() is true after you call file.open(). This problem is common when the programming IDE (e.g. Visual Studio) runs the exe with a different working folder to where the exe is being written.

axon
  • 1,190
  • 6
  • 16
0

I just compiled your code and it works fine for me....I used g++.... Things you need to fix ..

  1. get rid of the constructors you have in .h file and not defined in .cpp
  2. give an implementation of getName.

The following is my code:

food.h

#ifndef FOOD_H 
#define FOOD_H

#include <string> 
#include <fstream> 
#include <iostream>

using namespace std;

class Food { public:
    //Food();
    Food(string, int, int);
    //Food(const Food& orig);
    //virtual ~Food();
    string getName();
    int getCalories();
    int getCost();
    void setCost(int);
    void setCalories(int);
    void setName(string);
    int calories, cost;
    string name; private:

};

#endif

food.cpp

#include<iostream> 
#include<string> 
#include "food.h" 
using namespace std; 
 Food::Food(string newName, int newCalories, int newCost) {
    this->name = newName;
    this->calories = newCalories;
    this->cost = newCost; } string Food::getName() {
        return name; }

reader.h

#ifndef READER_H 
#define READER_H 
#include <string> 
#include <fstream> 
#include <iostream> 
#include <vector> 
#include "food.h"

    using namespace std;

    class Reader { public:
        //Reader();
        //Reader(const Reader& orig);
        //virtual ~Reader();

        void readFoodFile();
        void getStorage();
        vector<Food> foodStorage;

    private:

    };

    #endif

reader.cpp

#include <iostream> 
#include "reader.h"

using namespace std; void Reader::readFoodFile() {
    string name;
    int cal, cost;
    ifstream file;
    file.open("food.txt");
    while (file >> name >> cal >> cost) {
        Food f(name, cal, cost);
        foodStorage.push_back(f);

    } } void Reader::getStorage() {
    for (unsigned int i = 0; i < foodStorage.size(); i++) {
        cout << foodStorage[i].getName();
    } }

main.cpp

#include<iostream>
#include<fstream>
#include "food.h"
#include "reader.h"

using namespace std;

int main(int argc, char** argv) {
        Reader reader;
        reader.readFoodFile();
        reader.getStorage();
}

I compiled using -- g++ main.cpp reader.cpp food.cpp

and the output I get -- applestrawberrycarrots

So, I am not sure what you are missing....hope this helps

I did not add std::endl .. if you do add that where you have std::cout then each item will be on their own line. Also, you should close the stream after you are done using it.

Bill
  • 5,263
  • 6
  • 35
  • 50
  • Thank you very much. The removal of ` Food(); Food(const Food& orig); virtual ~Food();` Helped me to solve the problem. I'll keep that in mind for the future! – ChrisA May 02 '13 at 10:29
  • Glad it helped...whenever you define a constructor in `.h` but dnt give a definition, c++ will complain `undefined reference` as it does not understand how to instantiate the constructor (http://stackoverflow.com/questions/5393293/undefined-reference-to-classclass) – Bill May 02 '13 at 12:26