Skip to content

Instantly share code, notes, and snippets.

@Asyraf-wa
Last active May 2, 2023 03:20
Show Gist options
  • Save Asyraf-wa/418ca0d1998f12252ca7a2189e74d9e0 to your computer and use it in GitHub Desktop.
Save Asyraf-wa/418ca0d1998f12252ca7a2189e74d9e0 to your computer and use it in GitHub Desktop.
try
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
void ShowList(std::vector<std::string>& List);
void AddToList(std::vector<std::string>& List);
void RemoveFromList(std::vector<std::string>& List);
void finish(std::vector<std::string>& List);
int main()
{
string userType;
string userName;
string userPassword;
int loginAttempt = 0;
while (loginAttempt < 5)
{
cout << "Please enter your user type: ";
cin>> userType;
if (userType=="Administrator")
{cout << "Please enter Admin user name: ";
cin >> userName;
cout << "Please enter Admin password: ";
cin >> userPassword;
}
else if (userType=="User")
{cout << "Please enter your user name: ";
cin >> userName;
cout << "Please enter your user password: ";
cin >> userPassword;
}
if (userName == "Admin" && userPassword == "123")
{
break;
}
else if (userName == "Ali" && userPassword == "456")
{
break;
}
else if (userName == "Ahmad" && userPassword == "789")
{
break;
}
else if (userName == "Siti" && userPassword == "321")
{
break;
}
else
{
cout << "Access Denied. Please try again.\n" << '\n';
loginAttempt++;
}
}
if (loginAttempt == 5)
{
cout << "Too many login attempts! The program will terminate now.....";
return 0;
}
//settle login
std::vector<std::string> List;
std::string str;
int input;
std::ifstream loadFile("List.txt");
if (!loadFile.fail()) {
while (std::getline(loadFile, str))
{
if (str.size() > 0)
List.push_back(str);
}
}
loadFile.close();
cout << " \n \n Selamat Datang ke to-do list! \n\n";
cout << "Nak buat apa? \n";
cout << "\t" << "1. View List \n";
cout << "\t" << "2. Add item to list \n";
cout << "\t" << "3. Remove item from list \n";
cout << "\t" << "4. Exit \n";
cout << ">> ";
std::cin >> input;
cout << "\n";
while (input != 4){
switch (input) {
case 1:
ShowList(List);
break;
case 2:
AddToList(List);
break;
case 3:
RemoveFromList(List);
break;
default:
break;
}
cout << "Pilih menu \n";
cout << ">> ";
std::cin >> input;
cout << "\n";
}
finish(List);
std::system("cls");
cout << " >> Program closed successfully...Goodbye \n";
return 0;
}
void RemoveFromList(std::vector<std::string>& List){//remove item form list
if (List.size() > 0){
int selection;
cout << "Which item would you like to delete (type index number) \n";
cout << ">> ";
std::cin >> selection;
cout << "\n";
if (selection <= 0 || selection > List.size()) {
cout << "*ERROR* index out of scope, select an item index from within the list!\n\n";
}
else {
List.erase(List.begin() + (selection - 1));
cout << "Item successfully Removed! Returning to menu...\n\n";
}
}else {
cout << "*ERROR* List is empty! (Add one item to create a list before trying to remove it) \n\n";
}
}
void AddToList(std::vector<std::string>& List){// add item to list
std::string newItem;
std::cin.clear();
std::cin.ignore(10000, '\n');
cout << "Please type a new item to add to your to-do list. \n";
cout << ">> ";
getline(std::cin, newItem);
cout << "\n";
List.push_back(newItem);
cout << "Item successfully Added! Returning to menu...\n\n";
}
void ShowList(std::vector<std::string>& List){// show current to-do list
if (List.size() > 0) {
cout << "TO-DO List: \n";
for (int i = 0; i < List.size(); i++) {
cout << "\t" << i+1 << ". " << List[i] << "\n";
}
cout << "End of list.....\n\n";
}else {
cout << "*ERROR* List is empty! (Add one item to create a list before trying to view list) \n\n";
}
}
void finish(std::vector<std::string>& List) {//save list into document (ready system for shutdown)
std::ofstream backUpFile("List.txt", std::ios::out | std::ios::trunc);
if (List.size() > 0) {
for (int i = 0; i < List.size(); i++) {
backUpFile << List[i] << "\n";
}
backUpFile.close();
}
else {
backUpFile.close();
std::remove("List.txt");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment