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
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int n;
int total = 0;
void addWorld();
void viewWorld();
void funct();
void deleteWorld();
struct world_
{
char continent[15];
char country[15];
int avg_cit;
}world;
int main()
{
int option = 0;
enum menuitems{ view = 1, add = 2, func = 3, del = 4, exit = 5 };
while (option != exit)
{
cout << endl;
cout << setw(50) << " Chose an option " << endl << endl;
cout << setw(25) << view << " View entry's" << endl;
cout << setw(25) << add << " Add new entry's" << endl;
cout << setw(25) << func << " Calculate avarage citizen count in continent" << endl;
cout << setw(25) << del << " Delete current data in file " << endl;
cout << setw(25) << exit << " Exit!" << endl;
cin >> option;
switch (option)
{
case view: viewWorld();
break;
case add: addWorld();
break;
case func: funct();
break;
case del: deleteWorld();
}
}
cout << endl << endl;
return 0;
}
void addWorld()
{
ofstream outfile("world.txt", ios::out | ios::app);
if (!outfile)
{
cerr << " Error creating file! " << endl;
}
else
{
cerr << " File opened successfully! " << endl;
cout << " How many entry's you would like to add? ";
cin >> n;
for (int i = 0; i < n; i++)
{
cin.ignore();
cout << " Enter continent: ";
cin.getline(world.continent, 15);
cout << " Enter country: ";
cin.getline(world.country, 15);
cout << " Enter citizen count: ";
cin >> world.avg_cit;
outfile << world.continent << endl;
outfile << world.country << endl;
outfile << world.avg_cit << endl;
outfile << string(22, '-') << endl;
outfile.flush();
total = total + world.avg_cit;
}
cerr << " Data successfully writen to file " << endl;
outfile.close();
cerr << " File closed! " << endl;
}
}
void viewWorld()
{
ifstream infile("world.txt", ios::in);
// char buf[30];
if (!infile)
{
cerr << " Error opening file! " << endl;
}
else
{
while (!infile.eof())
{
infile >> world.continent;
cout << " Continent: " << world.continent << endl;
infile >> world.country;
cout << " Country: " << world.country << endl;
infile >> world.avg_cit;
cout << " Citizen count: " << world.avg_cit << endl;
break;
}
}
}
void funct()
{
ifstream infile("world.txt", ios::in);
if (!infile)
{
cerr << " Error opening file! ";
}
else
{
char search[15];
cout << " Search for continent: ";
cin.getline(search, 15);
cin.ignore();
int average = 0;
int counter = 0;
for (int i = 0; i < n; i++)
{
counter++;
if (strcmp(world.continent, search) == 0)
average = counter / total;
}
cout << " Avarage citizen count in " << search << " = " << average;
}
}
void deleteWorld()
{
ofstream outfile("world.txt", ios_base::trunc);
outfile.close();
cout << " Content sucessfully deleted ";
}
|