Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- void deleteFlavour(){
- bool isThereAnId = false ; // this boolean is used to see wether theres and id or not
- string line, name; // these variables will contain the read line from the textfile and the id user will enter
- cout << "Please Enter the ID of record you want to delete: ";
- cin >> name; // will store what user entered in the variable called name
- ifstream myfile; //instream to read file
- ofstream temp; //outstream to write file
- //open the files
- myfile.open("list.txt");
- temp.open("temp.txt");
- //loop the myfile which is list.txt
- //this while method will loop the text file and get every line to a variable called line which we declared at the top
- while (getline(myfile, line))
- {
- string s = line.substr (0,name.length() );
- if(name != s){
- // skip all the id user wants to delete
- // amd write everything on the temp file
- temp << line + "\n";
- }
- else {
- //id found
- isThereAnId = true ;
- }
- }
- if(isThereAnId) {
- cout << "Record Deleted \n" << endl;
- }
- else {
- cout << "ID Not found \n";
- }
- myfile.close();
- temp.close();
- remove("list.txt");
- rename("temp.txt", "list.txt");
- }
- int loginstatus = 0 ;
- // if 0 , login ;
- // if 1 , logout;
- void editFlavour(){
- bool isThereAnId = false ;
- string line, name,edit;
- cout << "Please Enter the ID of record you want to Edit: ";
- cin >> name; // this will be the id user will add
- cout << "Please Enter the new Flavour ";
- cin >> edit ;
- ifstream myfile; //instream
- ofstream temp; //outstream
- //open the original and make a temp file
- myfile.open("list.txt");
- temp.open("temp.txt");
- while (getline(myfile, line))
- {
- string neweditedline;
- string s = line.substr (0,name.length() );
- //assuming
- neweditedline = s+" "+edit;
- if(name != s){
- temp << line + "\n";
- }
- else {
- isThereAnId = true ;
- cout << "Editing line with " + neweditedline;
- temp << neweditedline + "\n";
- }
- }
- if(isThereAnId){
- cout << "\n The record has been Edited " << endl;
- }
- else {
- cout << "ID Not found \n";
- }
- myfile.close();
- temp.close();
- remove("list.txt");
- rename("temp.txt", "list.txt");
- }
- int getId(){
- ifstream inFile ;
- inFile.open("list.txt");
- int count = 0 ;
- string item;
- while(!inFile.eof()){
- inFile >> item ;
- count++;
- }
- return count + 1;
- }
- void addFlavour(){
- ofstream out; //outstream
- out.open("list.txt", ios_base::app);
- string item ;
- int id;
- id = getId();
- cout << "Please Enter Flavour \n";
- cin >> item ;
- out << id ;
- out << " ";
- out << item ;
- out << "\n";
- // it will save the flavour like this 01 chocolate
- cout << "Flavour " + item + " Added success" ;
- }
- void viewAllFlavours(){
- string line ;
- ifstream inFile ;
- inFile.open("list.txt");
- if(inFile.fail()){
- cout << "Error opening the File ";
- }
- else {
- string item ;
- while(getline(inFile, line))
- {
- cout << line;
- cout << "\n";
- }
- }
- }
- void menu(){
- int options ;
- bool ex = true ;
- while(ex) {
- cout << "Welcome to World Ice cream\n" ;
- 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";
- cin >> options ;
- switch(options){
- case 1: {
- viewAllFlavours();
- break;
- }
- case 2: {
- addFlavour();
- break;
- }
- case 3 :{
- deleteFlavour();
- break;
- }
- case 4 :{
- editFlavour();
- break;
- }
- case 5 : {
- ex = false;
- break;
- }
- case 6 : {
- ex = false;
- break;
- }
- default : //Optional
- ex << true;
- }
- }
- }
- int log(){
- cout<<"\n\t\tWELCOME TO ICE WORLD";
- cout<< endl;
- cout<< endl;
- string u,p,u1,p1 ;
- u1="p";
- p1="1234";
- int loginAttempt = 0;
- while (loginAttempt < 3)
- {
- cout<<"\n\t\tUSERNAME :" ;
- cin>>u;
- cout<< endl;
- cout<<"\t\tPASSWORD :";
- cin>>p;
- cout<< endl;
- if (u1==u & p1==p)
- menu();
- else
- cout << "Invalid login attempt. Please try again.\n" << '\n';
- loginAttempt++;
- }
- if (loginAttempt == 3)
- {
- cout << "Too many login attempts! The program will now terminate.";
- }
- }
- int main(){
- log();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement