Password Program

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;

void passwordEntry();
void history();
void deposit();
void withdraw();
bool quit();

int main()
{
bool exitProgram = false;


do
{
passwordEntry();

}
while (exitProgram = false);

do
{
system("cls");
cout << "Welcome back user" << endl << endl;
cout << "1. View account history" << endl;
cout << "2. Make a deposit" << endl;
cout << "3. Make a withdrawl" << endl;
cout << "4. Quit\n\n" << endl;
cout << "Select 1, 2, 3, or 4";

int choice = 0;
cin >> choice;

switch(choice)
{
case 1:
history();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
exitProgram = quit();
break;
default:
cout << "Invalid entry" << endl;
}

}

while (exitProgram == false);


system("pause");
return 0;
}
void passwordEntry()
{

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";

break;
}
cout << "Invalid password, try again.\n";

}
while (true);
system("pause");

}
void history()
{
system("cls");
cout << "Beginning Balance: $1,445.45" << endl;
cout << "Total deposits: $1000.00" << endl;
cout << "Total withdrawls: $45.45" << endl;
cout << "Ending balance: $2,400.00\n\n\n" << endl;

system("pause");
}
void deposit()
{
system("cls");
double depositedMoney;
double newBalance;
cout << "How much do you wish to deposit?" << endl;
cout << "$ ";
cin >> depositedMoney;

newBalance = depositedMoney + 2400.00;

cout.setf(ios::fixed);//code for 2 decimal places
cout.setf(ios::showpoint);
cout.precision(2);

cout << "Your new balance is $" << newBalance << ".\n\n\n";

system("pause");
}
void withdraw()
{
system("cls");
double withdrawAmount;
double newBalance;

cout << "How much money would you like to withdraw from your account?"<< endl;
cout << "$ ";
cin >> withdrawAmount;

newBalance = 2400.00 - withdrawAmount;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout << "Your new balance is $" << newBalance << ".\n\n\n";
system("pause");
}
bool quit()
{
char response;
bool returnValue = false;
do
{
system ("cls");
cout << "Do you really want to quit (y/n) " << endl <<endl;

cin >> response;
if ( (response == 'y') || (response == 'Y'))
{
returnValue = true;
break;
}
else if (( response == 'n') || (response == 'N'))
{
returnValue = false;
break;
}
}
while (true);
return returnValue;
}
while (exitProgram = false);

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.
Thank you for taking time to respond but what does this mean what do I need to change?
The following statement at line number ??? is what pogrady is referring to.
 
while (exitProgram = false);

That is an assignment statement, not a conditional test.

The statement should be:
 
while (exitProgram == false); 


I can't refer you to the line number because you didn't use code tags when posting your code. Hint: It's right after your call to passwordEntry().

PLEASE USE CODE TAGS (the <> formatting button) when posting code.

Last edited on
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
Consider your initial loop.
1
2
3
4
5
6
7
8
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.
when passwordEntry(); is being defined at the end of the do while i set exitProgram to true but what should i do abstractionanon
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.
Topic archived. No new replies allowed.