Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Karl Ramberg
- // 2-17-19
- // CS181 - Assignment 1 - Q4
- #include<iostream>
- #include<fstream>
- using namespace std;
- struct Division
- {
- string name;
- int id;
- double sales;
- int year;
- };
- void openFile(string &filename, fstream &file);
- void readFile(string filename, fstream &file);
- void writeFile(string filename, fstream &file);
- void modifyFile(string filename, fstream &file);
- void getInfo(Division &div);
- int main()
- {
- fstream file;
- string filename;
- openFile(filename, file);
- int choice;
- cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
- cin >> choice;
- while(choice != 1 && choice != 2 && choice != 3 && choice != 4)
- {
- cout << "Not a valid operation" << endl;
- cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
- cin >> choice;
- }
- while(choice != 4)
- {
- if(choice == 1)
- {
- readFile(filename, file);
- }
- else if(choice == 2)
- {
- writeFile(filename, file);
- }
- else
- {
- modifyFile(filename, file);
- }
- cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
- cin >> choice;
- while(choice != 1 && choice != 2 && choice != 3 && choice != 4)
- {
- cout << "Not a valid operation" << endl;
- cout << "(1) Read, (2) Write, (3) Modify, or (4) Quit? ";
- cin >> choice;
- }
- }
- file.close();
- }
- void openFile(string &filename, fstream &file)
- {
- cout << "Choose a file: ";
- cin >> filename;
- file.open(filename, ios::in | ios::out | ios::binary);
- while(file.fail())
- {
- cout << "Cannot read that file" << endl;
- cout << "Choose a file: ";
- cin >> filename;
- file.open(filename, ios::in | ios::out | ios::binary);
- }
- }
- void readFile(string filename, fstream &file)
- {
- cout << "Reading " << filename << "..." << endl;
- file.seekg(0);
- file.clear();
- Division div;
- while(!file.eof())
- {
- file.read(reinterpret_cast<char *>(&div), sizeof(div));
- cout << endl;
- cout << "Name: " << div.name << endl;
- cout << "ID: " << div.id << endl;
- cout << "Sales: $" << div.sales << endl;
- cout << "Year: " << div.year << endl;
- cout << endl;
- }
- }
- void writeFile(string filename, fstream &file)
- {
- cout << "Writing to " << filename << "..." << endl;
- Division div;
- getInfo(div);
- file.seekp(ios::end);
- file.write(reinterpret_cast<char *>(&div), sizeof(div));
- cout << endl;
- }
- void modifyFile(string filename, fstream &file)
- {
- int divNum;
- cout << "Which record would you like to modify? ";
- cin >> divNum;
- Division div;
- getInfo(div);
- file.seekp(divNum * sizeof div);
- file.write(reinterpret_cast<char *>(&div), sizeof div);
- }
- void getInfo(Division &div)
- {
- string dummy;
- getline(cin, dummy);
- string name;
- cout << "Enter a name: ";
- getline(cin, name);
- while(name == "")
- {
- cout << "Invalid name" << endl;
- cout << "Enter a name: ";
- getline(cin, name);
- }
- int id;
- cout << "Enter an ID: ";
- cin >> id;
- while(id < 0)
- {
- cout << "ID cannot be negative" << endl;
- cout << "Enter an ID: ";
- cin >> id;
- }
- double sales;
- cout << "Enter total sales: ";
- cin >> sales;
- while(sales < 0.0)
- {
- cout << "Sales cannot be negative" << endl;
- cout << "Enter total sales: ";
- cin >> sales;
- }
- int year;
- cout << "Enter a year: ";
- cin >> year;
- while(year < 0)
- {
- cout << "Year cannot be negative" << endl;
- cout << "Enter a year: ";
- cin >> year;
- }
- div.name = name;
- div.id = id;
- div.sales = sales;
- div.year = year;
- cout << div.name << " " << div.id << " " << div.sales << " " << div.year << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement