Question

Define a class StatePair with two template types (T1 and T2), a constructor, mutators, accessors

 13.5 LAB: Zip code and population (class templates)

 Define a class StatePair with two template types (T1 and T2), a constructor, mutators, accessors, and a Printlnfo0 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 mainO 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

----------------main.cpp

#include
#include
#include
#include
#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> zipCodeState;

   // state abbrev. - state name pairs
   vector<StatePair> abbrevState;

   // state name - population pairs
   vector<StatePair> 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()) {
       StatePairtemp;
       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()) {
       StatePairtemp;
       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()) {
       StatePairtemp;
       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.
   }

}

----------------------StatePair.h

#ifndef STATEPAIR
#define STATEPAIR

template
class StatePair {

// TODO: Define a constructor, mutators, and accessors
// for StatePair
  
// TODO: Define PrintInfo() method
};

#endif


0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code

main.cpp

#include
#include
#include
#include
#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> zipCodeState;

// state abbrev. - state name pairs
vector> abbrevState;

// state name - population pairs
vector> 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 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 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 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;

string abbre;
string stateName;
for (i = 0; i < zipCodeState.size(); ++i) {
// TODO: Using ZIP code, find state abbreviation
if(zipCodeState[i].GetKey()==zip)
{
    //save abbreviation
    abbre=zipCodeState[i].GetValue();
   }
}


for (i = 0; i < abbrevState.size(); ++i) {
// TODO: Using state abbreviation, find state name
if(abbrevState[i].GetKey()==abbre)
{
    //save the state
        state = abbrevState[i].GetValue();
   }
}


for (i = 0; i < statePopulation.size(); ++i) {
// TODO: Using state name, find population. Print pair info.
if(statePopulation[i].GetKey()==state)
{
    //print the pair
    statePopulation[i].PrintInfo();
   }
}

}

StatePair.h

#ifndef STATEPAIR
#define STATEPAIR
#include
using namespace std;
template
class StatePair {
   T1 type1;
   T2 type2;
public:
   //constructor
   StatePair()
   {
      
   }
   StatePair(T1 type1,T2 type2)
   {
       this->type1=type1;
       this->type2=type2;
   }
   //access and mutator
   T1 GetKey()
   {
       type1;
   }
   T2 GetValue()
   {
       type2;
   }
   void SetKey(T1 x)
   {
       type1=x;
   }
   void SetValue(T2 x)
   {
       type2=x;
   }
   void PrintInfo()
   {
       cout<    }
  
// TODO: Define a constructor, mutators, and accessors
// for StatePair
  
// TODO: Define PrintInfo() method
};

#endif

Add a comment
Know the answer?
Add Answer to:
Define a class StatePair with two template types (T1 and T2), a constructor, mutators, accessors
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB: Zip code and population (generic types)

    13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

  • I wrote code in C++ that takes a text file containing information on different football players...

    I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players. MAIN.CPP: #include "Combine.h" #include using namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello,...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode ha...

    Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode has its unique function member GetEmail() which returns the string variable "studentEmail". TtuStudentNode has its overriding "PrintContactNode()" which has the following definition. void TtuStudentNode::PrintContactNode() { cout << "Name: " << this->contactName << endl; cout << "Phone number: " << this->contactPhoneNum << endl; cout << "Email : " << this->studentEmail << endl; return; } ContactNode.h needs to have the function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT