counting loops on c++ password program - c++

Hi I am creating a simple password program. The program requires the user to enter an account number and password. The following code works fine however the only problem I have is after 3 incorrect attempts I want the program to terminate with an appropriate message. I can't figure out how to get the loop to stop after 3 incorrect attempts and was hoping someone could help me with this. From what I've gathered I think I may have to use a for loop but I just can't seem to get it working properly. Thanks!
int A;
string guess;
const string pass;
const int number;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
while(A!=number || guess!=pass)
{
cout<<"Incorrect password. Try again"<<endl;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
}

How about:
for (int counter = 0; counter < 2 && (A != number || guess != pass); ++counter)
{
...
}

int attempts = 0;
while(A!=number || guess!=pass)
{
if( attempts++ == 3 )
{
cout << "Tough luck; exitting ..." << endl;
break;
}
cout<<"Incorrect password. Try again"<<endl;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
}

int A;
string guess;
const string pass = /* some value */;
const int number = /* some value */;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
int i = 0;
const int MAX_ATTEMPT = 3;
bool success;
while( ( success = ( A!=number || guess!=pass ) ) && ( ++i < MAX_ATTEMPT ) )
{
cout<<"Incorrect password. Try again"<<endl;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
}
if ( success ) /* other stuff */

Related

Elephant SQL with c++ login system

i created a cleint that requires you to register and login to acces certain apps but txt is not a efficent idea especially when i gave it to my friends so am trying to use a online data base but have 0 experince on how to do so so any help would be appreciated try to make words as simple as possiible still a biggener i tried searching online and trying diffrent ways but coudnt find something i could understand ao am still not sure of how to implement this and if its even possible in c++
here is my initial code for login and registering
void login() {
system("color 0B");
int counts = 0;
string userid, password, id, pass;
system("cls");
cout << "\t\t\t Please Enter your username and password : " << endl;
cout << "\t\t\t USERNAME:";
cin >> userid;
cout << "\t\t\t PASSWORD:";
cin >> password;
ifstream input("records.txt");
while (input >> id >> pass) {
if (id == userid && pass == password) {
counts = 1;
system("cls");
}
}
input.close();
if (counts == 1) {
secret(userid);
}
else {
cout << "\nLOGIN ERROR \n Username or password is inccorect\n\n";
main();
}
};
void Register() {
system("color 0B");
system("cls");
string ruserid, rpassword, rid, rpass;
string choice_2;
cout << "\t\t\t Welcome to my application you can register below\n";
cout << "press 1 to register \n";
cout << "press 2 to get back to the main menu \n";
cout << "\t\t\t Enter your option : ";
cin >> choice_2;
if (choice_2 == "1") {
string lock;
cout << "Enter registration password\n";
cout << "input password : ";
cin>>lock;
if (lock == "register_2006") {
system("cls");
cout << "\t\t\t Enter The USERNAME : ";
cin >> ruserid;
cout << "\t\t\t Enter The PASSWORD : ";
cin >> rpassword;
ofstream f1("records.txt", ios::app);
f1 << ruserid << ' ' << rpassword << endl;
system("cls");
cout << "\n\t\t\t Registration is Successful \n";
main();
}
else { cout << "Wrong password please contact to get it\n"; }
main();
}
else if (choice_2 == "2") {
system("cls");
main();
}
else {
cout << "wrong choice try again";
Register();
}
}

What should I do to get out of the called function and continue with the other function?

