Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.81 KB | None | 0 0
  1. // A program that allows the user to display all current projects, look up
  2. // a project based on name or date, add new projects, and continue to look up
  3. // and add projects until they choose to exit.
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. enum Question {YES, NO};
  12.  
  13. struct dateTime{
  14.     string request;
  15.     string used;
  16.     string start;
  17.     string finish;
  18. };
  19.  
  20. struct toDoList{
  21.     string projName;
  22.     dateTime timeGive;
  23.     char completeYN;
  24.     string description;
  25. };
  26.  
  27.  
  28. //Pre: allows the user to answer either yes or no
  29. //Post: returns either yes or no
  30. Question askQ();
  31.  
  32. //Pre: GetProjects holds the Projects variable
  33. // Post: the fields are written for Projects
  34. toDoList GetProject(ifstream &inData);
  35.  
  36. // Prompt user for input file to be used
  37. // Pre: inData declared
  38. // Post: the program searches for the program via the name input
  39. void GetFileInfo(ifstream& inData);
  40.  
  41. // Function should print the projects contained in the allCars array
  42. // Pre: allProjects[] contains 0 or more projects
  43. //      projCount contains the number of projects in allProjects[]
  44. // Post: Contents of allProjects[] printed to the screen
  45. void PrintProjects(const toDoList allProjects[], int projCount);
  46.  
  47. //Pre: prompts the user if they want to view the projects
  48. //Post: displays the projects
  49. // prompts the user to display and displays
  50. void displayProjects(ifstream &inData, toDoList Project, Question YNans);
  51.  
  52. //Pre: file outData has been opened
  53. //Post: the fields of Project are written on the outData
  54. //      file and labeled
  55. void WriteProjects(ofstream &outData, toDoList Project);
  56.  
  57. //Pre: prompts the user to search for programs
  58. //Post: searches for the programs by date or name
  59. // prompts the user to search by either name or date
  60. char searchProject(ifstream &inData, toDoList Project, Question YNans);
  61.  
  62. //Pre: prompts the user to add new projects
  63. //Post: adds new projects to the output file
  64. //prompts the user to add files and add if yes
  65. char addNewProjects(ofstream &outData, toDoList Project, Question YNans);
  66.  
  67. //Pre: prompts the user to continue searching
  68. //Post: allows the user to search for more programs
  69. // prompts the user if they would like to continue searching
  70. // and restarts the search if yes
  71. bool continueLook();
  72.  
  73. const int MAX_PROJECTS = 50;
  74.  
  75. int main(){
  76.     ifstream inData;
  77.     ofstream outData;
  78.     Question YNans;
  79.     bool answer = true;     // bool for continuation of the program
  80.  
  81.  
  82.  
  83.     inData.open("Projects.txt");
  84.     outData.open ("ProjectsComplete.txt");
  85.  
  86.  
  87.     GetFileInfo(inData);
  88.  
  89.     toDoList allprojects[MAX_PROJECTS] = GetProject(inData);
  90.     while(inData){
  91.  
  92.         toDoList allprojects[MAX_PROJECTS] = GetProject(inData);
  93.  
  94.         toDoList Project = GetProject(inData);
  95.  
  96.         displayProjects(inData, Project, YNans);
  97.  
  98.         WriteProjects(outData, Project);
  99.  
  100.         searchProject(inData, Project, YNans);
  101.  
  102.         addNewProjects(outData, Project, YNans);
  103.  
  104.         bool answer = continueLook();
  105.         if(answer = true){
  106.             cout << "We go again!\n";
  107.         }
  108.         else if(answer == false){
  109.             cout << "Smell ya later.\n" << endl;
  110.             break;
  111.         }
  112.     }
  113.  
  114. return 0;
  115. }
  116.  
  117. Question askQ(){
  118.         char input;
  119.         cin >> input;
  120.         if (input == 'Y' || input == 'y'){
  121.             return YES;
  122.         }
  123.         else if(input == 'N' || input == 'n'){
  124.             return NO;
  125.         }
  126. }
  127.  
  128. toDoList GetProject(ifstream &inData){
  129.  toDoList Project;
  130.     inData >> Project.projName;
  131.     inData >> Project.timeGive.start;
  132.     inData >> Project.timeGive.finish;
  133.     inData >> Project.timeGive.request;
  134.     inData >> Project.timeGive.used;
  135.     inData >> Project.completeYN;
  136.     inData >> Project.description;
  137.     getline(inData, Project.description);
  138.     inData.ignore (2, '\n');
  139.     return Project;
  140. }
  141.  
  142. void PrintProjects(const toDoList allProjects[], int projCount){
  143.     cout << "in print.\n";
  144.     for(int i = 0; i < projCount; i++){
  145.          displayProjects(allProjects[i]);
  146.     }
  147. }
  148.  
  149. void displayProjects(ifstream &inData, toDoList Project, Question YNans){
  150. cout << "Would you like to display your projects? Y for yes or N for no.\n";
  151. YNans = askQ();
  152.  
  153.     if (YNans == YES){
  154.  
  155.     cout << "Name: " << Project.projName << endl;
  156.     cout << "Date started: " << Project.timeGive.start << endl;
  157.     cout << "Date ended: " << Project.timeGive.finish << endl;
  158.     cout << "Time required: " << Project.timeGive.request << endl;
  159.     cout << "Time expended: " << Project.timeGive.used << endl;
  160.     cout << "Complete? " << Project.completeYN << endl;
  161.     cout << "Description: " << Project.description << endl;
  162.  
  163.     }
  164.     else if(YNans == NO){
  165.        cout << "I see you like to live on the edge.\n" << endl;
  166.     }
  167. }
  168.  
  169. void WriteProjects(ofstream &outData, toDoList Project){
  170.     outData << "Name: " << Project.projName << endl;
  171.     outData << "Date started: " << Project.timeGive.start << endl;
  172.     outData << "Date ended: " << Project.timeGive.finish << endl;
  173.     outData << "Time required: " << Project.timeGive.request << endl;
  174.     outData << "Time expended: " << Project.timeGive.used << endl;
  175.     outData << "Complete? " << Project.completeYN << endl;
  176.     outData << "Description: " << Project.description << endl;
  177. }
  178.  
  179. char searchProject(ifstream &inData, toDoList Project, Question YNans){
  180.     string name;
  181.     string day;
  182.  
  183.     cout << "Would you like to search?\n";
  184.     YNans = askQ();
  185.  
  186.     if (YNans == YES){
  187.         cout << "Would you like to search by name?\n";
  188.         YNans = askQ();
  189.  
  190.         if (YNans == YES){
  191.             cout << "Please enter a name to search by.\n";
  192.             cin >> name;
  193.  
  194.  
  195.  
  196.         }
  197.         else if(YNans == NO){
  198.             cout << "Would you like to search by date?\n";
  199.             YNans = askQ();
  200.             if (YNans == YES){
  201.                 cout << "Please enter a date to search by.\n";
  202.                 cin >> day;
  203.  
  204.  
  205.             }
  206.             else if(YNans == NO){
  207.                 cout << "Then I can't help you with searching.\n";
  208.             }
  209.         }
  210.     }
  211.     else if(YNans == NO){
  212.         cout << "That's okay, searching isn't my favorite thing anyway.\n" << endl;
  213.     }
  214. }
  215.  
  216. char addNewProjects(ofstream &outData, toDoList Project, Question YNans){
  217. cout << "Would you like to add any new projects? Y for yes or N for no.\n";
  218. YNans = askQ();
  219.  
  220.     if (YNans == YES){
  221.         cout << "Please enter a name for your program:\n";
  222.         cin >> Project.projName;
  223.         cout << "Enter a start date:\n";
  224.         cin >> Project.timeGive.start;
  225.         cout << "Enter an end date:\n";
  226.         cin >> Project.timeGive.finish;
  227.         cout << "Enter the expected time required:\n";
  228.         cin >> Project.timeGive.request;
  229.         cout << "Enter the time taken:\n";
  230.         cin >> Project.timeGive.used;
  231.         cout << "Was it completed?\n";
  232.         Project.completeYN = askQ();
  233.         cout << "Please enter the description of the program:\n";
  234.         cin >> Project.description;
  235.         cin.ignore();
  236.         getline(cin, Project.description);
  237.  
  238.         outData << Project.projName << ", "
  239.                 << Project.timeGive.start << ", "
  240.                 << Project.timeGive.finish << ", "
  241.                 << Project.timeGive.request << ", "
  242.                 << Project.timeGive.used << ", "
  243.                 << Project.completeYN << endl;
  244.         outData << Project.description << endl;
  245.         }
  246.  
  247.     else if(YNans == NO){
  248.         cout << "Sometimes people don't want to add anything and that's okay.\n" << endl;
  249.     }
  250. }
  251.  
  252. bool continueLook(){
  253.     cout << "Would you like to continue searching? Y for yes or N for no.\n";
  254.     bool answer = askQ();
  255.  
  256.     return answer;
  257. }
  258.  
  259. void GetFileInfo(ifstream& inData){
  260.         while (!inData){
  261.             cout << "File Error. File not found, try again." << endl;
  262.         }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement