SHARE
TWEET

intMain(UPDATED)

a guest Jul 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. #include <stdlib.h> //for exit() function
  4.  
  5. #include <fstream>  //for creating text file
  6.  
  7. #include "Car.h"   //child class
  8.  
  9. using namespace std;
  10.  
  11. //Global variables
  12. string manufacturer, type, carPlateNo, model;
  13. int seat;
  14. const int SIZE = 80; //SIZE is the number of objects that the c[] array can store
  15. Car c[SIZE]; //c[SIZE] is an array used to store all 80 car details
  16. string option; //vairable for y/n choices
  17. int choice; //choice is used for user selections(in menu selection, type selection)
  18. int total = 0; //total recorded cars(used to keep count of total cars)
  19. int main();
  20.  
  21. //Type options/selection(used in addRecord() & updateRecord())
  22. string funcType() {
  23.   string type;
  24.   cout << "Select the type of the car\n " <<
  25.     "1.Hatchback\n" <<
  26.     " 2.Sedan\n" <<
  27.     " 3.MPV\n" <<
  28.     " 4.SUV\n" <<
  29.     " 5.Crossover\n" <<
  30.     " 6.Coupe\n" <<
  31.     " 7.Convertible\n" <<
  32.     " 8.Others \n";
  33.  
  34.   cout << "Choice: ";
  35.   cin >> choice;
  36.   cin.clear();
  37.   cin.ignore(1, '\n');
  38.  
  39.   switch (choice) {
  40.  
  41.   case 1:
  42.     return "Hatchback";
  43.     break;
  44.  
  45.   case 2:
  46.     return "Sedan";
  47.     break;
  48.  
  49.   case 3:
  50.     return "MPV";
  51.     break;
  52.  
  53.   case 4:
  54.     return "SUV";
  55.     break;
  56.  
  57.   case 5:
  58.     return "Crossover";
  59.     break;
  60.  
  61.   case 6:
  62.     return "Coupe";
  63.     break;
  64.  
  65.   case 7:
  66.     return "Convertible";
  67.     break;
  68.  
  69.   case 8:
  70.     cout << "Enter Type: ";
  71.     getline(cin, type);
  72.     return type;
  73.     break;
  74.  
  75.   default:
  76.     //print error messege and allow user to re-enter choice
  77.     cout << choice << " is not valid choice. Please choose again" << endl;
  78.     return funcType();
  79.   }
  80. }
  81.  
  82. //Detects special characters and certain alphabets in car plate number(used in addRecord() & updateRecord())
  83. char checker(string carPlateNo) {
  84.   char cpnError;
  85.   string cpnc(carPlateNo);
  86.  
  87.   if (cpnc.find_first_not_of("abcdefghjklmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY0123456789 ") != string::npos) { //check until the end of the string
  88.     cout << "Error. There are only alphabets(except for I,O & Z) and numbers in a car plate number.\n";
  89.  
  90.     return cpnError = 'a';
  91.   } else {
  92.     return cpnError = 'b';
  93.   }
  94.  
  95. }
  96.  
  97. //Detects if there is anything other than y or n ((used in addRecord() & updateRecord())
  98. string checker2(string option) {
  99.   string ynError(option);
  100.  
  101.   if (ynError.find_first_not_of("ynYN") != string::npos) {
  102.     cout << "Invalid input. Only 'y' or 'n'." << endl;
  103.     return ynError = "a";
  104.   } else {
  105.     return ynError = "b";
  106.   }
  107.  
  108. }
  109.  
  110. //+1 total recorded cars(used in addRecord())
  111. void myPlus() {
  112.   total++;
  113. }
  114.  
  115. //-1 total recorded cars(used in deleteRecord())
  116. void myMinus() {
  117.   total--;
  118. }
  119.  
  120. //OPTION 1: Creating cars
  121. void addRecord() {
  122.   cout << "------------------=CREATE=------------------" << endl;
  123.   cout << "Note: Only a maximum of 80 cars can be created." << endl;
  124.   cout << "------------------Car #" << total + 1 << "-------------------" << endl;
  125.  
  126.   //Manufacturer
  127.   cout << "Please enter the manufacturer of the car: ";
  128.   getline(cin, manufacturer);
  129.   c[total].setManufacturer(manufacturer);
  130.  
  131.   //Type
  132.   c[total].setType(funcType());
  133.  
  134.   //SeatNumber
  135.   string ynError; //ynError is used to check for anything other than y or n
  136.   do {
  137.     cout << "Do you know what is the number of seats?(y/n)\n";
  138.     cin >> option;
  139.     ynError = checker2(option);
  140.   } while ((ynError == "a")); //loop if option has anything other then y or n
  141.  
  142.   if ((option == "y") || (option == "Y")) {
  143.     do {
  144.       cin.clear();
  145.       cin.ignore(1000, '\n');
  146.       cout << "Please enter number of seats: \n";
  147.       cin >> seat;
  148.  
  149.       if ((cin.fail()) || (seat <= 0)) {
  150.         cout << endl;
  151.         cout << "Invalid input. Please key in a positive integer." << endl; //print error message
  152.       }
  153.  
  154.     } while ((cin.fail()) || (seat <= 0)); //condition to loop and allow user to re-enter
  155.  
  156.     c[total].setSeat(seat);
  157.   } else {
  158.     c[total].setSeat(1); //seats number is set to 1 if it is unknown(default)
  159.   }
  160.  
  161.   //Car Plate Number
  162.   char cpnError; //cpnError is used to check for special characters and certain alphabets
  163.   cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  164.   cin.ignore(1, '\n');
  165.   getline(cin, carPlateNo);
  166.   cpnError = checker(carPlateNo);
  167.  
  168.   if (cpnError == 'a') { //if carPlateNo have any special characters or certain alphabets
  169.     do {
  170.       cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  171.       getline(cin, carPlateNo);
  172.       cpnError = checker(carPlateNo);
  173.     } while (cpnError == 'a'); //will loop and allow user to re-enter if special characters/certain alphabets was entered
  174.   }
  175.   c[total].setCarPlateNo(carPlateNo);
  176.  
  177.   //Model
  178.   cout << "Please enter the model of the car: \n";
  179.   getline(cin, model);
  180.   c[total].setModel(model);
  181.  
  182.   //+1 to total
  183.   myPlus();
  184.  
  185.   cout << "-------------------------------------------" << endl;
  186.  
  187.   cout << "Press enter to continue . . .";
  188.   cin.clear();
  189.   cin.ignore(1000, '\n');
  190.   system("clear"); //erase previous output
  191.   main(); //return to main() function
  192. }
  193.  
  194. //OPTION 2: OPTION 1: Display only one car of user's choice
  195. void disCar() {
  196.   int sd; //sd(single display) is the number of the car that user wants to display;
  197.   do {
  198.     //user selects which car they want to display
  199.     cout << "Enter 1 to display the first car that was created,2 for the second, etc." << endl;
  200.     cout << "Please enter which vehicle you want to display: ";
  201.     cin >> sd;
  202.     //checks if the user chose within the number of recorded cars
  203.     if ((sd <= 0) || (sd > total) || (cin.fail())) {
  204.       system("clear");
  205.       cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
  206.         "Please enter a number within range." << endl;
  207.       cout << endl;
  208.     }
  209.   } while ((sd <= 0) || (sd > total) || (cin.fail())); //allow user to re-enter
  210.  
  211.   //Print user's selected car details
  212.   cout << "------------------Car #" << sd << "------------------" << endl;
  213.   c[sd - 1].display();
  214.   cout << "-------------------------------------------" << endl;
  215.  
  216.   getchar(); //pauses screen before previous output is erased
  217.   cout << "Press enter to continue . . .";
  218.   cin.clear();
  219.   cin.ignore(1000, '\n');
  220.   system("clear");
  221.   main();
  222.  
  223. }
  224.  
  225. //OPTION 2: OPTION 2: Display all cars that is in the records
  226. void disCreated() {
  227.   for (int i = 0; i < total; i++) {
  228.     cout << "------------------Car #" << i + 1 << "------------------" << endl;
  229.     c[i].display();
  230.     cout << "-------------------------------------------" << endl;
  231.   }
  232.  
  233.   cout << "Press enter to continue . . .";
  234.   getchar();
  235.   system("clear");
  236.   main();
  237. }
  238.  
  239. //OPTION 2: Display Options Menu
  240. void displayRecord() {
  241.   cout << "Select the choice"<<endl
  242.     <<"1.Display a single car" <<endl
  243.     <<"2.Display all cars in the car record"<<endl;
  244.  
  245.   cout << "Choice: ";
  246.   cin >> choice;
  247.   cin.clear();
  248.   cin.ignore(1, '\n');
  249.   system("clear");
  250.  
  251.   switch (choice) {
  252.   case 1:
  253.     //print error messege to user when there is no cars in record
  254.     if (total == 0) {
  255.       cout << "You have no created cars available." << endl;
  256.       main();
  257.     }
  258.     //goes to disCar() function (display single car)
  259.     disCar();
  260.     break;
  261.  
  262.   case 2:
  263.     if (total == 0) {
  264.       cout << "You have no created cars available." << endl;
  265.       main();
  266.     }
  267.     disCreated();
  268.     break;
  269.  
  270.   default:
  271.     //print error messege and allow user to re-enter choice
  272.     cout << choice << " is not valid choice" << endl;
  273.     displayRecord();
  274.   }
  275. }
  276.  
  277. //OPTION 3: Updating/Overriding previous car details of user's choice
  278. //Note: updateRecord() is similar to addRecord()
  279. void updateRecord() {
  280.   int u; //u is the number of the vehicle that user wants to update;    
  281.   cout << "------------------=UPDATE=------------------" << endl;
  282.  
  283.   do {
  284.     //user selects which car they want to update
  285.     cout << "Enter 1 to update the first cars that was created,2 for the second, etc." << endl;
  286.     cout << "Please enter which vehicle you want to update: ";
  287.     cin >> u;
  288.     //checks if the user chose within the number of recorded cars
  289.     if ((u <= 0) || (u > total) || (cin.fail())) {
  290.       system("clear");
  291.       cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
  292.         "Please enter a number within range." << endl;
  293.         cin.clear();
  294.         cin.ignore(1000,'\n');
  295.       cout << endl;
  296.     }
  297.   } while ((u <= 0) || (u > total) || (cin.fail()));
  298.  
  299.   //Prints details of the car user wants to update
  300.   cout << endl;
  301.   cout << "------------------Current Car #" << u << "------------------" << endl;
  302.   c[u - 1].display();
  303.   cout << "-------------------------------------------" << endl;
  304.   cout << endl;
  305.  
  306.   //Manufacturer
  307.   cout << "Please enter the manufacturer of the car: ";
  308.   cin.clear();
  309.   cin.ignore(1000, '\n');
  310.   getline(cin, manufacturer);
  311.   c[u - 1].setManufacturer(manufacturer);
  312.  
  313.   //Type
  314.   c[u - 1].setType(funcType());
  315.  
  316.   //SeatNumber
  317.   string ynError;
  318.   do {
  319.     cout << "Do you know what is the number of seats?(y/n)\n";
  320.     cin >> option;
  321.     ynError = checker2(option);
  322.   } while ((ynError == "a"));
  323.  
  324.   if ((option == "y") || (option == "Y")) {
  325.     do {
  326.       cin.clear();
  327.       cin.ignore(1000, '\n');
  328.       cout << "Please enter number of seats: \n";
  329.       cin >> seat;
  330.  
  331.       if ((cin.fail()) || (seat <= 0)) {
  332.         cout << endl;
  333.         cout << "Invalid input. Please key in a positive integer." << endl; //print error message
  334.       }
  335.  
  336.     } while ((cin.fail()) || (seat <= 0)); //condition to loop and allow user to re-enter
  337.  
  338.     c[total].setSeat(seat);
  339.   } else {
  340.     c[total].setSeat(1); //seats number is set to 1 if it is unknown(default)
  341.   }
  342.  
  343.   //Car Plate Number
  344.   char cpnError; //cpnError is used to check for special characters and certain alphabets
  345.   cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  346.   cin.ignore(1, '\n');
  347.   getline(cin, carPlateNo);
  348.   cpnError = checker(carPlateNo);
  349.  
  350.   if (cpnError == 'a') { //if carPlateNo have any special characters or certain alphabets
  351.     do {
  352.       cout << "Please enter the car plate number(e.g.: PNM1234): \n";
  353.       getline(cin, carPlateNo);
  354.       cpnError = checker(carPlateNo);
  355.     } while (cpnError == 'a'); //will loop and allow user to re-enter if special characters/certain alphabets was entered
  356.   }
  357.   c[u - 1].setCarPlateNo(carPlateNo);
  358.  
  359.   //Model
  360.   cout << "Please enter the model of the car: \n";
  361.   getline(cin, model);
  362.   c[u - 1].setModel(model);
  363.  
  364.   cout << "-------------------------------------------" << endl;
  365.  
  366.   cout << "Press enter to continue . . .";
  367.   cin.clear();
  368.   cin.ignore(1000, '\n');
  369.   system("CLS");
  370.   main();
  371. }
  372.  
  373. //OPTION 4: Delete a car of user's choice
  374. void deleteRecord() {
  375.   string ynError;
  376.   int d; //d is number of the car that user wants to delete;
  377.   cout << "------------------=DELETE=------------------" << endl;
  378.  
  379.   do {
  380.     //user selects which car they want to delete
  381.     cout << "Enter 1 to delete the first car that was created,2 for the second, etc." << endl;
  382.     cout << "Please enter which car you want to delete: ";
  383.     cin >> d;
  384.     if ((d <= 0) || (d > total) || (cin.fail())) {
  385.       system("clear");
  386.       cout << "Invalid input. You only have " << total << " car(s) in the car record." << endl <<
  387.         "Please enter a number within range." << endl;
  388.       cout << endl;
  389.     }
  390.   } while ((d <= 0) || (d > total) || (cin.fail()));
  391.   cout << "------------------Car #" << d << "------------------" << endl;
  392.   c[d - 1].display();
  393.   cout << "-------------------------------------------" << endl;
  394.   cout << endl;
  395.  
  396.     do {
  397.       cout << "Are you sure you want to delete this car?(y/n)" << endl;
  398.       cin.clear();
  399.       cin.ignore();
  400.       cin >> option;
  401.       ynError = checker2(option);
  402.     } while (ynError == "a");
  403.  
  404.     if ((option == "y") || (option == "Y")) {
  405.       for (int i = d - 1; i < SIZE - 1; i++) {
  406.         c[i] = c[d]; //replaces a car's details with details of the next car starting from the car that the user chose to delete
  407.         d++;
  408.       }
  409.       c[SIZE - 1] = Car(); //give the last car(car #80) default values
  410.  
  411.       myMinus();
  412.     }
  413.  
  414.   cout << "Press enter to continue . . .";
  415.   cin.clear();
  416.   cin.ignore(1000, '\n');
  417.   system("CLS");
  418.   main();
  419. }
  420.  
  421. //OPTION 5: Creating a .txt file(To save car record)
  422. void textFile() {
  423.   //declare variables
  424.   string fileName, ma, ty, cpn, mo;
  425.   int se;
  426.  
  427.   cout << "------------------=Create Txt File=------------------" << endl;
  428.   //Create file with the file name user entered
  429.   cout << "Enter file name: ";
  430.   cin >> fileName;
  431.   cout << "Your .txt file can be found in the same folder where your .cpp file is saved." << endl;
  432.   fileName = fileName + ".txt";
  433.  
  434.   //output to .txt file
  435.   ofstream file(fileName.c_str());
  436.  
  437.   if (file.is_open()) {
  438.     //output all the cars in car record
  439.     for (int i = 0; i < total; i++) {
  440.       file << "------------------Car #" << i + 1 << "------------------" << endl;
  441.       ma = c[i].getManufacturer(); //get the manufacturer value of the car stored in the array
  442.       file << "Manufacturer: " << ma << endl;
  443.       ty = c[i].getType();
  444.       file << "Type: " << ty << endl;
  445.       se = c[i].getSeat();
  446.       file << "Seat: " << se << endl;
  447.       cpn = c[i].getCarPlateNo();
  448.       file << "Car Plate Number: " << cpn << endl;
  449.       mo = c[i].getModel();
  450.       file << "Model: " << mo << endl;
  451.       file << "-------------------------------------------" << endl;
  452.     }
  453.     file.close(); //close file
  454.   }
  455.   cin.get();
  456.   cout << "-----------------------------------------------------" << endl;
  457.  
  458.   cout << "Press enter to continue . . .";
  459.   cin.clear();
  460.   cin.ignore(1000, '\n');
  461.   system("CLS");
  462.   main();
  463. }
  464.  
  465. int main() {
  466.   cout << "===============WELCOME TO CAR MANAGEMENT SYSTEM===============" << endl;
  467.   cout << "Please enter your selection" << endl <<
  468.     "1. Create new car" << endl <<
  469.     "2. View car(s)" << endl <<
  470.     "3. Update car" << endl <<
  471.     "4. Delete  " << endl <<
  472.     "5. Create txt file" << endl <<
  473.     "6. Exit" << endl;
  474.  
  475.   cout << "Choice: ";
  476.   cin >> choice;
  477.   cin.clear();
  478.   cin.ignore(1, '\n');
  479.   system("clear");
  480.  
  481.   switch (choice) {
  482.   case 1:
  483.     if (total == 80) {
  484.       cout << "You have reached the maximum number of cars that can be created" << endl;
  485.     }
  486.     addRecord();
  487.     break;
  488.  
  489.   case 2:
  490.     displayRecord();
  491.     break;
  492.  
  493.   case 3:
  494.     //print error messege to user when there is no cars in record
  495.     if (total == 0) {
  496.       cout << "You have no created cars available." << endl;
  497.       main();
  498.     }
  499.     updateRecord();
  500.     break;
  501.  
  502.   case 4:
  503.     if (total == 0) {
  504.       cout << "You have no created cars available." << endl;
  505.       main();
  506.     }
  507.     deleteRecord();
  508.     break;
  509.  
  510.   case 5:
  511.     textFile();
  512.     break;
  513.  
  514.   case 6:
  515.     exit(1); //behaves like return 1 and ends the program
  516.     break;
  517.  
  518.   default:
  519.     cout << choice << " is not valid choice" << endl;
  520.  
  521.     main();
  522.   }
  523.  
  524.   return 0;
  525. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top