| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
i have a small problem with a program i am making for college.
i have made a program that will prompt for a password and check it and only let you continue if it is correct the problem is... ...i need to make it ask 3 times but only if the password is incorrect. if it is enterd correctly then it should continue i thort i may be able to achieve this be making a simple count but it still asks for my password again even if it is correct i have posted the code below if it helps! /////////////////////////////////////////// // Sam Horton - Group C // Password Protected Program - Assignment // Creates Date Tag /////////////////////////////////////////// #include <iostream.h> // include headers #include <stdlib.h> // include stdlib header to enable CLS function char password[5], name[50]; // declare variables of type char int day, month, date,year; // declare program variables - int void main() // start of program { cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n"; cout<<"Password Protected Program!\n"; cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n"; cout<<"Please enter your Name: "; cin>>name; // prompt for Name to be enterd system("CLS"); cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n"; cout<<"Password Protected Program!\n"; cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n"; cout<<"Welcome "; cout<<name; cout<<": \n"; cout<<"\nPlease enter the program Password (5 characters): "; cin>>password; // prompt for password if (password[0] == 'm' && password[1] == 'o' && password [2] == 'n' && password[3] == 'k' && password[4] == 'e' && password [5] == 'y') // check password is correct { system("CLS"); cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n"; cout<<"Loged In as: "; cout<<name; cout<<"\n+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n"; // tells user who is logged in cout<<"\nCorrect - Welcome to my program!!\n\n"; // Verifies correct password enterd and starts program ////////////////////////////////////////////////////// // INTERNAL PROGRAM STARTS HERE ////////////////////// cout<<"\nPlease enter the day of the month: "; cin>>day; // prompt for day of the month cout<<"\n\nPlease enter the month: "; cin>>month; // prompt for month cout<<"\n\nPlease enter the year: "; cin>>year; // prompt for year system("CLS"); cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n"; cout<<"Loged In as: "; cout<<name; cout<<"\n+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n"; // tells the user who is logged in if ( day > 31 ) { cout<<"Error!\n\n"; } else cout << "\n\nThe date is the "; switch (day) { case 1: case 21: case 31: cout << day << "st"; break; case 2: case 22: cout << day << "nd"; break; case 3: case 23: cout << day << "rd"; break; default: cout << day << "th"; }; // Outputs the day switch (month) { case 1: cout << " of "; cout << "January"; break; case 2: cout << " of "; cout << "February"; break; case 3: cout << " of "; cout << "March"; break; case 4: cout << " of "; cout << "April"; break; case 5: cout << " of "; cout << "May"; break; case 6: cout << " of "; cout << "June"; break; case 7: cout << " of "; cout << "July"; break; case 8: cout << " of "; cout << "August"; break; case 9: cout << " of "; cout << "September"; break; case 10: cout << " of "; cout << "October"; break; case 11: cout << " of "; cout << "November"; break; case 12: cout << " of "; cout << "December"; break; default: cout<<"an Invalid Month!\n\n"; // Outputs a month } cout<<" " << year <<"\n\n\n"; // Outputs the year // END OF INTERNAL PROGRAM /////////////////////////// ////////////////////////////////////////////////////// } else { system("CLS"); cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n"; cout<<"Password Protected Program!\n"; cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n"; cout<<"\n!ERROR!\n\n"; cout<<"Incorrect password - please restart program and try again!\n\n"; // tells user they have enterd an incorrect password } }; // end of program thanx - piepes |
|
#2
|
|||
|
|||
|
I was bored so I rewrote your program with password checking working. Hope this helps.
Code:
#include <iostream>
#include <cstdlib> // system(CLS)
#include <string>
using namespace std;
// Forward Declarations
bool IsCorrectPW( string password );
void RunInternalProgram( string name, string password );
// Main
void main()
{
string name, password;
cout << "+*+*+*+*+*+*+*+*+*+*+*+*+*+\n";
cout << "Password Protected Program!\n";
cout << "+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n";
// prompt for Name to be entered
cout << "Please enter your Name: ";
cin >> name;
// Welcome user
system( "CLS" );
cout << "+*+*+*+*+*+*+*+*+*+*+*+*+*+\n";
cout << "Password Protected Program!\n";
cout << "+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n";
cout << "Welcome ";
cout << name;
cout << ": \n";
// User gets 3 tries to enter a correct p/w
int remainingRetry(3);
while( remainingRetry > 0 )
{
// Prompt for password
cout << "\nPlease enter the program Password: ";
cin >> password;
// Check if p/w is correct
if( IsCorrectPW( password ) )
{
RunInternalProgram( name, password );
remainingRetry = 0;
}
else
{
system("CLS");
cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n";
cout<<"Password Protected Program!\n";
cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n";
cout<<"\n!ERROR!\n\n";
cout<<"Incorrect password - please try again!\n\n";
--remainingRetry;
if( remainingRetry == 0 )
{
cout << "Failed to enter correct password. Exiting program.\n\n";
}
}
}
}
// Check if the password is Monkey
bool IsCorrectPW( string password )
{
if( password == "Monkey" )
return true;
return false;
}
// Runs if the p/w is correct
void RunInternalProgram( string name, string password )
{
// Tells user who is logged in
system("CLS");
cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n";
cout<<"Loged In as: ";
cout<<name;
cout<<"\n+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n";
// Correct p/w mesaage
cout<<"\nCorrect - Welcome to my program!!\n\n";
int day, month, year;
// Prompt for day
cout<<"\nPlease enter the day of the month: ";
cin>>day;
// Prompt for month
cout<<"\n\nPlease enter the month: ";
cin>>month;
// Prompt for year
cout<<"\n\nPlease enter the year: ";
cin>>year;
// clear background
system("CLS");
cout<<"+*+*+*+*+*+*+*+*+*+*+*+*+*+\n";
cout<<"Loged In as: ";
cout<<name;
cout<<"\n+*+*+*+*+*+*+*+*+*+*+*+*+*+\n\n";
// Validate day
if ( day > 31 )
{
cout<<"Error!\n\n";
}
else
cout << "\n\nThe date is the ";
// Output day
switch( day )
{
case 1:
case 21:
case 31:
cout << day << "st";
break;
case 2:
case 22:
cout << day << "nd";
break;
case 3:
case 23:
cout << day << "rd";
break;
default:
cout << day << "th";
}
// Output month
switch (month)
{
case 1:
cout << " of ";
cout << "January";
break;
case 2:
cout << " of ";
cout << "February";
break;
case 3:
cout << " of ";
cout << "March";
break;
case 4:
cout << " of ";
cout << "April";
break;
case 5:
cout << " of ";
cout << "May";
break;
case 6:
cout << " of ";
cout << "June";
break;
case 7:
cout << " of ";
cout << "July";
break;
case 8:
cout << " of ";
cout << "August";
break;
case 9:
cout << " of ";
cout << "September";
break;
case 10:
cout << " of ";
cout << "October";
break;
case 11:
cout << " of ";
cout << "November";
break;
case 12:
cout << " of ";
cout << "December";
break;
default:
cout << "an Invalid Month!\n\n";
}
// Output year
cout << " " << year << "\n\n\n";
}
|
|
#3
|
|||
|
|||
|
cheers man.
thats spot on, now i have to actually make the internal program! lol i jus put in the date thing so i could see if the two functions worked properly |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Passwording Loop in Console - Microsoft |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|