Passwrd

i want to add a prompt at the start of my program which ask a user to enter its password
wat will be its syntax it is confusing me alot
i have written this code but its gving me a problem

int security(char *num)
{
char pass[10];
ifstream file("pass.txt", ios::in);
file>>pass;
if(pass==num)
cout<<"congrates valid pass\n";
else
{
cout<<"Try again\n";
exit(0);
}
}
void main()
{
char n[10];
cout<<"Enter Passward\n";
cin>>n;
security(n);

}

i am working with C++ and File handling
What is your problem?

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
#include <iostream>
#include <fstream>

using namespace std;

int security(char *num)
{
	char pass[10];
	ifstream file("pass.txt", ios::in);
	file>>pass;
	if(pass==num)
	cout<<"congrates valid pass\n";
	else
	{
	cout<<"Try again\n";
	exit(0);
	}
}

void main()
{
	char n[10];
	cout<<"Enter Passward\n";
	cin>>n;
	security(n);
}


(edit)
ohh I did not see that pass==num
this is wrong
Last edited on
No, it doesn't. This line: if(pass==num) doesn't do what you want: it checks if the memory address of pass is equal to that of of num. To check equality between the strings, use this:
if ( strcmp( num, pass ) == 0 )
http://www.cplusplus.com/reference/cstring/strcmp/
Last edited on
as pass and num are pointers, if (pass==num) checks if they both point to the same memory location - so it doesn't actually tell you anything about the strings involved - not even the first element.

by the way, i'd at least suggest encrypting your password file - otherwise it is edittable to all and sundry.
Topic archived. No new replies allowed.