I have created a struct questions with the element testQuestion, answerQuestion and point. I also created a void function which is connected to the elements of the struct. When I call the function and input the question, the program does not continue to the other element which is the answer but turns back to input username and password.
Why does this happen?
struct professor
{
string username;
string password;
};
professor professorData;
struct question
{
char testQuestion[50];
char answerQuestion[50];
int pointQuestion;
};
question questionData;
void readProfessor(professor &professorData)
{
cout << "Enter the professor username: ";
cin >> professorData.username;
cout << "Enter the professor password: ";
cin >> professorData.password;
cout << endl;
}
void readQuestion(question &questionData)
{
cout << "Enter the question: " << endl;
cin >> questionData.testQuestion;
cout << "Enter the answers: " << endl;
cin >> questionData.answerQuestion;
}
void professorMenu()
{
string pUsername;
string pPassword;
cout << "Enter professor username: ";
cin >> pUsername;
cout << "Enter professor password: ";
cin >> pPassword;
if ((pUsername.compare(professorData.password) == 0) && (pPassword.compare(professorData.password) == 0))
{
readQuestion(questionData);
}
}
Calling from here
adminMenu()
{
int adminUsername = 1234;
int inputAdminUsername;
char adminPassword[6] = "admin";
char inputAdminPassword[6];
int a;
string inputStudent[20];
string inputProfessor[20];
cout << "Enter admin username: ";
cin >> inputAdminUsername;
cout << "Enter admin password: ";
cin >> inputAdminPassword;
if (inputAdminUsername == adminUsername && strcmp(adminPassword, inputAdminPassword) == 0)
{
cout << "Successful login." << endl;
do
{
cout << "Press 1 to add professor" << endl;
cout << "Press 2 to add student" << endl;
cout << "Press 3 to go back to main menu" << endl;
cin >> a;
if (a == 1)
{
readProfessor(professorData);
}
else if (a == 2)
{
readStudent(studentData);
}
else if (a == 3)
mainMenu();
} while (a != 3);
}
}

How to display user account only using user ID num

