Hey guys I've been working on C++ for just a couple months now and i want to create a program that asks the user for a password, i will set that password as 1234. After the user will input the password and if he enter it will open up a siwtch statement with 4 options, thats the easy part. But I'm having trouble with the other part, after i have inputed the wrong password 5 times i want it to say "Sorry the system has been locked until tomorrow" and then i want it to close, but the thing is that after i get the password wrong 5 times it says "Sorry..." but then it brings me to the switch statement as if i had gotten the password right. Please help me guys im desperate right now, i don't know if i'm just positioning things wrong or something but i'm using microsoft visual studio 2010. Here's my code sorry for the long topic and also feel free to use this code too :)
#include "stdafx.h"
#include <iostream>
#include <process.h>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
int password = 1234;
int trials = 1;
int input = 0;
do
{
cout << "Please enter your password for this account: ";
cin >> input;
if (input == password)
{
system("cls");
cout << "Welcome back user" << endl;
break;
}
trials++;
if (trials > 5)
{
cout << "You have exceeded the amount of attempts for "
<< "entering your password." << endl;
cout<< "The system has locked until tomorrow. \n\n\n";
you are confusing the assignment operator with the equals operator. The two are different:
= assigns the right hand value to the variable.
== checks if the right hand value is equivelant to the left hand value.
In your statement above, you are continuously assigning the value of false to the variable exitProgram, and when assigning a value, it always returns true.
Okay so I I fixed the == but now when I run the program after I input the wrong password 5 times the password(); starts over and will ask for the pass word again. I think this may have something to do with the fact of the switch statement follwing the code. Should i put that switch statment in a function somehow?
Hey you guys i just had an idea what if for passwordEntry(); instead of using a do while statement, i do a for loop statement that way i can make it so it performs the task of passwordEntry(); only 5 times will this work or am i way off
nevermind for loop was a stupid idea, guys things in the main are going in order of how things are written i honestly think that its how i arranged my things in the main, or maybe i have to put my switch statement in a function guys please help i can't figure this out
bool exitProgram = false;
do
{
passwordEntry();
}
while (exitProgram == false);
How does exitProgram ever get set true by passwordEntry() ?
You want passwordEntry to provide some kind of indication upon return that the password entry succeeded or failed. if passwordEntry returns a failure indication, you want to exit the program immediately, not fall through to the following do while loop which will execute at least once.
Also, since password entry is responsible for counting attempts, there is no reason for the loop around the call to it.
1 2
if (! passwordEntry())
return 0;
This assumes passwordEntry returns 0 (or false) if the password attempt fails and 1 (or true) if it succeeds.
Huh? Look at lines 20-25 (lines 1-8 in my post above).
At no point is exitProgram changed. That loop will execute forever.
I already told you what you need to do. password_entry needs to return a pass or fail indication (a bool return value). Get rid of the do/while loop. It's not needed. Test the result of passwordEntry using the if statement I showed you and return out of main immediately if the password check has failed 5 times.
By doing it AbstractionAnons way, your loop terminates. Doing it any other way causes the system to keep repeating itself. Just do what he asks you too.