-2

Lets say I have a .txt file that contains names and values as follows:

Seattle

10.5 11 12 16

Chicago

12 13.5 14 18
Quebec

20 18 19 11.5

how can I read from the text file to get the name and the values for each city separately without using arrays. Thanks.

Ron
  • 7
  • 1
  • 4
  • It is unlikely you will get help with this question. People don't tend to do other's homework for them. You should give it a try and then ask a question when you get stuck. But you should be able to [google opening a file and reading values from it](https://www.google.com/search?q=c%2B%2B+open+a+file&rlz=1C5CHFA_enUS917US919&oq=c%2B%2B+open+a+file&aqs=chrome..69i57j0i512l4j0i22i30l5.3861j0j7&sourceid=chrome&ie=UTF-8). – Martin York Nov 29 '21 at 21:48
  • Instead of an array you should use a class and a std::vector. An array should only be used when you know exactly how many items there will be in the file. – drescherjm Nov 29 '21 at 21:51
  • I tried reading values from the text file but I was just able to get name using getline and the values using a loop for the first city and now I am stuck on how to continue to get the name of the second city and its values – Ron Nov 29 '21 at 21:53
  • You will need to use a loop. You may run into this also: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – drescherjm Nov 29 '21 at 21:54
  • The likelihood of you getting help fixing your code greatly goes up if you add it to the question as text. See the console.log() example here for how to format a code block: [https://stackoverflow.com/help/formatting](https://stackoverflow.com/help/formatting) – drescherjm Nov 29 '21 at 21:56
  • Can other containers be used instead of array: `std::list`, `std::map`, etc? – Thomas Matthews Nov 29 '21 at 22:43

1 Answers1

2

You can use a string to read in the city name:

std::string city_name;
std::getline(std::cin, city_name);

You can use 4 variables to read in the next values:

double a, b, c, d;
std::cin >> a >> b >> c >> d;
std::cin.ignore(100000, '\n');  // Align to next line.

Notice, in the above code no arrays or vectors are used.

Edit 1: Records & Database
Usually, the preferred method in this situation is to use a class or struct to model the records or lines.

struct Record  
{
  std::string city_name;
  double      a, b, c, d;
  friend std::istream& operator>>(std::istream& input, Record& r);
};

std::istream& operator>>(std::istream& input, Record& r)
{
    std::getline(input, r.city_name);
    input >> r.a >> r.b >> r.c >> r.d;
    input.ignore(10000, '\n');
    return input;
}

Your input could be written as:

std::vector<Record> database;
Record r;
// code for opening file goes here.
while (my_input_file >> r)
{
    database.push_back(r);
}

Edit 2: Fetching the city names
All the records are read into the database variable.
You can print the city names using:

const unsigned int quantity = database.size();
for (unsigned int i = 0U; i < quantity; ++i)
{
    std::cout << database[i].city_name << "\n";
}

Other fields can be accessed in a similar manner:
database[record_ID].field_name

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154