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