Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DeadlyZ
Created March 26, 2015 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeadlyZ/de5cc0b64318b12ac3af to your computer and use it in GitHub Desktop.
Save DeadlyZ/de5cc0b64318b12ac3af to your computer and use it in GitHub Desktop.
AreaCode Project
#ifndef AREACODE
#define AREACODE
#include <string>
using namespace std;
const int MAX = 400;
class AreaCodes{
public:
AreaCodes(string);
void lookUpCity(string areaCode, string & city);
void lookUpState(string areaCode, string & state);
void lookUpCityState(string areaCode, string & city, string & state);
void searchByCity(string city, string & yourCity);
void sortByCity();
private:
int size;
string dash;
string areaCodes[MAX];
string cities[MAX];
string states[MAX];
string sortedAreaCodes[MAX];
string sortedCities[MAX];
string sortedStates[MAX];
};
#endif
#include "areaCode.h"
#include <iostream>
#include <fstream>
using namespace std;
//Reads file into arrays
AreaCodes::AreaCodes(string filename){
int i = 0;
ifstream areaCodeFile;
areaCodeFile.open(filename);
if (!areaCodeFile){
cout << "Error! File not Found" << endl;
exit(1);
}
while (!areaCodeFile.eof()){
areaCodeFile >> areaCodes[i];
areaCodeFile >> dash >> ws;
getline(areaCodeFile, cities[i], ',');
getline(areaCodeFile, states[i]);
i++;
}areaCodeFile.close();
size = i;
}
//Looks up city assoiciated with areacode
void AreaCodes::lookUpCity(string areaCode, string & city){
int index = -1;
for (int i = 0; i < size; i++){
if (areaCode == areaCodes[i]){
index = 1;
city = cities[i];
}
}
if (index == -1){
cout << "Error!" << endl;
exit(1);
}
}
//looks up state associated with areacode
void AreaCodes::lookUpState(string areaCode, string & state){
int index = -1;
for (int i = 0; i < size; i++){
if (areaCode == areaCodes[i]){
index = 1;
state = states[i];
}
}
if (index == -1){
cout << "Error!" << endl;
exit(1);
}
}
//looks up both city and state assoiciated with areacode
void AreaCodes::lookUpCityState(string areaCode, string & city, string & state){
int index = -1;
for (int i = 0; i < size; i++){
if (areaCode == areaCodes[i]){
index = 1;
city = cities[i];
state = states[i];
}
}
if (index == -1){
cout << "Error!" << endl;
exit(1);
}
}
void AreaCodes::sortByCity(){
for (int i = 0; i < size; i++){
sortedAreaCodes[i] = areaCodes[i];
sortedCities[i] = cities[i];
sortedStates[i] = states[i];
}
int index;
int smallestIndex;
int location;
string temp;
for (index = 0; index < size - 1; index++){
smallestIndex = index;
for (location = index + 1; location < size; location++){
if (sortedCities[location] < sortedCities[smallestIndex])
smallestIndex = location;
temp = sortedAreaCodes[smallestIndex];
sortedAreaCodes[smallestIndex] = sortedAreaCodes[index];
sortedAreaCodes[index] = temp;
temp = sortedCities[smallestIndex];
sortedCities[smallestIndex] = sortedCities[index];
sortedCities[index] = temp;
temp = sortedStates[smallestIndex];
sortedStates[smallestIndex] = sortedStates[index];
sortedStates[index] = temp;
}
}
}
void AreaCodes::searchByCity(string city, string & yourCity){
for (int i = 0; i < size; i++){
if (sortedCities[i] == city){
yourCity = sortedAreaCodes[i];
break;
}
}
}
//Pick a choice, enter your areacode, and see either your city, state, or both.
#include "areaCode.h"
#include <iostream>
using namespace std;
void main(){
//Declare Variables
string areaCode, city, state, myCity, yourCity;
char choice;
//Reads in file
AreaCodes AreaCodes("areaCodes.txt");
AreaCodes.sortByCity();
cout << "Choose an option from the menu. 1, 2, or 3" << endl;
cout << "1. Look up City" << endl;
cout << "2. Look up State" << endl;
cout << "3. Look up both City and State" << endl;
cout << "4. Look up AreaCode" << endl;
cout << "Choice: ";
cin >> choice;
//Different Statements for choice picked
switch (choice){
case '1':
cout << "Enter Areacode: ";
cin >> areaCode;
AreaCodes.lookUpCity(areaCode, city);
cout << "Your city is: " << city << endl;
break;
case '2':
cout << "Enter Areacode: ";
cin >> areaCode;
AreaCodes.lookUpState(areaCode, state);
cout << "Your state is: " << state << endl;
break;
case '3':
cout << "Enter Areacode: ";
cin >> areaCode;
AreaCodes.lookUpCityState(areaCode, city, state);
cout << "Your city is: " << city << endl;
cout << "Your state is: " << state << endl;
break;
case '4':
cout << "Enter City: ";
cin.ignore();
getline(cin, myCity);
AreaCodes.searchByCity(myCity, yourCity);
cout << "Your areacode is: " << yourCity << endl;
break;
default:
cout << "Invalid AreaCode" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment