i don't understand this program

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
void change()
{	int c;
	char tmp[30];
	clrscr();
	cout<<"\nENTER CURRENT PASSWORD:";
	int t=0;
	char ch;
	while(ch!=13)
	{	ch=getch();
		if(ch==8)
		{	clrscr();
			cout<<"\nENTER CURRENT PASSWORD:";
			t=t-1;
			for(int i=0;i<t;i++)
			{	cout<<"*";
			}
			tmp[t]='\0';
		}
		else
		{	tmp[t]=ch;
			cout<<"*";
			t++;
		}
	}
	tmp[t-1]='\0';

	ifstream fin("pass.dat",ios::binary);
	fin.getline(pass,30);
	fin.close();
	if(strcmp(pass,tmp)==0)
	{	t=0;
		clrscr();
		cout<<"\nENTER NEW PASSWORD:";

		ch=0;
		while(ch!=13)
		{	ch=getch();
			if(ch==8)
			{	clrscr();
				cout<<"\nENTER NEW PASSWORD:";
				t=t-1;
				for(int i=0;i<t;i++)
				{	cout<<"*";
				}
				tmp[t]='\0';
			}
			else
			{	tmp[t]=ch;
				cout<<"*";
				t++;
			}
		}
		tmp[t-1]='\0';

		strcpy(pass,tmp);
		int l=strlen(pass);
		ofstream fout("pass.dat",ios::binary|ios::trunc);
		for(int i=0;i<=l;i++)
		{	fout.put(pass[i]);
		}
		fout.close();
		cout<<"\n!!PASSWORD CHANGED SUCCESSFULLY!!";
		getche();
		clrscr();
		login_menu();
	}
	else
	{	selection2:
		clrscr();
		cout<<"\nWRONG CURRENT PASSWORD";
		cout<<"\nWANT TO TRY AGAIN?\n";
		cout<<"\n1.YES\n";
		cout<<"\n2.NO \n";
		cout<<"\nENTER YOUR CHOICE:";
		cin>>c;
		if(c==1)
		{	clrscr();
			change();
		}
		else if(c==2)
		{	clrscr();
			login_menu();
		}
		else
		{
			clrscr();
			cout<<"\n\n\n\t\t\tINVALID CHOICE...ENTER A VALID ONE!!";
			getche();
			clrscr();
			goto selection2;
		}
	}
}




please give suggestions as per turbo c++ and not visual c++ plzzz!!!!
Last edited on
What exactly do you not understand? Also, this program is using bad programming habits, like using goto.
Also, this program is using bad programming habits, like using goto.

And - even worse - it's obscuring the flow of control by hiding the goto labels at the ends of lines.

Whoever wrote this code needs to be exiled to a place with no electricity, to ensure they don't write any more... :P
please tell me what is the use of while(ch!=13)
It means that the while loop will continue until the integer value of ch is 13.

If you look at line 9, you can see that the value of ch is set to the return value of the function getch(). I believe this is a 3rd-party function which prompts the user to enter a character with the keyboard, and returns the value of the character.

So, the loop repeats until the user enters a character whose value is 13.

If you look at an ASCII value table (e.g. http://www.asciitable.com/) you'll see that 13 corresponds to a carriage return. So, basically, this loop continues until the user hits Enter.
Isn't the clrscr() also bad coding habits since its slower and a security risk, or does that only apply to system("cls")? Also in place of the clear screen you would use some form of output manipulation? Just want to know since I am a beginner and trying to learn to iron out bad habits.
Last edited on
@gobiking clrscr() is part of the non-standard <conio.h> library.
Its use is discouraged as it isn't available in all compilers, and even those which have a conio.h library may not offer the same functions, or they may not work in the same way.

In other respects it is ok, it is fast and there are no security risks. The downside is that if you share your code with others, it may not work, or if you upgrade to a different compiler, it may not work.

There are alternatives, such as using the Winapi functions, or a library such as ncurses.
Topic archived. No new replies allowed.