Im basically making a database that stores a users account info. (Name, Phone number, ID, etc) I want to be able to display a specific persons info by entering in their ID num.
I have a struct with basic info and an array struct that stores each person and their info. I need to be able to type in a persons ID and have it display their info.
(I am still in first year of CS degree plz be gentle lol)
struct Account{
string name;
string city;
string state;
int ZIP;
int phone;
int IDNUM;
double ACT_BAL;
string LST_PMNT;};
Main fuction
const int SIZE = 20;
Account customers[SIZE];
const int NEW_INFO = 1, CHNG_INFO = 2, DISP = 3, EXIT = 4;
char choice1;
int choice;
int n;
int NEWCUST;
int results;
do
{ // menu display
cout << "Customer Database\n"
<< "----------------------------\n"
<< "1. Enter new account info\n"
<< "2. Change account info\n"
<< "3. Display all account info\n"
<< "4. Exit\n";
cin >> choice;
//respond to user input
switch (choice)
case NEW_INFO:
cout << "Would you like to enter a new cusomter?\n"
<< "(Y/N)";
cin >> choice1;
if (choice1 == 'Y' || choice1 == 'y')
{
cout << "How many new customers?" << endl; //User eneters in new customer info without having to enter in a full array worth of customers.
cin >> NEWCUST;
for (n = 0; n < NEWCUST; n++)
{
cout << "ID Number: ";
cin >> customers[n].IDNUM;
cout << "Enter in a name: ";
cin >> customers[n].name;
cout << "City: ";
cin >> customers[n].city;
cout << "State: ";
cin >> customers[n].state;
cout << "ZIP code: ";
cin >> customers[n].ZIP;
cout << "Phone number: ";
cin >> customers[n].phone;
cout << "Account Balance: ";
cin >> customers[n].ACT_BAL;
cout << "Lasy payment date: ";
cin >> customers[n].LST_PMNT;
}
}
break;
case CHNG_INFO: // Changes info
break; // displays all info (work in progress)
case DISP:
cout << "Enter customers ID number" << endl;
cin >> customers[].IDNUM;
results = linearSearch(customers, SIZE, customers[].IDNUM);
break;
case EXIT:
cout << "Cosing......" << endl; //exits progeam
break;
To get a specific user details, you can do the following:
Declaring num as short int to hold a simple User ID, you can change it. N = 50 as you've provided the constant. Now, unless the loop finds the inputted ID matching with one of the struct's idNum, it'll be executed 50 times (max).
cout << "Input account code: ";
cin >> num;
for (int i = 0; i < N; i++)
{
if (num == acc[i].idNum)
{
cout << "Name: " << acc[i].name << endl
<< "..." << endl;
}
}

input validation in loop

I'm trying to make a validation loop in C++ that checks the user's input until they enter a number between 0 and 100 and however my loop only checks the first condition. Any guidance is appreciated!
#include <iostream>
using namespace std;
int main()
{
const int max_num = 100;
const int min_num = 0;
int num;
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
do {
if (!(cin >> num))
{
cout << "ERROR:The value provided was not a number" << endl;
cin.clear();
cin.ignore(1024, '\n');
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
}
else if (num<min_num || num>max_num)
{
cout << "ERROR: value out of range" << endl;
cin.clear();
cin.ignore(1024, '\n');
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
}
} while (!(cin >> num) || (num<min_num || num>max_num));
return 0;
}
Add lots of logging to your code so that you know what it's doing. This will help you find the problem. For example, instead of:
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
Try:
cout << "Enter a number between 0 and 100" << endl;
cerr << "About to read into num outside the loop" << endl;
cin >> num;
cerr << "Read into num outside the loop, got: " << num << endl;
And so on, throughout your code. This should give you enough information to find the bug. Alternatively, use a debugger with a single step function to accomplish the same thing.
Check that in the part of while:
instead of
while (!(cin >> num) || (num<min_num || num>max_num));
this:
while (!cin || (num<min_num || num>max_num));
the same for the upper if
cin >> num means putting user input to the variable num . So you are trying to take user inputs 2 times in the loop. Maybe the check condition: (num == (int)num)will solve your problem. It will try to verify the number you have stored in num is really of the type int

Code to get user input not executing/skipping in C++

In the below code, I'm running into an error when I try to get the user to input their name. My program just skips it over and goes right over to making the function calls without allowing the user to enter their name. Despite the error, my program is compiling. I'm not sure what's going wrong as I wrote that part based off other examples I found on here. Any suggestions?
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
char showMenu();
void getLottoPicks(int[]);
void genWinNums(int[]);
bool noDuplicates(int[]);
const int SIZE = 7;
int main()
{
int userTicket[SIZE] = {0};
int winningNums[SIZE] = {0};
char choice;
string name;
srand(time(NULL));
do
{
choice = showMenu();
if (choice == '1')
{
cout << "Please enter your name: " << endl;
getline(cin, name);
getLottoPicks(userTicket);
genWinNums(winningNums);
for (int i = 0; i < SIZE; i++)
cout << winningNums[i];
}
} while (choice != 'Q' && choice != 'q');
system("PAUSE");
return 0;
}
Added the code for showMenu:
char showMenu()
{
char choice;
cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << "1) Play Lotto" << endl;
cout << "Q) Quit Program" << endl;
cout << "Please make a selection: " << endl;
cin >> choice;
return choice;
}
And getLottoPicks (this part is very wrong and I'm still working on it):
void getLottoPicks(int numbers[])
{
cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << "Selection #" << i + 1 << endl;
cin >> numbers[i];
if (numbers[i] < 1 || numbers[i] > 40)
{
cout << "Please choose a number between 1 and 40: " << endl;
cin >> numbers[i];
}
if (noDuplicates(numbers) == false)
{
do
{
cout << "You already picked this number. Please enter a different number: " << endl;
cin >> numbers[i];
noDuplicates(numbers);
} while (noDuplicates(numbers) == false);
}
}
}
After doing cin >> choice; inside char showMenu(), if a user inputs 1[ENTER], the char consumes 1 character from cin, and the newline stays inside the stream. Then, when the program gets to getline(cin, name);, it notices that there's still something inside cin, and reads it. It's a newline character, so getline gets it and returns. That's why the program is behaving the way it is.
In order to fix it - add cin.ignore(); inside char showMenu(), right after you read the input. cin.ignore() ignores the next character - in our case, the newline char.
And a word of advice - try not to mix getline with operator >>. They work in a slightly different way, and can get you into trouble! Or, at least remember to always ignore() after you get anything from std::cin. It may save you a lot of work.
This fixes the code:
char showMenu()
{
char choice;
cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << "1) Play Lotto" << endl;
cout << "Q) Quit Program" << endl;
cout << "Please make a selection: " << endl;
cin >> choice;
cin.ignore();
return choice;
}
from looking at code showMenu function has problem. and it's not returning asccii equivalent of '1' that is: 31 integer. try printing value returned by showmenu. you will get that
UPDATE:
It is because cin in delimited by ' '(whitespace) and getline by '\n' character, so when enter name and press enter cin in showmenu will consume whole string except '\n' from istream and that is read by getline. to see this when it ask for choice enter string like 1 myname (1 whitespace myname)and press ENTER will display name. now cin will read 1 in choice and myname in name by getline.