Password Based Function

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
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.
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
password base function is excellent , these are no hack
what do you mean by saying
ahmadabbas wrote:
these are no hack
> strcpy(user,"Admin@00");
out of bounds (don't forget the '\0' terminator)
But there is no requirement to use '/0' when we use strings comparison isnt it ?
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)
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.
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.