Please tell me what's wrong with the code.

The program would stop at after one of the statements. There's no error shown by the compiler.

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


//----------------------------------------------------------------------
//-----------------------------Login------------------------------------
//----------------------------------------------------------------------

int Login(){
clrscr();

//Variables
char newpass[20];
char checkpass[20];
int cnt=-1;



cout<<"\nLogin Menu -> Login";  //  <-----Last statement  that gets executed.


ifstream ReadFile("pass",ios::in);//DECLARE INPUT STREAM

while(!ReadFile)
			{
			cnt++;
			}

ReadFile.close();

if(cnt==-1){//Create A Password, if it doesn't exist yet.
ofstream WriteToFile("pass",ios::out);
cout<<"You haven't set the password yet. Please set it now.";
cout<<"Enter a New Password (Maximum of 20 characters)";
gets(newpass);
	for(int i=0;i!='\0';i++)
				{
				WriteToFile<<newpass[i];
				}
WriteToFile.close();
return 1;
}//PASSWORD CREATED!

ReadFile.open("pass",ios::in);//READ FILE

cout<<"Enter Password:\t";
gets(newpass);

ReadFile.get(checkpass,20,'\n');
ReadFile.close();//CLOSE STREAM

if(strcmp(checkpass,newpass)==0)
return 1;
else
return 0;

}
I can't even tell what is happening here. Is this function inside of another? It looks like it is.
I'm sorry for not providing enough details.

(The code isn't complex as it's for my school project.)


It is a function that is used for entering the password to login. If the password has been created, the user enters it, else they are prompted to create a password.

1
2
3
4
while(!ReadFile)
			{
			cnt++;
			}



It's used to find the length of the file "Pass". If it contains any characters, the cnt gets updated.




1
2
3
4
5
6
7
8
9
10
11
12
if(cnt==-1){//Create A Password, if it doesn't exist yet.
ofstream WriteToFile("pass",ios::out);
cout<<"You haven't set the password yet. Please set it now.";
cout<<"Enter a New Password (Maximum of 20 characters)";
gets(newpass);
	for(int i=0;i!='\0';i++)
				{
				WriteToFile<<newpass[i];
				}
WriteToFile.close();
return 1;
}//PASSWORD CREATED! 


If cnt wasn't updated, User is prompted to create the password. The function then returns 1.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
ReadFile.open("pass",ios::in);//READ FILE

cout<<"Enter Password:\t";
gets(newpass);

ReadFile.get(checkpass,20,'\n');
ReadFile.close();//CLOSE STREAM

if(strcmp(checkpass,newpass)==0)
return 1;
else
return 0;

}


If the cnt was updated (means that password has been created before), the user is prompted to enter it. If it matches with the password saved in the file, the function returns 1, else 0.
Last edited on
Another thing to mention is that, line 17 is the last one that gets executed (Last output).
Topic archived. No new replies allowed.