Jump to content

how to set a user login with password?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Peter

Peter

    Newbie

  • Members
  • Pip
  • 6 posts
Hi All,
i need some help on how do i set a user login with password before it go to my main menu?

Here are my code,hope that someone can teach me :(

#include <iostream>
using namespace std;

int  main()
{
	char choice;

	cout << "Enter 1, 2, 3 or 4: ";
	cin  >> choice;

	switch (choice)
	{
	case '1': cout << "You have entered Cashier Module.\n";
				  {
	// Call displayValue with argument 5
	cout << "Now I am back in main.\n";
				  }

					 break;
	case '2': cout << "You have entered Inventory Database Module.\n";
					break;
	case '3': cout << "You have entered Report Module.\n";
				      break;
             case '4': cout << "You have entered Exit.\n";
			            break;
             default:  cout << "You did not enter 1, 2, 3, or 4!\n";
	}
	return 0;
}


#2
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
you have not made any provisions for entering passwords or for checking them!take a character array-1d and scanf or gets() the password.
eg :
char pass[10];
gets(pass);

now cross check this with a password saved in your program.if you are willing to use this for long terms, implement careful file handling.

#3
Peter

Peter

    Newbie

  • Members
  • Pip
  • 6 posts
thank you but,how do i go about it? in C++
#include <iostream>
    char password[10];
    char password1[10];
    char userid[10];
    char userid1[10];
    void mainmenu(int num);

    using namespace std;

int main(int num)
{
   if userid = userid1 then
   mainmenu(0);
   else
   main(0);
}
void mainmenu(int num);
{
char choice;

cout << "Enter 1, 2, 3 or 4: ";
cin >> choice;

switch (choice)
{
case '1': cout << "You have entered Cashier Module.\n";
{
// Call displayValue with argument 5
cout << "Now I am back in main.\n";
}

break;
case '2': cout << "You have entered Inventory Database Module.\n";
break;
case '3': cout << "You have entered Report Module.\n";
break;
case '4': cout << "You have entered Exit.\n";
break;
default: cout << "You did not enter 1, 2, 3, or 4!\n";
}
return 0;
}


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Peter, first of all, please use code blocks when entering code (it preserves formatting).

Second: you get a username/password by asking for a username/password. Then you check the values entered against the correct values. The rest of the code is wrapped in an if statement, only displayed if the username/password were correct.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Peter

Peter

    Newbie

  • Members
  • Pip
  • 6 posts
Thank you but how do i go about it? i am so new in vc++.
can you show me some code ?

#6
Peter

Peter

    Newbie

  • Members
  • Pip
  • 6 posts
i try the things that you say but still no display out the mainmenu.
can u teach me which parts of my code is wrong?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <ctime>
#include <cmath>
using namespace std;

void mainmenu(int num);

int main(int num)
	{
	  // User has three trys to login with the correct password until they are shut off
	int wrongpasswordCount = 3;
          // User has five trys to login with a correct username and password until they are shut off
	int wrongsusernamepasswordCount = 5;
	bool loopStatus = true;	
	while (wrongpasswordCount != 0 && wrongsusernamepasswordCount != 0 && loopStatus == true)
	{
		cout << "Username: ";
		string username;
		cin >> username;
		cout << "Password: ";
		string password;
		cin >> password;
		
			string correctusername ="Jacky";
			string correctpassword ="12345";
				

		if (username==correctusername && password==correctpassword) 
		  {	
			loopStatus = false;
		  	cout << "\nWelcome, " << username << "! ";

			bool loopCommand  = true;
			while (loopCommand  == true)
			{
				mainmenu(0);
			} //end bool loopCommand while
			
		}	
		else 
			{ 
				
				if (username==correctusername && password!=correctpassword)
					{ wrongpasswordCount -= 1;
					cout<<"Password is incorrect, " << wrongpasswordCount << " attempts left.\n"; }

				else {  wrongsusernamepasswordCount -= 1;
					cout << "Username and/or password are incorect, "<< wrongsusernamepasswordCount <<" attempts left.\n"; }
				
			}		
	}	
	return 0; 
}

void mainmenu(int num);
{
char choice;

cout << "Enter 1, 2, 3 or 4: ";
cin >> choice;

switch (choice)
{
case '1': cout << "You have entered Cashier Module.\n";
{
// Call displayValue with argument 5
cout << "Now I am back in main.\n";
}

break;
case '2': cout << "You have entered Inventory Database Module.\n";
break;
case '3': cout << "You have entered Report Module.\n";
break;
case '4': cout << "You have entered Exit.\n";
break;
default: cout << "You did not enter 1, 2, 3, or 4!\n";
}
return 0;
}


#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First: what do you expect it to do?
Second: what is it doing?

A quick glance suggests you're on the right track.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
Peter

Peter

    Newbie

  • Members
  • Pip
  • 6 posts
Firstly as a user to login then if the password n userid is right it will run and display the mainmenu. But it cant display the mainmenu.
I only got 1 day to do this program for my school project assignment. Died end assignment on 11/03/08.
Someone help me pls!
I really need help. :(

#9
Babbage

Babbage

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi Peter:

From the code you posted here on the 7th, I found a couple things wrong with your mainmenu function. I have pointed them out here (see capital comments):


void mainmenu(int num); //SEMI-COLAN SHOULD NOT BE HERE

{

char choice;


cout << "Enter 1, 2, 3 or 4: ";

cin >> choice;


switch (choice)

{

case '1': cout << "You have entered Cashier Module.\n";

{

// Call displayValue with argument 5

cout << "Now I am back in main.\n";

}


break;

case '2': cout << "You have entered Inventory Database Module.\n";

break;

case '3': cout << "You have entered Report Module.\n";

break;

case '4': cout << "You have entered Exit.\n";

break;

default: cout << "You did not enter 1, 2, 3, or 4!\n";

}

return 0; //SHOULD NOT RETURN AN INT. SHOULD EITHER SAY 'return;' OR SHOULD BE REMOVED

}

You can see here, that the problem lies with the first line ( void mainmenu(int num); ), and the second to last line ( return 0; ). The problem exists with the first line, because you are defining the function, but cutting it off immediately (the semi-colan does this). This is an honest mistake, however - you probably cut and pasted the prototype from the top of the program, but forgot to remove the semi-colan :o

A problem exists with the second to last line, because you are trying to return an int, when it should be returning nothing (void). So, to fix this, you could completely remove this line or change it to simply say: return;

Other than that, I believe everything else looked fine. My computer was having a hissy fit with the '#include "stdafx.h"' include at the top, but i was able to omit that and still get the program to compile fine.

Let me know if these fixes work for you. Good luck with your class!

-Babbage

#10
Peter

Peter

    Newbie

  • Members
  • Pip
  • 6 posts
Thank you but stil the main menu cant display out.

#11
Babbage

Babbage

    Newbie

  • Members
  • PipPip
  • 11 posts

Quote

Thank you but stil the main menu cant display out.

Were you able to compile it successfully? Can you post your code now, after your modifications, so we can take a look at it?

When I try it on my computer (after making the changes I had suggested to you), I am able to compile it here.

-Babbage