The file that I am reading from contains data about cities. I am wondering how I can extract just the data I want and apply it to my city objects. Below is my city class.
#include <iostream>
#include <string>
#include <cstdlib>
#include "City.h"
using namespace std;
/*
* Define the constructor for the class
*/
City::City(string city_name, string state, int longitude, int latitude, int population) {
this->city_name = city_name;
this->state = state;
this->longitude = longitude;
this->latitude = latitude;
this->population = population;
}
/*
*Accessors
*/
string City::getCity() {
return city_name;
}
string City::getState() {
return state;
}
int City::getLongitude() {
return longitude;
}
int City::getLatitude() {
return latitude;
}
int City::getPopulation() {
return population;
}
/*
* Mutators
*/
void City::setCity(string city_name) {
this->city_name = city_name;
}
void City::setState(string state) {
this->state = state;
}
void City::setLongitude(int longitude) {
this->longitude = longitude;
}
void City::setLatitude(int latitude) {
this->latitude = latitude;
}
void City::setPopulation(int population) {
this->population = population;
}
/*
* Sorting methods
*/
void City::sortByCity() {
// Code to sort cities by city name
}
void City::sortByLongitude() {
// Code to sort cities by longitude
}
Here is an example of the type of text the file contains that I want to read from
1063061|OH|Tobias|ppl|Marion|39|101|404118N|0830359W|40.68833|-83.06639|||||985|||Monnett
1063062|OH|Todds|ppl|Morgan|39|115|393129N|0815049W|39.52472|-81.84694|||||983|||Stockport
My question is how do I exclude the ‘|’ characters from my file input stream? As well as how do I extract only the strings that I need. (Ex. Tobias as city_name or OH for state) in order to create my city objects. Thank You
use getline with delimiter character ‘|’ and write an inputting sequence adapted to your formatted input data