Define a class StatePair with two template types (T1 and T2), constructors, mutators, accessors, and a PrintInfo() method. Three vectors have been pre-filled with StatePair data in main(): vector> zipCodeState: ZIP code - state abbreviation pairs vector> abbrevState: state abbreviation - state name pairs vector> statePopulation: state name - population pairs Complete main() to use an input ZIP code to retrieve the correct state abbreviation from the vector zipCodeState. Then use the state abbreviation to retrieve the state name from the vector abbrevState. Lastly, use the state name to retrieve the correct state name/population pair from the vector statePopulation and output the pair.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Define a class StatePair with two template types (T1 and T2), constructors, mutators, accessors, and a PrintInfo() method. Three vectors have been pre-filled with StatePair data in main():

  • vector<StatePair <int, string>> zipCodeState: ZIP code - state abbreviation pairs
  • vector<StatePair<string, string>> abbrevState: state abbreviation - state name pairs
  • vector<StatePair<string, int>> statePopulation: state name - population pairs

Complete main() to use an input ZIP code to retrieve the correct state abbreviation from the vector zipCodeState. Then use the state abbreviation to retrieve the state name from the vector abbrevState. Lastly, use the state name to retrieve the correct state name/population pair from the vector statePopulation and output the pair.

Ex: If the input is:

21044

the output is:

Maryland: 6079602
 

#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
    }


    for (i = 0; i < abbrevState.size(); ++i) {
        // TODO: Using state abbreviation, find state name
    }


    for (i = 0; i < statePopulation.size(); ++i) {
        // TODO: Using state name, find population. Print pair info.
    }

}

 
Current file: StatePair.h - Load default template... 1 #ifndef STATEPAIR 2 #define STATEPAIR 3 4 template<typename T1, typename T2> 5 class StatePair { 6 7 // TODO: Define constructors statePair(); StatePair(T1 userkey, T2 userValue); 8 9 10 11 // TODO: Define mutators, and accessors for StatePair 12 13 // TODO: Define PrintInfo() method 14 }; 15 16 #endif
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY