15.4 Zip code and population (class templates)
Complete the TODOs in StatePair.h and main.cpp.
StatePair.h
Define a class StatePair with two template types (T1 and T2), and constructor, mutators, accessors, and PrintInfo() member functions which will work correctly with main.cpp. Note, the three vectors (shown below) have been pre-filled with StatePair data in main() and do not require modification.
main.cpp
Complete main() to:
For FULL CREDIT on this assignment, the member functions for StatePair MUST be written outside the class StatePair { ... }; context as indicated by the TODO comments.
Ex: If the input is: 21044 the output is: Maryland: 6079602
Here's the code:
//StatePair.h
#ifndef STATEPAIR
#define STATEPAIR
template<typename T1, typename T2>
class StatePair {
// TODO: Define signatures ONLY for a constructor,
mutators,
// and accessors for StatePair which work with main.cpp
// as written
// TODO: Define a signature for the PrintInfo() method
// TODO: Define data member(s) for StatePair
};
// TODO: Complete the implementations of the StatePair methods
#endif
//main.cpp
#include<iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;
int main() {
ifstream inFS; // File input stream
int zip;
int population;
string abbrev;
string state;
unsigned int i;
// ZIP code - state abbrev. pairs
vector<StatePair <int, string>>
zipCodeState;
// state abbrev. - state name pairs
vector<StatePair<string, string>>
abbrevState;
// state name - population pairs
vector<StatePair<string, int>>
statePopulation;
// Fill the three ArrayLists
// Try to open zip_code_state.txt file
inFS.open("zip_code_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file
zip_code_state.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <int, string>
temp;
inFS >> zip;
if (!inFS.fail()) {
temp.SetKey(zip);
}
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetValue(abbrev);
}
zipCodeState.push_back(temp);
}
inFS.close();
// Try to open abbreviation_state.txt file
inFS.open("abbreviation_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file
abbreviation_state.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <string, string>
temp;
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetKey(abbrev);
}
getline(inFS, state); //flushes
endline
getline(inFS, state);
state = state.substr(0,
state.size()-1);
if (!inFS.fail()) {
temp.SetValue(state);
}
abbrevState.push_back(temp);
}
inFS.close();
// Try to open state_population.txt file
inFS.open("state_population.txt");
if (!inFS.is_open()) {
cout << "Could not open file
state_population.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <string, int>
temp;
getline(inFS, state);
state = state.substr(0,
state.size()-1);
if (!inFS.fail()) {
temp.SetKey(state);
}
inFS >> population;
if (!inFS.fail()) {
temp.SetValue(population);
}
getline(inFS, state); //flushes
endline
statePopulation.push_back(temp);
}
inFS.close();
cin >> zip;
for (i = 0; i < zipCodeState.size(); ++i) {
// TODO: Using ZIP code, find state
abbreviation
if(zipCodeState.at(i).SetValue() ==
zip)
{
abbrev = zipCodeState.at(i).SetValue();
break;
}
}
for (i = 0; i < abbrevState.size(); ++i) {
// TODO: Using state abbreviation,
find state name
if(abbrevState.at(i).SetValue() ==
abbrev){
state = abbrevState.at(i).SetValue();
break;
}
}
for (i = 0; i < statePopulation.size(); ++i)
{
// TODO: Using state name, find
population. Print pair info.
if(statePopulation.at(i).SetValue()
== state){
statePopulation.at(i).PrintInfo();
break;
}
}
}
// StatePair.h
#ifndef STATEPAIR
#define STATEPAIR
#include <iostream>
using namespace std;
template<typename T1, typename T2>
class StatePair {
private:
T1 key;
T2 value;
public:
// Define signatures ONLY for a constructor,
mutators,
// and accessors for StatePair which work with
main.cpp
// as written
// default constructor to initialize the key and value
to default values
StatePair()
{}
// mutators
// function to set the key
void SetKey(T1 key)
{
this->key = key;
}
// function to set the value
void SetValue(T2 value)
{
this->value = value;
}
// accessors
// function to return key
T1 GetKey() { return key;}
// function to return value
T2 GetValue() { return value;}
// Define PrintInfo() method
// function to display key and value
void PrintInfo()
{
cout<<key<<" : "<<value<<endl;
}
};
#endif
//end of StatePair.h
// main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;
int main()
{
ifstream inFS; // File input stream
int zip;
int population;
string abbrev;
string state;
unsigned int i;
// ZIP code - state abbrev. pairs
vector<StatePair <int, string>> zipCodeState;
// state abbrev. - state name pairs
vector<StatePair<string, string>> abbrevState;
// state name - population pairs
vector<StatePair<string, int>> statePopulation;
// Try to open zip_code_state.txt file
inFS.open("zip_code_state.txt");
if (!inFS.is_open()) {
cout <<
"Could not open file zip_code_state.txt." << endl;
return 1; // 1
indicates error
}
while (!inFS.eof()) {
StatePair <int, string>
temp;
inFS >> zip;
if (!inFS.fail()) {
temp.SetKey(zip);
}
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetValue(abbrev);
}
zipCodeState.push_back(temp);
}
inFS.close();
// Try to open abbreviation_state.txt file
inFS.open("abbreviation_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file
abbreviation_state.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <string, string>
temp;
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetKey(abbrev);
}
getline(inFS, state); //flushes
endline
getline(inFS, state);
state = state.substr(0,
state.size()-1);
if (!inFS.fail()) {
temp.SetValue(state);
}
abbrevState.push_back(temp);
}
inFS.close();
// Try to open state_population.txt file
inFS.open("state_population.txt");
if (!inFS.is_open()) {
cout << "Could not open file
state_population.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <string, int>
temp;
getline(inFS, state);
state = state.substr(0,
state.size()-1);
if (!inFS.fail()) {
temp.SetKey(state);
}
inFS >> population;
if (!inFS.fail()) {
temp.SetValue(population);
}
getline(inFS, state); //flushes
endline
statePopulation.push_back(temp);
}
inFS.close();
cin >> zip;
// Using ZIP code, find state abbreviation
// loop over zipCodeState vector, find the state
abbreviation with zipcode zip
for (i = 0; i < zipCodeState.size(); ++i) {
if(zipCodeState.at(i).GetKey() ==
zip) // zip found
{
abbrev =
zipCodeState.at(i).GetValue(); // set abbrev to abbreviation and
exit the loop
break;
}
}
// Using state abbreviation, find state name
// loop over abbrevState vector, find the state name
for the given abbreviation
for (i = 0; i < abbrevState.size(); ++i) {
if(abbrevState.at(i).GetKey() == abbrev) // abbrev found, set state
to state name for abbreviation and exit the loop
{
state =
abbrevState.at(i).GetValue();
break;
}
}
// Using state name, find population. Print pair
info.
// loop over statePopulation vector to find the
population for state name
for (i = 0; i < statePopulation.size(); ++i)
{
if(statePopulation.at(i).GetKey()
== state) // state name found, display the information and exit the
loop
{
statePopulation.at(i).PrintInfo();
break;
}
}
return 0;
}
//end of main.cpp
Get Answers For Free
Most questions answered within 1 hours.