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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype> // for toupper function
using namespace std;
struct Country {
char name[16]; // country name
long pop; // population
double gdp; // gross domestic product
};
const char * file = "countries.dat";
// read and display contents of countries.txt;
// return number of records in file
int prntCtries(fstream & fil);
// update countries.txt; size is number of records
void updtCtries(fstream & fil, int size);
// add data to countries.txt
void addCtries(fstream & fil);
int menu(); // pulls up menu
int main()
{
fstream fin; // input file object
fstream fout; // output file object
int cnt = 0; // number of records in file
fout.open(file, ios::out | ios::binary);
fout.close();
int choice;
do{
choice = menu(); //choice for menu
switch(choice)
{
case 1:
cnt = prntCtries(fin);
break;
case 2:
cout << "This is the function I don't have! LOL " << endl;
break;
case 3:
addCtries(fin);
break;
case 4:
updtCtries(fin, cnt); // update file contents
break;
case 5:
cout << "Quitting program..." << endl << endl;
break;
default:
cout << "This is not a valid menu option!" << endl;
break;
}
}while(choice != 5);
if (cnt == -1) // problem encountered
exit(2);
cout << endl;
cnt = prntCtries(fin); // show file contents
if (cnt == -1) // problem encountered
exit(2);
fin.close(); // close file
return 0;
}//endmain
int prntCtries(fstream & fil) // end openFiles()
{
Country c;
int ndx = 0;
cout << fixed << showpoint << setprecision(1);
fil.open(file, ios::in | ios::binary);
if (fil.is_open()) {
fil.seekg(0); // position at beginning of file
cout << "Current contents of \"" << file << "\" file:\n";
cout << " Country \t Population (M) GDP ($M)\n";
while (fil.read(reinterpret_cast<char *>(&c), sizeof(c))) {
cout << ++ndx << ". " << setw(15) << c.name << setw(16) << c.pop << setw(16) << c.gdp << endl;
} // endwhile
if (fil.eof())
fil.clear(); // clear EOF flag
else { // possible error condition
cout << " *** Error reading " << file << endl;
return -1;
} // endif
} // endif-thenPart
else {
cout << " *** Open \"" << file << "\" for input failed ***\n";
return -1;
} // endif-elsePart
fil.close(); // close file
return ndx;
} // end prntCtries()
void updtCtries(fstream & fil, int size)
{
Country c;
int ndx = 0; // record number to change
streampos pos; // location of record to update
cout << fixed << showpoint << setprecision(1); // output formatting
fil.open(file, ios::out | ios::in | ios::binary);
cout << " Enter the record # to change: ";
cin >> ndx;
cin.get(); // get and ignore endline
--ndx; // we start indexes at zero
if (ndx < 0 || ndx >= size) {
cout << " *** Invalid record # - Adios!\n";
exit(3);
} // endif
pos = ndx * sizeof(Country);
fil.seekg(pos);
if (fil.fail()) {
cout << " *** seek failed ***\n";
return;
} // endif
fil.read(reinterpret_cast<char *>(&c), sizeof(c));
cout << "Country selected:\n";
cout << ndx+1 << ". " << setw(15) << c.name << setw(16) << c.pop << setw(16) << c.gdp << endl;
if (fil.eof())
fil.clear(); // clear EOF flag
cout << "Enter country name: ";
cin.get(c.name, 16);
//cout << " Country: " << c.name << endl;
cin.get(); // get and ignore endline
cout << " Enter population of " << c.name << ": ";
cin >> c.pop;
//cout << " Population: " << c.pop << endl;
cout << " Enter GDP of " << c.name << "(in USD): $";
cin >> c.gdp;
fil.seekg(pos); // set pointer to start of record
fil.write(reinterpret_cast<char *>(&c), sizeof(c));
if (fil.fail()) {
cout << " *** attempted write failed ***\n";
return;
} // endif
fil << flush; // force disk write
fil.close(); // close file
int k = prntCtries(fil); // print updated file
} // end updtCtries()
void addCtries(fstream & fil) // end openFiles()
{
Country c;
cout << fixed << showpoint << setprecision(1);
fil.open(file, ios::out | ios::app | ios::binary);
if (fil.is_open()) {
cout << "\n ... adding to \"" << file << "\" ...\n";
cout << "Enter country name (or or 'X' to quit): ";
cin.get(c.name, 16);
while (toupper(c.name[0]) != 'X') {
cout << " Country: " << c.name << endl;
cin.get(); // get and ignore endline
cout << " Enter population of " << c.name << ": ";
cin >> c.pop;
cout << " Population: " << c.pop << endl;
cout << " Enter GDP of " << c.name << "(in USD): $";
cin >> c.gdp;
fil.write(reinterpret_cast<char *>(&c), sizeof(c));
cin.get(); // get and ignore endline
cout << "\nEnter country name (or 'X' to quit) : ";
cin.get(c.name, 20);
} // endwhile
} // endif-thenPart
else {
cout << " *** Open \"" << file << "\" for input failed ***\n";
return;
} // endif-elsePart
fil.close(); // close file
} // end addCtries()
int menu()
{
int sel = 5; // menu selection
cout << "\n * * * M A I N M E N U * * *\n\n";
cout << "1. Print All Records " << endl;
cout << "2. Print One Specific Countries Info" << endl;
cout << "3. Add a Record" << endl;
cout << "4. Update a Record " << endl;
cout << "5. Quit " << endl;
cout << "\tEnter your choice (1..5): ";
cin >> sel;
while (sel < 1 || sel > 5) {
cout << "\tPlease enter 1 to 5: ";
cin >> sel;
} // endwhile
return sel;
}
|