I am a beginner in C++ and I am trying to make a To-Do program. But when I am selecting 1 Create a To-do
and entering more than a word, the do{}while(choice != 9
) loop goes into an infinite loop showing the menu repeatedly. I tried using cin.ignore()
and was able to enter a string of more than a word but then a second time if I try to enter a string(1 Create a To-do
) it again goes to an infinite loop.
Below is my code, it's not complete but I have written the code for 1st two options in the menu. Please if anyone can help me figure out where to use cin.ignore()
and how to use it, it would be really helpful. Thanks.
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
// ======================= USER CLASS ============================================================================================
class User{
string fname;
int id;vector<string> current;
vector<string> done;
public:
static int count;
string pass;
User() { count++; id = count; }
void setfname(string fn) { fname = fn; }
string getfname() { return fname; }
int getid() { return id; }
void greetnewux() {
cout << "\n\n>>>>> Welcome, " << fname << endl;
cout << ">>>>> Your ID is : " << id << endl;
cout << "(Note: Please remember this id, it will be needed in Login)" << endl;
}
void createtodo(string todo) {
current.push_back(todo);
cout << "\n\tSuccessfully Entered!!";
cout << "!!!!!!!!!!!!!!!!Out of User Create todo";
}
void setpass(string p) { pass = p; cout << "\nPASSWORD SUCCESSFULLY SET!"; }
void deletetodo(int i) {
int input = 0;
switch (i) {
case 1:
cout << "\n<<<<<<< INCOMPLETE TO-DOS >>>>>>>\n\n";
i = 0;
while (i < current.size()) {
cout << i + 1 << " " << current[i] << endl;
i++;
}
cout << "\n\nENTER THE TO-DO NUMBER TO DELETE : ";
cin >> input;
if(input<=current.size() && input!=0){
current.erase(current.begin()+(input-1));
cout << "\n\tSuccessfully DELETED!!";
}
else { cout << "\n\n!!! INVALID INPUT !!! NUMBER NOT PRESENT IN LIST "; }
break;
case 2:
cout << "\n<<<<<<< COMPLETED TO-DOS >>>>>>>\n\n";
i = 0;
while (i < done.size()) {
cout << i + 1 << " " << done[i] << endl;
i++;
}
cout << "\n\nENTER THE TO-DO NUMBER TO DELETE : ";
cin >> input;
if (input > done.size() + 1 && input != 0) {
done.erase(done.begin() + (input - 1));
cout << "\n\tSuccessfully DELETED!!";
}
else { cout << "\n\n!!! INVALID INPUT !!! NUMBER NOT PRESENT IN LIST "; }
break;
}
}
};
int User::count = 0;
// ======================= ADMIN CLASS ============================================================================================
class Admin {
public:
int random;
int id;
map<int, User> DB;
static vector<string> motivation;
Admin() { }
bool ifpresent(int i){
if (DB.find(i) == DB.end()) {return false; }
else { return true; }
}
bool checkpass(string p, int i){
if (DB[i].pass == p) {
User& ux = DB[i]; id = i;
cout << "\nWelcome back, "<<ux.getfname()<<" !";
random = rand() % motivation.size();
string sel_elem = motivation[random];
cout << "\n\n\t" << sel_elem << "\n";
return true;
}
else {
cout << "\n\n\t!!!WRONG PASSWORD!!!";
return false;
}
}
void createtodo() {
User& ux = DB[id];
string todo;
cout << "\nEnter the to-do: ";
getline(cin, todo);
ux.createtodo(todo);
cout << "!!!!!!!!!!!!!!!!Out of Admin Create todo";
};
bool createacc() {
User u; string fn; string p;
cout << "\nEnter Your First Name: ";
cin >> fn;
u.setfname(fn);
cout << "\nFirst Name: " << fn;
cout << "\nSet your Password: ";
cin >> p;
u.setpass(p);
DB[u.getid()] = u; //Entering User in DataBase
u.greetnewux();
random = rand() % motivation.size();
string sel_elem = motivation[random];
cout << "\n\n\t" << sel_elem << "\n";
return true;
};
void deltodo(){
int i;
auto& u = DB[id];
cout << "\n\nDo you want to Delete: ";
cout << "\n1 An incompleted to-do?";
cout << "\nOR";
cout << "\n2 A completed to-do?";
cin >> i;
if (i == 1 || i == 2) { u.deletetodo(i); }
else { cout << "\n\n!!! INVALID INPUT !!! Please enter 1 or 2 !!"; }
}
~Admin() { }
};
vector<string> Admin::motivation = { "“You do not find the happy life. You make it”",
"“The most wasted of days is one without laughter.” ",
"“Make each day your masterpiece.”",
"“Happiness is not by chance, but by choice.”",
"“It is never too late to be what you might have been.”",
"“Dream big and dare to fail.”",
};
// ======================= MAIN FUNCTION ============================================================================================
int main()
{
Admin A;
int id; string pass;
int input; int choice;
bool flag = true;
cout << "\n\n*************************************** TO DO APP ***************************************\n\n\n\n";
do {
cout << "\n\n1 Login";
cout << "\n\n2 Create Account\n\n";
cout << "Enter 1 or 2 : ";
cin >> input;
switch(input){
case 1:
cout << "\n\t\t\t<<<<<<< LOGIN PAGE >>>>>>>\n\n";
cout << "\nEnter your id: ";
cin >> id;
if (A.ifpresent(id)) {
cout << "\nid " << id; cout << "\nEnter Password: "; cin >> pass;
if (A.checkpass(pass, id)) {
flag = false;
}
cout << "\n\n\t!!! ENTER CORRECT DETAILS !!!!\n\n";
}
else { cout << "\n\n\t!!!Account Not Found!!!\n\n"; }
break;
case 2:
cout << "\n\t\t\t<<<<<<< ACCOUNT CREATION PAGE >>>>>>>\n\n";
if (A.createacc()) { flag = true; };
break;
default:
cout << "\n\n\t\t!!! INVALID INPUT !!! Please enter 1 or 2 !!";
break;
}
} while (flag);
do{
cout << "\n\n**ENTER THE NUMBER**\n";
cout << "\n1 Create a To-do";
cout << "\n2 Delete a To-do";
cout << "\n3 Mark as Done";
cout << "\n4 Mark as Not done";
cout << "\n5 Show Completed To-do";
cout << "\n6 Show Remaining To-do";
cout << "\n7 Remove all To-do";
cout << "\n\n 8 Clear Screen";
cout << "\n 9 Log Out [>\n\n";
cout << "Enter: ";
cin >> choice;
switch (choice) {
case 1:
A.createtodo();
break;
case 2:
A.deltodo();
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
system("cls");
break;
case 9:
cout << "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< LOGGED OUT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n\n\n\n\n\n\n";
break;
default:
cout << "\n\nINVALID INPUT";
}
} while (choice != 9);
}
Aucun commentaire:
Enregistrer un commentaire