C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 6th, 2004, 02:36 PM
piepes piepes is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 7 piepes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Passwording Loop in Console - Microsoft

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

Reply With Quote
  #2  
Old December 6th, 2004, 09:01 PM
marmo marmo is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 37 marmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 20 sec
Reputation Power: 5
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";
 }
 

Reply With Quote
  #3  
Old December 7th, 2004, 05:59 PM
piepes piepes is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 7 piepes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Passwording Loop in Console - Microsoft


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT