1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
2415, Target Corporation, 3400 Green Mt Crossing Dr, Shiloh, IL, 62269, 5.7
1705, Starbucks, 1411 W Hwy 50, O'Fallon, IL, 62269, 6.4
3218, ALDI, 1708 N Illinois St, Swansea, IL, 62226, 0.9
4062, Walmart Supercenter, 2608 Green Mt Commons Dr, Belleville, IL, 62221, 4.0
2011, Spectrum Store, 3950 Green Mt Crossing Dr, Shiloh, IL, 62269, 5.4
912, Marco's Pizza, 1838 Central Plaza Dr, Belleville, IL, 62221, 1.8
[code]
[code]
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
const string FILE_NAME = "text.txt";
struct Business
{
string id;
string name;
string address;
string city;
string state;
string zip;
double distance;
string get_id()
{
string tmp_id = id;
return tmp_id.replace(0, 1, "**");
}
void print()
{
cout << get_id() << ", " << name << ", " << address << ", " << state << ", " << zip << ", " << (distance * 1.6) << endl;
}
};
void DisplayAllBusinessInfo(Business* businesses, int count)
{
for (int i = 0; i < count; ++i)
{
businesses[i].print();
}
}
void SearchByCity(Business* businesses, int count, const string& c_name)
{
for (int i = 0; i < count; ++i)
{
if (businesses[i].city.find(c_name) != string::npos)
{
businesses[i].print();
}
}
}
void SortByDistanceAndSaveToFile(Business* businesses, int count)
{
for (int i = 0; i < count - 1; ++i)
{
for (int j = i + 1; j < count; ++j)
{
if (businesses[j].distance < businesses[i].distance)
auto tmp = businesses[i];
}
}
// Writing Sorted data to the original file
ofstream outFile(FILE_NAME);
for (int i = 0; i < count; ++i)
{
outFile << businesses[i].id << " " << businesses[i].name << " " << businesses[i].address << " " << businesses[i].city << " " << businesses[i].state << " " << businesses[i].zip << " " << businesses[i].distance;
if (i < count - 1)
outFile << endl;
}
outFile.close();
}
int main()
{
int size;
int count = 0;
ifstream fin(FILE_NAME);
fin >> size;
if (!fin)
{
cout << "Could not open file.\n";
return 1;
}
fin.ignore(1000, '\n'); // ignore everything until end of line
Business* businesses = new Business[size]; // Dynamic allocation of array
// while array is not filled AND the file status is good
// do the following:
while (count < size && fin)
{
getline(fin, businesses[count].id);
getline(fin, businesses[count].name);
getline(fin, businesses[count].address);
getline(fin, businesses[count].city);
getline(fin, businesses[count].state);
getline(fin, businesses[count].zip);
fin >> businesses[count].distance;
fin.ignore(); // ignore a single character
fin.ignore(",");
if (fin) // add 1 to count if the file access was successful
count++;
}
fin.close();
// Display menu
while (1)
{
cout << "1. Display all business' info" << endl;
cout << "2. Search by city" << endl;
cout << "3. Sort by distance" << endl;
cout << "4. Exit" << endl;
cout << endl << "Please pick a number: ";
int choice;
cin >> choice;
if (choice == 4) exit(0);
string c_name;
switch (choice)
{
case 1:
DisplayAllBusinessInfo(businesses, count);
break;
case 2:
cout << "Enter city name: ";
cin >> c_name;
SearchByCity(businesses, count, c_name);
break;
case 3:
SortByDistanceAndSaveToFile(businesses, count);
break;
default:
cout << "Invalid selction, try again...";
break;
}
cout << endl;
}
return 0;
}
|