Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <fstream>
- using namespace std;
- struct kitten
- {
- int score;
- string name;
- string color;
- };
- struct roster
- {
- kitten kittens[10];
- int size = 0;
- };
- string printMenu()
- {
- int loop = 1;
- string choice;
- string valid[8] = {"a", "d", "u", "f", "l", "s", "o", "q"};
- while (loop == 1)
- {
- cout << "MENU" << endl;
- cout << "a - Add kitten" << endl;
- cout << "d - Delete kitten" << endl;
- cout << "u - Update kitten color and cuteness score" << endl;
- cout << "f - Find kitten" << endl;
- cout << "l - Load kitten info from file" << endl;
- cout << "s - Save kitten info to file" << endl;
- cout << "o - Output roster" << endl;
- cout << "q - Quit" << endl;
- cout << endl;
- cout << "Choose an option:" << endl;
- cin >> choice;
- int validity = 0;
- for (int i = 0; i < 8; i++)
- {
- if (choice == valid[i])
- {
- validity += 1;
- }
- }
- if (validity >= 1)
- {
- if (choice == "q")
- {
- break;
- }
- else
- {
- return choice;
- }
- }
- else
- {
- cout << "Invalid option. Please try again." << endl;
- }
- }
- }
- int findKitten(string name, roster& q)
- {
- int nameinroster;
- int position = 0;
- for (int i = 0; i < 10; i++)
- {
- if (name == q.kittens[i].name)
- {
- return i;
- }
- }
- return -1;
- }
- void addKitten(kitten x, roster& q)
- {
- if (q.size == 10)
- {
- cout << "Impossible to add new kitten: roster is full." << endl;
- }
- else
- {
- q.kittens[q.size] = x;
- cout << "Successfully added new kitten to roster." << endl;
- q.size += 1;
- }
- }
- bool deleteKitten(string name, roster& q)
- {
- int exists = findKitten(name, q);
- if (exists == -1)
- {
- return false;
- }
- else
- {
- for (int i = exists; i < q.size; i++)
- {
- q.kittens[i] = q.kittens[i+1];
- }
- q.size -= 1;
- return true;
- }
- }
- void getKittenFromFile(string filename, roster& q)
- {
- string temp = "";
- ifstream file;
- file.open(filename);
- if (file.is_open())
- {
- while(true)
- {
- if (q.size >= 10)
- {
- cout << "Impossible to add new kitten: roster is full." << endl;
- cout << endl;
- return;
- }
- if (file.eof()) break;
- getline(file, q.kittens[q.size].name);
- if (file.eof()) break;
- getline(file, q.kittens[q.size].color);
- if (file.eof()) break;
- getline(file, temp);
- q.kittens[q.size].score = stoi(temp);
- q.size += 1;
- }
- }
- else
- {
- cout << "Error! File not found." << endl;
- }
- }
- bool updateKitten(kitten x, roster& q)
- {
- int exists = findKitten(x.name, q);
- if (exists == -1)
- {
- return false;
- }
- else
- {
- q.kittens[exists].color = x.color;
- q.kittens[exists].score = x.score;
- return true;
- }
- }
- void printToFile(string filename, roster q)
- {
- ofstream file;
- file.open(filename);
- file << "ROSTER:" << endl;
- for (int i = 0; i < q.size; i++)
- {
- int number = i+1;
- file << "Kitten " << number << " -- Name: " << q.kittens[i].name << ", Color: " << q.kittens[i].color << ", Score: " << q.kittens[i].score << endl;
- }
- }
- void printRoster(roster q)
- {
- cout << "ROSTER:" << endl;
- for (int i = 0; i < q.size; i++)
- {
- int number = i+1;
- cout << "Kitten " << number << " -- Name: " << q.kittens[i].name << ", Color: " << q.kittens[i].color << ", Score: " << q.kittens[i].score << endl;
- }
- cout << endl;
- }
- int main()
- {
- roster kittenRoster;
- kittenRoster.size = 0;
- int keep = 1;
- while (keep == 1)
- {
- string decision = printMenu();
- if (decision == "a")
- {
- kitten adding;
- cout << "Enter a new kitten's name:" << endl;
- cin >> adding.name;
- cout << "Enter the kittens's color:" << endl;
- cin >> adding.color;
- cout << "Enter the kittens's cuteness score:" << endl;
- cin >> adding.score;
- addKitten(adding, kittenRoster);
- cout << endl;
- }
- if (decision == "d")
- {
- if (kittenRoster.size == 0)
- {
- cout << "Cannot delete from empty roster." << endl;
- }
- else
- {
- kitten removing;
- cout << "Enter kitten name to delete:" << endl;
- cin >> removing.name;
- bool tried = deleteKitten(removing.name, kittenRoster);
- if (tried == false)
- {
- cout << "Error! Kitten not found." << endl;
- }
- else
- {
- kittenRoster.size -= 1;
- }
- }
- }
- if (decision == "u")
- {
- kitten update;
- cout << "Enter a kitten name:" << endl;
- cin >> update.name;
- cout << "Enter a new color for the kitten:" << endl;
- cin >> update.color;
- cout << "Enter a new cuteness score for the kitten:" << endl;
- cin >> update.score;
- bool attempt = updateKitten(update, kittenRoster);
- if (attempt == true)
- {
- cout << "Successfully updated kitten info." << endl;
- }
- if (attempt == false)
- {
- cout << "Cannot find kitten." << endl;
- }
- }
- if (decision == "f")
- {
- kitten find;
- cout << "Enter a kitten name: " << endl;
- cin >> find.name;
- int found = findKitten(find.name, kittenRoster);
- if (found == -1)
- {
- cout << "Kitten not found." << endl;
- }
- else
- {
- cout << find.name << " info: " << kittenRoster.kittens[found].color << ", " << kittenRoster.kittens[found].score << endl;
- }
- cout << endl;
- }
- if (decision == "l")
- {
- string namefile;
- cout << "Enter file name:" << endl;
- cin >> namefile;
- getKittenFromFile(namefile, kittenRoster);
- }
- if (decision == "s")
- {
- string namefile;
- cout << "Enter file name:" << endl;
- cin >> namefile;
- printToFile(namefile, kittenRoster);
- }
- if (decision == "o")
- {
- printRoster(kittenRoster);
- }
- if (decision == "q")
- {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement