Refer to the picture for directions) (My code produces no output) My code: Main.cpp #include #include #include #include #include "StatePair.h" using namespace std; int main() { ifstream inFS; int zip; int population; string abbrev; string state; unsigned int i; vector> zipCodeState; vector> abbrevState; vector> statePopulation; inFS.open("zip_code_state.txt"); if (!inFS.is_open()) { cout << "Could not open file zip_code_state.txt." << endl; return 1; } 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(); inFS.open("abbreviation_state.txt"); if (!inFS.is_open()) { cout << "Could not open file abbreviation_state.txt." << endl; return 1; } while (!inFS.eof()) { StatePair temp; inFS >> abbrev; if (!inFS.fail()) { temp.SetKey(abbrev); } getline(inFS, state); getline(inFS, state)
(Refer to the picture for directions)
(My code produces no output)
My code:
Main.cpp
#include<iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;
int main() {
ifstream inFS;
int zip;
int population;
string abbrev;
string state;
unsigned int i;
vector<StatePair <int, string>> zipCodeState;
vector<StatePair<string, string>> abbrevState;
vector<StatePair<string, int>> statePopulation;
inFS.open("zip_code_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file zip_code_state.txt." << endl;
return 1;
}
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();
inFS.open("abbreviation_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file abbreviation_state.txt." << endl;
return 1;
}
while (!inFS.eof()) {
StatePair <string, string> temp;
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetKey(abbrev);
}
getline(inFS, state);
getline(inFS, state);
state = state.substr(0, state.size()-1);
if (!inFS.fail()) {
temp.SetValue(state);
}
abbrevState.push_back(temp);
}
inFS.close();
inFS.open("state_population.txt");
if (!inFS.is_open()) {
cout << "Could not open file state_population.txt." << endl;
return 1;
}
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);
statePopulation.push_back(temp);
}
inFS.close();
cin >> zip;
for (i = 0; i < zipCodeState.size(); ++i) {
}
for (i = 0; i < abbrevState.size(); ++i) {
}
for (i = 0; i < statePopulation.size(); ++i) {
}
}
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 a constructor, mutators, and accessors
// for StatePair
StatePair() //default constructor
{}
// set the key
void SetKey(T1 key)
{
this->key = key;
}
// set the value
void SetValue(T2 value)
{
this->value = value;
}
// return key
T1 GetKey() { return key;}
// return value
T2 GetValue() { return value;}
// Define PrintInfo() method
// display key and value in the format key : value
void PrintInfo()
{
cout<<key<<" : "<<value<<endl;
}
};
#endif
Step by step
Solved in 4 steps with 1 images