Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <iomanip>
- #include <algorithm>
- #include <stdio.h>
- #include <ctype.h>
- #include "constants.h"
- using namespace std;
- //----------------------------------------------------------------------------------------------------------------------
- // Function List
- //----------------------------------------------------------------------------------------------------------------------
- void mainMenu();
- int menuValidate(int menuLimit, int numSelect);
- void addEntry();
- void printList();
- void searchList();
- void sortList();
- void randomOrder();
- void saveFile();
- void readFile();
- string toLower(string s);
- //----------------------------------------------------------------------------------------------------------------------
- // Values
- //----------------------------------------------------------------------------------------------------------------------
- vector<list> nameList;
- //----------------------------------------------------------------------------------------------------------------------
- // Main program
- //----------------------------------------------------------------------------------------------------------------------
- int main() {
- mainMenu();
- return 0;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // mainMenu
- //----------------------------------------------------------------------------------------------------------------------
- void mainMenu() {
- string menuSelect;
- int numSelect;
- cout << "==== Main Menu ====" << endl;
- cout << endl;
- cout << "1. Add Entry" << endl;
- cout << "2. Print List" << endl;
- cout << "3. Search" << endl;
- cout << "4. Sort" << endl;
- cout << "5. Randomize Order" << endl;
- cout << "6. Save to File" << endl;
- cout << "7. Read from File" << endl;
- cout << "8. Exit" << endl;
- cout << endl;
- cout << "Make Selection [1-8]: ";
- cin >> menuSelect;
- istringstream iss(menuSelect);
- iss >> numSelect;
- numSelect = menuValidate(8, numSelect);
- switch(numSelect) {
- case 1 : addEntry();
- break;
- case 2 : printList();
- break;
- case 3 : searchList();
- break;
- case 4 : sortList();
- break;
- case 5 : randomOrder();
- break;
- case 6 : saveFile();
- break;
- case 7 : readFile();
- break;
- case 8 : return;
- }
- }
- //----------------------------------------------------------------------------------------------------------------------
- // menuValidate
- //----------------------------------------------------------------------------------------------------------------------
- int menuValidate(int menuLimit, int numSelect) {
- string menuSelect;
- bool cinFail = true;
- while(cinFail == true) {
- if (cin.fail() || numSelect < 1 || numSelect > menuLimit) {
- numSelect = 0;
- cout << "Invalid Choice!" << endl;
- cout << "Make Selection [1-8]: ";
- cin >> menuSelect;
- istringstream iss(menuSelect);
- iss >> numSelect;
- } else {
- cinFail = false;
- }
- }
- return numSelect;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // addEntry
- //----------------------------------------------------------------------------------------------------------------------
- void addEntry() {
- string s1;
- string s2;
- string sign = "xxxxxx";
- double d1;
- //New Entry
- int size = nameList.size();
- nameList.push_back(list());
- cout << "Name: ";
- cin >> s1;
- nameList[size].first = s1;
- cout << "Last: ";
- cin >> s2;
- nameList[size].last = s2;
- cout << "Height: ";
- cin >> d1;
- nameList[size].height = d1;
- //Signature
- for(int i = 0; i < 3; i++) {
- if((i + 1) > s1.length() || s1[i] == ' ') {
- sign[i] = 'x';
- }
- else {
- sign[i] = s1[i];
- }
- }
- for(int i = 0; i < 3; i++) {
- if((i + 1) > s2.length() || s1[i] == ' ') {
- sign[i + 3] = 'x';
- }
- else {
- sign[i + 3] = s2[i];
- }
- }
- sign = toLower(sign);
- nameList[size].sign = sign;
- cout << endl;
- mainMenu();
- return;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // printList
- //----------------------------------------------------------------------------------------------------------------------
- void printList() {
- cout << setw(4) << left << "Nr";
- cout << setw(8) << left << "Sign";
- cout << setw(32) << left << "Name";
- cout << setw(16) << left << "Height [m]";
- cout << endl;
- for(int i = 0; i < nameList.size(); i++){
- cout << setw(4) << left << (i + 1);
- cout << setw(8) << left << nameList[i].sign;
- cout << setw(32) << left << nameList[i].first + " " + nameList[i].last;
- cout << setw(16) << left << nameList[i].height << endl;
- }
- cout << endl;
- mainMenu();
- return;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // searchList
- //----------------------------------------------------------------------------------------------------------------------
- void searchList() {
- string target;
- bool found = false;
- int pos;
- char choice;
- cout << "Enter a signature: " << endl;
- cin >> target;
- for(int i = 0; i < nameList.size(); i++) {
- if(target == nameList[i].sign) {
- found = true;
- pos = i;
- cout << "Entry found!" << endl;
- cout << " " << endl;
- cout << "Nr: " << (i + 1) << " ";
- cout << "Sign: " << nameList[i].sign << " ";
- cout << "Name: " << nameList[i].first << " ";
- cout << nameList[i].last << " ";
- cout << "Height: " << nameList[i].height << " m" << endl;
- }
- }
- if(found == true) {
- cout << "Remove entry [y/n]: ";
- cin >> choice;
- if(choice == 'y') {
- nameList.erase(nameList.begin() + pos);
- cout << "Entry removed!" << endl;
- }
- }
- if(found == false) {
- cout << "Nope!" << endl;
- }
- cout << endl;
- mainMenu();
- return;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // sortList
- //----------------------------------------------------------------------------------------------------------------------
- void sortList() {
- cout << "fml" << endl;
- mainMenu();
- return;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // randomOrder
- //----------------------------------------------------------------------------------------------------------------------
- void randomOrder() {
- random_shuffle(nameList.begin(), nameList.end());
- cout << "List has been shuffled!" << endl;
- cout << endl;
- mainMenu();
- return;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // saveFile
- //----------------------------------------------------------------------------------------------------------------------
- void saveFile() {
- string fileName;
- cout << "Enter filename: ";
- cin >> fileName;
- string outputFile = fileName + ".txt";
- ofstream outFile(outputFile);
- for(int i = 0; i < nameList.size(); i++) {
- outFile << nameList[i].first << delim;
- outFile << nameList[i].last << delim;
- outFile << nameList[i].sign << delim;
- outFile << nameList[i].height << endl;
- }
- cout << endl;
- mainMenu();
- return;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // toLower
- //----------------------------------------------------------------------------------------------------------------------
- string toLower(string s) {
- char letter;
- string result;
- for(int i = 0; i < s.size(); i++) {
- letter = s[i];
- tolower(letter);
- result[i] = letter;
- cout << letter << endl;
- }
- return result;
- }
- //----------------------------------------------------------------------------------------------------------------------
- // readFile
- //----------------------------------------------------------------------------------------------------------------------
- void readFile() {
- int i = 0;
- int pos = 0;
- int pos1 = 0, pos2 = 0;
- int length;
- int nextPos;
- double height;
- string fileName;
- string line;
- cout << "Enter filename: ";
- cin >> fileName;
- string inputFile = fileName + ".txt";
- ifstream inFile(inputFile);
- while(getline(inFile, line)) {
- nameList.push_back(list());
- pos2 = line.find(delim, pos1);
- length = (pos2 - pos1);
- nameList[i].first = line.substr(pos1, length);
- pos1 = (pos2 + 1);
- pos2 = line.find(delim, pos1);
- length = (pos2 - pos1);
- nameList[i].last = line.substr(pos1, length);
- pos1 = (pos2 + 1);
- pos2 = line.find(delim, pos1 + 1);
- length = (pos2 - pos1);
- nameList[i].sign = line.substr(pos1, length);
- pos1 = (pos2 + 1);
- pos2 = line.find(delim, pos1 +1);
- length = (pos2 - pos1);
- nameList[i].height = stod(line.substr(pos1, length));
- pos1 = 0;
- pos2 = 0;
- i++;
- }
- inFile.close();
- cout << endl;
- mainMenu();
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement