Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5.  
  6.  
  7.  
  8. using namespace std;
  9.  
  10. void deleteFlavour(){
  11.  
  12. bool isThereAnId = false ; // this boolean is used to see wether theres and id or not
  13.  
  14.  
  15. string line, name; // these variables will contain the read line from the textfile and the id user will enter
  16.  
  17.  
  18. cout << "Please Enter the ID of record you want to delete: ";
  19. cin >> name; // will store what user entered in the variable called name
  20.  
  21.  
  22. ifstream myfile; //instream to read file
  23. ofstream temp; //outstream to write file
  24.  
  25.  
  26.  
  27. //open the files
  28. myfile.open("list.txt");
  29. temp.open("temp.txt");
  30.  
  31.  
  32. //loop the myfile which is list.txt
  33. //this while method will loop the text file and get every line to a variable called line which we declared at the top
  34.  
  35. while (getline(myfile, line))
  36. {
  37.  
  38. string s = line.substr (0,name.length() );
  39.  
  40.  
  41.  
  42. if(name != s){
  43.  
  44. // skip all the id user wants to delete
  45. // amd write everything on the temp file
  46. temp << line + "\n";
  47.  
  48.  
  49. }
  50. else {
  51. //id found
  52. isThereAnId = true ;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. }
  59.  
  60. if(isThereAnId) {
  61. cout << "Record Deleted \n" << endl;
  62. }
  63. else {
  64. cout << "ID Not found \n";
  65. }
  66.  
  67. myfile.close();
  68. temp.close();
  69. remove("list.txt");
  70. rename("temp.txt", "list.txt");
  71. }
  72.  
  73.  
  74. int loginstatus = 0 ;
  75. // if 0 , login ;
  76. // if 1 , logout;
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. void editFlavour(){
  84. bool isThereAnId = false ;
  85.  
  86. string line, name,edit;
  87.  
  88. cout << "Please Enter the ID of record you want to Edit: ";
  89. cin >> name; // this will be the id user will add
  90. cout << "Please Enter the new Flavour ";
  91. cin >> edit ;
  92.  
  93. ifstream myfile; //instream
  94. ofstream temp; //outstream
  95.  
  96.  
  97. //open the original and make a temp file
  98. myfile.open("list.txt");
  99. temp.open("temp.txt");
  100.  
  101. while (getline(myfile, line))
  102. {
  103.  
  104.  
  105.  
  106. string neweditedline;
  107.  
  108. string s = line.substr (0,name.length() );
  109.  
  110. //assuming
  111.  
  112. neweditedline = s+" "+edit;
  113.  
  114.  
  115. if(name != s){
  116. temp << line + "\n";
  117.  
  118.  
  119. }
  120. else {
  121.  
  122. isThereAnId = true ;
  123. cout << "Editing line with " + neweditedline;
  124. temp << neweditedline + "\n";
  125.  
  126. }
  127.  
  128.  
  129.  
  130. }
  131.  
  132.  
  133. if(isThereAnId){
  134. cout << "\n The record has been Edited " << endl;
  135. }
  136. else {
  137. cout << "ID Not found \n";
  138. }
  139.  
  140. myfile.close();
  141. temp.close();
  142. remove("list.txt");
  143. rename("temp.txt", "list.txt");
  144.  
  145. }
  146.  
  147.  
  148. int getId(){
  149. ifstream inFile ;
  150. inFile.open("list.txt");
  151. int count = 0 ;
  152. string item;
  153. while(!inFile.eof()){
  154.  
  155. inFile >> item ;
  156. count++;
  157. }
  158.  
  159. return count + 1;
  160. }
  161.  
  162. void addFlavour(){
  163.  
  164.  
  165. ofstream out; //outstream
  166.  
  167.  
  168. out.open("list.txt", ios_base::app);
  169.  
  170.  
  171. string item ;
  172. int id;
  173.  
  174. id = getId();
  175.  
  176. cout << "Please Enter Flavour \n";
  177. cin >> item ;
  178.  
  179.  
  180.  
  181. out << id ;
  182. out << " ";
  183. out << item ;
  184. out << "\n";
  185. // it will save the flavour like this 01 chocolate
  186.  
  187. cout << "Flavour " + item + " Added success" ;
  188.  
  189. }
  190.  
  191.  
  192.  
  193.  
  194. void viewAllFlavours(){
  195.  
  196. string line ;
  197. ifstream inFile ;
  198.  
  199. inFile.open("list.txt");
  200.  
  201. if(inFile.fail()){
  202. cout << "Error opening the File ";
  203.  
  204. }
  205. else {
  206. string item ;
  207. while(getline(inFile, line))
  208. {
  209.  
  210. cout << line;
  211. cout << "\n";
  212.  
  213.  
  214. }
  215.  
  216. }
  217.  
  218.  
  219. }
  220.  
  221. void menu(){
  222.  
  223. int options ;
  224. bool ex = true ;
  225.  
  226. while(ex) {
  227. cout << "Welcome to World Ice cream\n" ;
  228. cout << "Please choose an option \n 01) View All Icecream Flavours\n 02) Add new Flavour\n 03) Delete flavour\n 04) Edit Flavour\n 05) logout";
  229. cin >> options ;
  230.  
  231. switch(options){
  232.  
  233. case 1: {
  234. viewAllFlavours();
  235. break;
  236.  
  237. }
  238. case 2: {
  239. addFlavour();
  240. break;
  241. }
  242.  
  243. case 3 :{
  244. deleteFlavour();
  245. break;
  246. }
  247.  
  248. case 4 :{
  249. editFlavour();
  250. break;
  251. }
  252.  
  253. case 5 : {
  254. ex = false;
  255. break;
  256. }
  257.  
  258. case 6 : {
  259. ex = false;
  260. break;
  261. }
  262.  
  263. default : //Optional
  264. ex << true;
  265.  
  266. }
  267. }
  268.  
  269. }
  270.  
  271.  
  272. int log(){
  273.  
  274.  
  275. cout<<"\n\t\tWELCOME TO ICE WORLD";
  276. cout<< endl;
  277. cout<< endl;
  278.  
  279. string u,p,u1,p1 ;
  280. u1="p";
  281. p1="1234";
  282.  
  283. int loginAttempt = 0;
  284.  
  285. while (loginAttempt < 3)
  286. {
  287.  
  288. cout<<"\n\t\tUSERNAME :" ;
  289. cin>>u;
  290. cout<< endl;
  291. cout<<"\t\tPASSWORD :";
  292. cin>>p;
  293. cout<< endl;
  294.  
  295.  
  296. if (u1==u & p1==p)
  297. menu();
  298. else
  299. cout << "Invalid login attempt. Please try again.\n" << '\n';
  300. loginAttempt++;
  301.  
  302. }
  303.  
  304.  
  305. if (loginAttempt == 3)
  306. {
  307. cout << "Too many login attempts! The program will now terminate.";
  308.  
  309. }
  310. }
  311.  
  312.  
  313.  
  314.  
  315.  
  316. int main(){
  317.  
  318.  
  319.  
  320. log();
  321.  
  322.  
  323.  
  324.  
  325.  
  326. return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement