Hello everybody!

I am creating a Book library system in C++.

I created a login class where the user types in a username and password and if successful is able to log into the menu system.

Now, everything goes well unless and untill the user types in the right credentials but things fall apart when the user is given three chances if the username and password is not right. After three chances the console closes down.

But in my case after three failed attempts the user is able to access the menu system anyhow regardless of the user credentials???

please check my code and advise me accordingly

#include <include.h>
#include <exit.h>

void login()
{
	string username; //This is a string variable calld username(string variables r 4 saving text)
	string password; //this is a string variable calld password
	int tries = 3;       //This is a integer variable tries.Here we save the number of tries to ques the
	int success =0; // false

	//username and password(integer = 4 saving numbers not decimal numbers).And we set it on 0

	while(tries && !success)
	{
		cout << "Username: "; //displays Username: on the screen
		cin >> username; //Saves the input in the variable username
		cout << "Password: ";
		cin >> password;

		if(username == "admin" && password == "getaccess") //if username is the same as Admin and password is the
		//same as GetAcces then:
		{
			system("cls"); //clear screen
			cout << "You have got access. \n";
			success =1;
		}
		else //else:
		{
			tries--; //tries + 1
		}
				
	}
}

This class gives access to the main menu

#include "include.h"
#include "menu.h"
#include "exit.h"
#include "login.h"

int main()
{	
	
	login();
	
	printMenu();
			
}

you should let execute printMenu() call if only success. If not just print a "Invalid user " message.

you can change your method

void login()

to

int login();

say we are returning zero if login success -1 if otherwise. ( most of the Functions are implemented like that).

and using a simple if check you can allow it to print the menu or disallow.

you should let execute printMenu() call if only success. If not just print a "Invalid user " message.

you can change your method

void login()

to

int login();

say we are returning zero if login success -1 if otherwise. ( most of the Functions are implemented like that).

and using a simple if check you can allow it to print the menu or disallow.

Yeah thats what I had in mind.....

Could give me an example.....please as I am still a newbie.

Thanks

Okay after what you said.....

I made these changes to my code:
But if the login credentials are right it doesn't get into the menu??

#include "include.h"
#include "menu.h"
#include "exit.h"
#include "login.h"

int main()
{	
	int success =0;
	
	login();
	
	if (success=0)
	{
		cout << "Invalid user \n";
	}
	else
	{
		printMenu();
	}
			
}

you should return 0 if success otherwise return -1

#include <include.h>
#include <exit.h>

int  login()
{
	string username; //This is a string variable calld username(string variables r 4 saving text)
	string password; //this is a string variable calld password
	int tries = 3;       //This is a integer variable tries.Here we save the number of tries to ques the
	int success =0; // false

	//username and password(integer = 4 saving numbers not decimal numbers).And we set it on 0

	while(tries && !success)
	{
		cout << "Username: "; //displays Username: on the screen
		cin >> username; //Saves the input in the variable username
		cout << "Password: ";
		cin >> password;

		if(username == "admin" && password == "getaccess") //if username is the same as Admin and password is the
		//same as GetAcces then:
		{
			system("cls"); //clear screen
			cout << "You have got access. \n";
			//success =1;
                         return 0;
		}
		else //else:
		{
			tries--; //tries + 1
		}
				
	}
        return -1;
}

Oky now in your main code try to implement this . Using a "if" check
for the return value. if it 0 then allow if not disallow.
Try to implement that part you're alone . Give it a try. and reply if you failled ! Pll here to help you. But you should show a little bit encourage.

sorry yes I should have tried but did just before you posted......

let me try your code......

cheers

#include "include.h"
#include "menu.h"
#include "exit.h"
#include "login.h"

int main()
{	
	int success =0;
	
	login();
	
	if (success=0)
	{
		cout << "Invalid user \n";
	}
	else
	{
		printMenu();
	}
			
}

Oky the 'int succes ' inside the login() and' int success' inside main is
totally different. You should read something about C++ variable scopes . here is a good article.
http://www.cplusplus.com/doc/tutorial/variables.html

start reading at scope of variables.

sorry yes I should have tried but did just before you posted......

let me try your code......

cheers

It's oky , try to get the value as a return value. Or there are other ways.
But for now ( for easy underestand ) try it using a return value.

It's oky , try to get the value as a return value. Or there are other ways.
But for now ( for easy underestand ) try it using a return value.

Okay I understand your return value in the login page.......

But I am confused about implementing it in 'if else' on my main page.......?

Well bro problem resolved......tried a couple different things by taking your pointers....

here is the code:

#include "include.h"
#include "exit.h"

int login()
{
	string username; //This is a string variable calld username(string variables r 4 saving text)
	string password; //this is a string variable calld password
	int tries = 3;       //This is a integer variable tries.Here we save the number of tries to ques the
	int success =0; // false

	//username and password(integer = 4 saving numbers not decimal numbers).And we set it on 0

	while(tries && !success)
	{
		cout << "Username: "; //displays Username: on the screen
		cin >> username; //Saves the input in the variable username
		cout << "Password: ";
		cin >> password;

		if(username == "admin" && password == "getaccess") //if username is the same as Admin and password is the
		//same as GetAcces then:
		{
			system("cls"); //clear screen
			cout << "You have got access. \n";
			//success =1;
			printMenu();
			system("pause");
			return 0;
			
		}
		else //else:
		{
			tries--; //tries + 1
		}
				
	}
	return -1;
}
#include "include.h"
#include "menu.h"
#include "exit.h"
#include "login.h"

int main()
{	
	int success=0;
	login();
	
	if (success = 0)
	{
		return -1;
	}
	else 
	{
		
		return 0;
		
	}
		
}

Its minor but it bugs me, change success from an into to a bool type variable and toggle it true if the password is correct, that way you can just write

if(login())
PrintMenu();

else
return 0;

Its minor but it bugs me, change success from an into to a bool type variable and toggle it true if the password is correct, that way you can just write

if(login())
PrintMenu();

else
return 0;

No problem bruv....Thanks for the advice......:)

Its minor but it bugs me, change success from an into to a bool type variable and toggle it true if the password is correct, that way you can just write

if(login())
PrintMenu();

else
return 0;

Bro Iam sure your right but checked your code and it still gives access to the menu even if the user has entered the wrong credentials three times.

This question has already been answered. Start a new discussion instead.