1

I have file with records separated by comas:

cities.txt:

1,NYC
2,ABQ
...

I would like to iterate over each row: ids and names. I have created the code:

#include <iostream>
#include <string>
using namespace std;

class City {
    int id;
    string name;

public:
    City() {}
    City(int id, int name)
    {
        this->id = id;
        this->name = name;
    }

    void load_file()
    {
        ifstream v_file("cities.txt");
        if (v_file.is_open()) {
            while (!v_file.eof()) {
            //...
            }
        }
        v_file.close();
    }
}

int main()
{
    City array_city[1000];

    array_city.load_file();

    return 0;
}

Could you tell me how to load all rows to array array_city and iterate over it? I don't know what to place in while block in load_file method. I don't know weather, the method load_file should have void type. Unfortunately I have to do it on arrays.

gsamaras
  • 66,800
  • 33
  • 152
  • 256
Qwe Poian
  • 69
  • 9
  • 3
    Aren't you be supposed to do your homework yourself? –  May 18 '17 at 08:52
  • 1
    Arrays are not classes, they cannot have methods. You need to return the array from the load function, likely with `std::vector` – Passer By May 18 '17 at 08:53
  • 1
    @manni66 there are [rules](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) on homework questions, they are not completely forbidden. That said OP should clarify that this is an assignment if it is – Passer By May 18 '17 at 08:54
  • Figure out how to read the file line by line. google is your friend. Then figure out what you have to do next. – nakiya May 18 '17 at 09:02
  • Two things: 1) don't close the file your self, rely on RAII to do it for you. 2) Have a look at http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Jonas May 18 '17 at 09:04
  • Thanks guys. I know now how to add lines to City array_city but who to iterate over it? E.G display all names from array_city? – Qwe Poian May 18 '17 at 09:11

1 Answers1

1

It's not a good idea to use EOF in a while loop. Read more in Why is iostream::eof inside a loop condition considered wrong?


In , vectors should be preferred over arrays. However, your teacher knows something more to suggest using an array here. For that reason I am providing a solution with an array:

  1. Read the file line by line
  2. Extract the id and the string
  3. Assign it to the i-th cell of the array

Code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class City {
    int id;
    string name;

public:
    City() {}
    City(int id, string name) : id(id), name(name)
    {
    }
    void print()
    {
        cout << "ID = " << id << ", name = " << name << endl;
    }
};

void load_file(City* cities, const int n)
{
    ifstream v_file("cities.txt");
    if (v_file.is_open()) {
        int number, i = 0;
        string str;
        char c;
        while (v_file >> number >> c >> str && c == ',' && i < n)
        {
            //cout << number << " " << str << endl;
            cities[i++] = {number, str};
        }
    }
    v_file.close();
}

int main()
{
    City cities[4]; // assuming there are 4 cities in the file
    load_file(cities, 4);
    for(unsigned int i = 0; i < 4; ++i)
        cities[i].print();

    return 0;
}

Same solution with std::vector, if you are interested. =) If you haven't been taught about them, I suggest you skip that part and come back later when you do that in the course.

Use a vector of City. Read the file line by line, and push back into the vector every line you read, by constructing an instance of your class, and you are done!

Example:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

class City {
    int id;
    string name;

public:
    City() {}
    City(int id, string name) : id(id), name(name)
    {
    }
    void print()
    {
        cout << "ID = " << id << ", name = " << name << endl;
    }
};

void load_file(vector<City>& cities)
{
    ifstream v_file("cities.txt");
    if (v_file.is_open()) {
        int number;
        string str;
        char c;
        while (v_file >> number >> c >> str && c == ',' && i < n)
        {
            //cout << number << " " << str << endl;
            cities.push_back({number, str});
        }
    }
    v_file.close();
}

int main()
{
    vector<City> cities;
    load_file(cities);
    for(unsigned int i = 0; i < cities.size(); ++i)
        cities[i].print();

    return 0;
}

Input:

Georgioss-MacBook-Pro:~ gsamaras$ cat cities.txt 
1,NYC
2,ABQ
3,CCC
4,DDD

Output:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
ID = 1, name = NYC
ID = 2, name = ABQ
ID = 3, name = CCC
ID = 4, name = DDD
Community
  • 1
  • 1
gsamaras
  • 66,800
  • 33
  • 152
  • 256