Password Based Function

Nov 4, 2015 at 1:16pm
Is this ok for a function based on changing the variables with the requirement of passwords ?, if not please say how ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
float dc[5]={3,3.25,3.5,4.6,6.6};
float df[3]={30,40,50};
float cf=120;

void getslab()
{
	char ch;int count=0;
	char user[8],pass[8];
	strcpy(user,"Admin@00");
	strcpy(pass,"Access00");
	cout<<"\nDo you wish to change the Consumption rates ?(Y/N) : ";
	cin>>ch;
	if((ch=='Y')||(ch=='y'))
	{
		cout"\nWarning ! , This will change all the bill rates !\n\tThis cannot be undone ,";
		cout<<"\nAnd You must take Full responsibility if the System crashes !";
		cout<<"\nDo you really want to change the rates ?(Y/N) : ";
		cin>>ch;
		if((ch=='Y')||(ch=='y'))
		{
			if(count>=3)
			{
				cout<<"\nTrials Exceeded !!!";
				cout<<"\nExitting Program !!!";
				exit(1);
			}
			again:
			clrscr();
			cout<<"\nAdmin Login";
			cout<<"\n----- -----\n\n";
			cout<<"\nAdmin Name : ";
			cin>>user;
			cout<<"\nPassphrase : ";
			cin>>pass;
			if((strcmp(user,"Admin@00"))&&(strcmp(pass,"Access00")))
			{
											//change rates
			}
			else
			{
				cout<<"\nTry Again !!!";
				count++;
				goto again;
			}
		}
		else
		{
			cout<<"\nThe Rates are as Before !";
		}
	}
	else
	{
		cout<<"\nThe Rates are as Before !";
	}
}
Last edited on Dec 29, 2015 at 12:16pm
Nov 4, 2015 at 4:12pm
You need to take another look at the documentation for strcmp, to see what the return value actually means. What you've written right now won't do what you want it to do.

As you would have known, if you'd taken two minutes to run and test it.
Nov 6, 2015 at 2:29pm
Got What You Meant !


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
float dc[5]={3,3.25,3.5,4.6,6.6};
float df[3]={30,40,50};
float cf=120;

void getslab()
{
	char ch;int count=0;
	char user[8],pass[8];
	strcpy(user,"Admin@00");
	strcpy(pass,"Access00");
	cout<<"\nDo you wish to change the Consumption rates ?(Y/N) : ";
	cin>>ch;
	if((ch=='Y')||(ch=='y'))
	{
		cout"\nWarning ! , This will change all the bill rates !\n\tThis cannot be undone ,";
		cout<<"\nAnd You must take Full responsibility if the System crashes !";
		cout<<"\nDo you really want to change the rates ?(Y/N) : ";
		cin>>ch;
		if((ch=='Y')||(ch=='y'))
		{
			again:
                        if(count>=3)
			{
				cout<<"\nTrials Exceeded !!!";
				cout<<"\nExitting Program !!!";
				exit(1);
			}
			clrscr();
			cout<<"\nAdmin Login";
			cout<<"\n----- -----\n\n";
			cout<<"\nAdmin Name : ";
			cin>>user;
			cout<<"\nPassphrase : ";
			cin>>pass;
			if((strcmp(user,"Admin@00/0")==0)&&(strcmp(pass,"Access00/0")==0))
			{
											//change rates
			}
			else
			{
				cout<<"\nTry Again !!!";
				count++;
				goto again;
			}
		}
		else
		{
			cout<<"\nThe Rates are as Before !";
		}
	}
	else
	{
		cout<<"\nThe Rates are as Before !";
	}
}


What about now ?
Last edited on Nov 17, 2015 at 3:42pm
Nov 6, 2015 at 2:31pm
password base function is excellent , these are no hack
Nov 6, 2015 at 2:38pm
what do you mean by saying
ahmadabbas wrote:
these are no hack
Nov 6, 2015 at 3:26pm
> strcpy(user,"Admin@00");
out of bounds (don't forget the '\0' terminator)
Nov 17, 2015 at 3:43pm
But there is no requirement to use '/0' when we use strings comparison isnt it ?
Nov 17, 2015 at 4:08pm
But there is no requirement to use '/0' when we use strings comparison isnt it ?
It is required. Basically it's essential. Actually it is '\0' (Note: backslash)
Nov 17, 2015 at 4:56pm
It seems that you misunderstood.
`user' has to have enough space to hold each character of the "Admin@00" string and the null terminator.
{'A', 'd', 'm', 'i', 'n', '@', '0', '0', '\0'}

right now you have char user[8], so it may only store seven characters and the null terminator, but you need to store eight characters.

You need to make `user' bigger.
Dec 29, 2015 at 12:07pm
Thanks a Lot !
ne555 wrote:
It seems that you misunderstood.
`user' has to have enough space to hold each character of the "Admin@00" string and the null terminator.
{'A', 'd', 'm', 'i', 'n', '@', '0', '0', '\0'}

right now you have char user[8], so it may only store seven characters and the null terminator, but you need to store eight characters.

You need to make `user' bigger.


I got it !
Topic archived. No new replies allowed.