HELP

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
56
57
58
59
60
// Password Input

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string.h>
#include <string>

using namespace std;

int main ()
{
	char ch[9];
	char mypass[5]="pass";
	int y;
	char upass[]="     ";
	
	cout<<"Enter PIN: ";
	 
	 
	 
	 
	 
	
	do{
		ch[0]=getch();
		
		if(ch[0]==13 || y==7) //enter is the 13 scan code
		{
			upass[y]='\0'; //null char, end of your upass string
			break;
		}
		else if(ch[0]==8 && y!=0) //scancode 8 is backspace
		{
			y--;
			cout<<"\b";
			cout<<" ";
			cout<<"\b";
		}
		else
		{
			y++;
			upass[y]=ch[0]; //assign the value 'ch' as your password
			cout<<"*";
			 
		}
}
	while(1);
	
	if(strcmp(mypass,upass)==0){
	
	
		cout<<"\n Granted";
	
}
	else
	{
		cout<<"\n Denied";
	}
}

Help I set a password but when I typed it it always showing "denied" although the password that I set is correct with what I've typed
y is uninitialized and contains an invalid (maybe negative) value.
On what number line please be specific
What is the password?
On what number line please be specific


It's never given a value. Line 28 is when you first try to compare it.
the password is "pass"
what seems to be the problem on that line
line 15: int y = 0;
nothing change!!!
@vendetta2014

Your main problem was that you were entering chars, but checking for int. This way works..

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

// Password Input

#include <iostream>
#include <windows.h>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main ()
{
 char ch[5];
 char mypass[5]="pass";
 int y=0;
 char upass[5];
 cout<<"Enter PASSWORD: ";

 do{

	ch[y]=_getch();

	if(ch[y]=='\x0D' || y==4) //enter is the 13 scan code
	{
	 upass[y]='\0'; //null char, end of your upass string
	 break;
	}
	if(ch[y]=='\x08' && y!=0) //scancode 8 is backspace
	{
	 y--;
	 cout<<"\b \b";
	}
	else
	{
	 upass[y]=ch[y]; //assign the value 'ch' as your password
	 cout<<"*";
	 y++; // Increase AFTER assigning
	}
 }	while(1);

 if(strcmp(mypass,upass)==0)
	cout<<"\n Granted" << endl;
 else
	cout<<"\n Denied" << endl;
}
Last edited on
to make that better you could also use
using namespace std;
to make that better you could also use
using namespace std;
That would actually make it worse, since it can lead to possible naming conflicts.
oh... ok
line 28, you assined y=0 but checking it for 7 at first do while no input was given to y, btw if u wana have password, save some password/s in file in bin form then write to char and compare with user input..more security
there are no naming conflicts in the program vendetta2014 showed us so vendetta2014 could use
using namespace std;
If they wanted.
"There are no conflicts" and "it won't lead to conflicts" are not the same. using namespace is discouraged because the latter is not true.
Last edited on
Just because something is possible, doesn't make it good practice.
I use codeblocks and It is the first thing the program puts in. It works for me.
That may be true, but that does not invalidate the fact that it can lead to naming conflicts.

What happens if you have a variable named count and you try and store the value of the number of 1's in an array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>

using namespace std;

//program to count the number of 1's in an array
int main()
{
    int count = 0;
    size_t const size = 10; //size_t = std::size_t
    int array[size] = {1, 2, 3, 4, 1, 1, 7, 8 ,1, 1};
    
    count = count(array, array+size, 1); //error: can't use count as a function
    //the working solution would be 1) remove the namespace
    //or 2) add :: before the function
    //http://www.cplusplus.com/reference/algorithm/count/
    
    std::cout << count << std::endl;
}


Here are more examples
http://stackoverflow.com/a/1265092
Last edited on
Topic archived. No new replies allowed.