Question

15.4 Zip code and population (class templates) Complete the TODOs in StatePair.h and main.cpp. StatePair.h Define...

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.

  • 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

main.cpp

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.
  • Then, use the state name to retrieve the correct state name/population pair from the vector statePopulation and,
  • Finally, output the pair.

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

}

Homework Answers

Answer #1

// 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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream>...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {         ifstream infile("worldpop.txt");         vector<pair<string, int>> population_directory;         string line;         while(getline(infile, line)){                 if(line.size()>0){                         stringstream ss(line);                         string country;                         int population;                         ss>>country;                         ss>>population;                         population_directory.push_back(make_pair(country, population));                 }         }         cout<<"Task 1"<<endl;         cout<<"Names of countries with population>=1000,000,000"<<endl;         for(int i=0;i<population_directory.size();i++){                 if(population_directory[i].second>=1000000000){                        ...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
C++ program that Create a struct called car that has the following data members (variables): -...
C++ program that Create a struct called car that has the following data members (variables): - Color //color of the car - Model //model name of the car - Year //year the car was made - isElectric //whether the car is electric (true) or not (false) - topSpeed //top speed of the car, can be a decimal. code i have done struct not working properly. #include <iostream> using namespace std; struct Car { string color; string model_number; int year_model; bool...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
C++ Question I have a text file that contains data from a CD (ex 1. Adagio...
C++ Question I have a text file that contains data from a CD (ex 1. Adagio “MoonLight” Sonata - Ludwig Van Beethoven /n 2. An Alexis - F.H. Hummel and J.N. Hummel) How do I sort the data by author since that information is in the middle of the string? Here's what I have that sorts the string from the beginning: if (file.is_open())    {        while (getline(file, line))        {            lines.push_back(line);        }...