Date changes

Pages: 12
I have created a program to create ID.The input of date works fine.So is output till shown.(means correct date showingtill that),but after that(shown in program i.e. if condition) the date changes to 0.Please help.(small "id" means a->admin g->guest)

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
char User::Input()
{
	clrscr();
	char ch='n';
	gotoxy(10,30);
	cout<<"Note:";
	gotoxy(15,2);
	cout<<"Enter details for ID";
	gotoxy(5,5);
	cout<<"First Name(include Middle name):";
	gets(FName);
	gotoxy(5,6);
	cout<<"Last Name:";
	gets(LName);
	gotoxy(5,7);
	cout<<"Date of Birth:";
	gotoxy(7,8);
	cout<<"Date(dd):";
	cin>>dd;          //This having problem
	gotoxy(7,9);
	cout<<"Month(mm):";
	cin>>mm;
	gotoxy(7,10);
	cout<<"Year(yyyy):";
	cin>>yyyy;
	while(dd<0 || dd>31 || (mm%2==0 && dd==31) || (mm==2 && yyyy%4!=0 && dd>28) || (mm==2 && yyyy%4==0 && dd>29))
	{
		gotoxy(15,30);
		cout<<"Incorrect Data!Re-enter Date, Month and Year";
		gotoxy(7,8);
		clreol();
		gotoxy(7,9);
		clreol();
		gotoxy(7,10);
		clreol();
		gotoxy(7,8);
		cout<<"Date(dd):";
		cin>>dd;
		gotoxy(7,9);
		cout<<"Month(mm):";
		cin>>mm;
		gotoxy(7,10);
		cout<<"Year(yyyy):";
		cin>>yyyy;
		gotoxy(15,30);
		clreol();
	}
	gotoxy(5,11);
	cout<<"Enter Address:";
	gotoxy(7,12);
	cout<<"Line 1:";
	gets(Address1);
	gotoxy(7,13);
	cout<<"Line 2:";
	gets(Address2);
	gotoxy(5,14);
	cout<<"City:";
	gets(City);
	gotoxy(5,15);
	cout<<"State:";
	gets(State);
	gotoxy(5,16);
	cout<<"Country:India";
	strcmp(Country,"India");
	gotoxy(5,17);
	cout<<"Litrate(y/n):";
	cin>>Litrate;
	while(Litrate!='y' && Litrate!='Y' && Litrate!='n' && Litrate!='N')
	{
		gotoxy(15,30);
		cout<<"Only y or n allowed";
		gotoxy(5,17);
		clreol();
		gotoxy(5,17);
		cout<<"Literate(y/n):";
		cin>>Litrate;
		gotoxy(15,30);
		clreol();
	}
	gotoxy(15,30);
	cout<<"g->general  O->OBC  s->ST/SC";
	gotoxy(5,18);
	cout<<"Category:";
	cin>>Category;
	while(Category!='g' && Category!='o' && Category!='s')
	{
		gotoxy(15,30);
		clreol();
		gotoxy(15,30);
		cout<<"Only g,o or s allowed";
		getch();
		gotoxy(15,30);
		clreol();
		gotoxy(5,18);
		clreol();
		gotoxy(15,30);
		cout<<"g->general  O->OBC  s->ST/SC";
		gotoxy(5,18);
		cout<<"Category:";
		cin>>Category;
	}
	gotoxy(15,30);
	clreol();
	gotoxy(5,19);
	cout<<"Select Password:";
	gets(Password);
	gotoxy(10,28);
	cout<<"(Password more than 5 character but less than 20)";
	while(strlen(Password)<6 || strlen(Password)>20)
	{
		gotoxy(15,30);
		cout<<"Invalid Password!!!Re-Try";
		getch();
		gotoxy(5,19);
		clreol();
		gotoxy(5,19);
		cout<<"Select Password:";
		gets(Password);
		gotoxy(15,30);
		clreol();
	}
	gotoxy(10,28);
	clreol();
	gotoxy(5,21);
	cout<<"Enter a security Question:";
	gets(Security_Ques);
	gotoxy(5,22);
	cout<<"Enter its answer:";
	gets(Answer);              //Till here date is correctly shown if tested
	if(id=='a')                     
	{
		Status='p';
		gotoxy(5,24);
		cout<<"Enter ID:";
		gets(ID);                            //Here date changes
		while(strlen(ID)!=16)
		{
			gotoxy(15,30);
			cout<<"ID must be 16 characters long!!!!";
			getch();
			gotoxy(5,24);
			clreol();
			gotoxy(5,24);
			cout<<"Enter ID:";
			gets(ID);
			gotoxy(15,30);
			clreol();
		}
	}
	else
	{
		Status='r';
		strcpy(ID,"ABCDEFGH12345678");
	}
	gotoxy(15,30);
	cout<<"Press any key to continue...";       
	getch();
	Details(u1,'a');
	gotoxy(5,30);
	cout<<"1) Create/Apply";
	gotoxy(25,30);
	cout<<"2) Modify";
	gotoxy(35,30);
	cout<<"3) Cancel";
	ch=getch();
	while(ch!='1' && ch!='2' && ch!='3')
	{
		gotoxy(15,35);
		cout<<"Error in Input!!!Re-enter";
		getch();
		gotoxy(15,35);
		clreol();
		ch=getch();
	}
	switch(ch)
	{
		case'1':ch='y';
			 break;
		case'2':
			break;
		case'3':ch='n';
			break;
	}
	return(ch);
}
Last edited on
I think the getch() function might be the problem.
getch() @ line140?
Removing that doesn't help.Still same problem.
any help?
Are you sure that your arrays to store data you read from gets is big enough. gets do no bounds checking so if you enter too long input it will just write out of bounds which might affect other data.
1)All arrays size are good enough and I tried restrictions on them also.Doesn't help

2)Date till shown is correct but after that changes.There is no new entry there

3)Is it problem with my compiler or computer?
Is it dd that becomes 0? How can you know without printing it?
Yes dd is changing.I know its problem because I tested it in b/w lines.Printed it.That code is not shown here.But you can consider that.
Also Details() function prints date.That too not shown here(long).But I have tested it at various points.And dd changes before using Details() function.(shown where it changes in code)
Make a real program of the code you have posted that compiles and demonstrates the problem you have. If you fail to do this, the problem is probably somewhere else.
I am posting from real code only.Program is 1000's in line.I can post program(.exe) and you can check this.Can I upload that to this site.(I am new here so don't know)
What you have posted so far doesn't contain enough information to know what's wrong. 1000 lines is a bit much to look though so it's good if you remove as much as possible that is not relevant.

What I mean is that you should remove all the unrelated stuff from your program and create a program as small as possible but that still compiles and experience the problem you have.

Last edited on
Debugger
Conditional break point. 'dd changes'
Or step by step execution, while watching the variables.


All arrays size are good enough and I tried restrictions on them also
I doubt it.
while(strlen(ID)!=16) is too late, you already corrupt everything.

Also, don't use gets()
You shouldn't embed the interface with the logic.
then should I use cin.get() or cin.getline()???
And I have to use cin.ignore with them????
See if its correct or not

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>

char id='a';	//for admin


class User
{
	char FName[15];
	char LName[15];
	char ID[16];
	int dd,mm,yyyy;
	char Address1[40],Address2[40];
	char City[15];
	char State[15];
	char Country[15];          //of Current residense
	char Litrate;                       //y or n
	char Category;         //g->OP   o->OBC   s->ST/SC
	char Password[20];
	char Status;      //a->active    b->banned   r->request/applied    p->under process
	char Security_Ques[25];
	char Answer[10];
	public:
	int SNo;
	void Name(char []);
	void Pass(char []);
	char Input();
	void Checkreq();
	void Details(User ,char );   //char  o->applied   c->created
	char Search_User(User );
	void Request();
}u1;

char User::Input()
{
	clrscr();
	char ch='n';
	cin.ignore();
	gotoxy(10,30);
	cout<<"Note:";
	gotoxy(15,2);
	cout<<"Enter details for ID";
	gotoxy(5,5);
	cout<<"First Name(include Middle name):";
	cin.getline(FName,15);
	gotoxy(5,6);
	cout<<"Last Name:";
	cin.getline(LName,15);
	gotoxy(5,7);
	cout<<"Date of Birth:";
	gotoxy(7,8);
	cout<<"Date(dd):";
	cin>>dd;
	gotoxy(7,9);
	cout<<"Month(mm):";
	cin>>mm;
	gotoxy(7,10);
	cout<<"Year(yyyy):";
	cin>>yyyy;
	while(dd<0 || dd>31 || (mm%2==0 && dd==31) || (mm==2 && yyyy%4!=0 && dd>28) || (mm==2 && yyyy%4==0 && dd>29))
	{
		gotoxy(15,30);
		cout<<"Incorrect Data!Re-enter Date, Month and Year";
		gotoxy(7,8);
		clreol();
		gotoxy(7,9);
		clreol();
		gotoxy(7,10);
		clreol();
		gotoxy(7,8);
		cout<<"Date(dd):";
		cin>>dd;
		gotoxy(7,9);
		cout<<"Month(mm):";
		cin>>mm;
		gotoxy(7,10);
		cout<<"Year(yyyy):";
		cin>>yyyy;
		gotoxy(15,30);
		clreol();
	}
	cin.ignore();
	gotoxy(5,11);
	cout<<"Enter Address:";
	gotoxy(7,12);
	cout<<"Line 1:";
	cin.getline(Address1,40);
	gotoxy(7,13);
	cout<<"Line 2:";
	cin.getline(Address2,40);
	gotoxy(5,14);
	cout<<"City:";
	cin.getline(City,15);
	gotoxy(5,15);
	cout<<"State:";
	cin.getline(State,15);
	gotoxy(5,16);
	cout<<"Country:India";
	strcmp(Country,"India");
	gotoxy(5,17);
	cout<<"Litrate(y/n):";
	cin>>Litrate;
	while(Litrate!='y' && Litrate!='Y' && Litrate!='n' && Litrate!='N')
	{
		gotoxy(15,30);
		cout<<"Only y or n allowed";
		gotoxy(5,17);
		clreol();
		gotoxy(5,17);
		cout<<"Literate(y/n):";
		cin>>Litrate;
		gotoxy(15,30);
		clreol();
	}
	gotoxy(15,30);
	cout<<"g->general  O->OBC  s->ST/SC";
	gotoxy(5,18);
	cout<<"Category:";
	cin>>Category;
	while(Category!='g' && Category!='o' && Category!='s')
	{
		gotoxy(15,30);
		clreol();
		gotoxy(15,30);
		cout<<"Only g,o or s allowed";
		getch();
		gotoxy(15,30);
		clreol();
		gotoxy(5,18);
		clreol();
		gotoxy(15,30);
		cout<<"g->general  O->OBC  s->ST/SC";
		gotoxy(5,18);
		cout<<"Category:";
		cin>>Category;
	}
	cin.ignore();
	gotoxy(15,30);
	clreol();
	gotoxy(5,19);
	cout<<"Select Password:";
	cin.getline(Password,20);
	gotoxy(10,28);
	cout<<"(Password more than 5 character but less than 20)";
	while(strlen(Password)<6 || strlen(Password)>20)
	{
		gotoxy(15,30);
		cout<<"Invalid Password!!!Re-Try";
		getch();
		gotoxy(5,19);
		clreol();
		gotoxy(5,19);
		cout<<"Select Password:";
		cin.getline(Password,20);
		gotoxy(15,30);
		clreol();
	}
	gotoxy(10,28);
	clreol();
	gotoxy(5,21);
	cout<<"Enter a security Question:";
	cin.getline(Security_Ques,25);
	gotoxy(5,22);
	cout<<"Enter its answer:";
	cin.getline(Answer,10);
	if(id=='a')                          
	{
		Status='p';
		gotoxy(5,24);
		cout<<"Enter ID:";
		cin.get(ID,17);                  //Here date changes
		while(strlen(ID)!=16)
		{
			gotoxy(15,30);
			cout<<"ID must be 16 characters long!!!!";
			getch();
			gotoxy(5,24);
			clreol();
			gotoxy(5,24);
			cout<<"Enter ID:";
			cin.ignore();
			cin.get(ID,17);
			gotoxy(15,30);
			clreol();
		}
	}
	else
	{
		Status='r';
		strcpy(ID,"ABCDEFGH12345678");
	}
	cout<<"\n"<<dd;
	gotoxy(15,30);
	cout<<"Press any key to continue...";
	getch();
	//Details(u1,'o');
	gotoxy(5,30);
	cout<<"1) Create";
	gotoxy(25,30);
	cout<<"2) Modify";
	gotoxy(35,30);
	cout<<"3) Cancel";
	ch=getch();
	while(ch!='1' && ch!='2' && ch!='3')
	{
		gotoxy(15,35);
		cout<<"Error in Input!!!Re-enter";
		getch();
		gotoxy(15,35);
		clreol();
		ch=getch();
	}
	switch(ch)
	{
		case'1':ch='y';
			 break;
		case'2':
			break;
		case'3':ch='n';
			break;
	}
	return(ch);
}                       //sends to file write

void main()
{
        User user;
        user.Input();
}
Last edited on
It is not.
1
2
3
4
#include<iostream.h>
char ID[16];
cin.get(ID,17);
strcpy(ID,"ABCDEFGH12345678");
You are forgetting the '\0' so your array is too small.
Try to avoid magic numbers. As you are using char arrays, you may sizeof( ID )
Also the header should be #include <iostream>

Another alternative to gets() is fgets()
You may want to try std::string too
Last edited on
My program is working fine.Previous design and new one also.With correct ID and everything.So thanks for help on that regard.

But my original problem is date change.(not about ID,Its working correctly).

Please tell me what to do to retain correct date(dd).
Last edited on
Peter87 wrote:
Are you sure that your arrays to store data you read from gets is big enough. gets do no bounds checking so if you enter too long input it will just write out of bounds which might affect other data.
1
2
	char ID[16];
	int dd,mm,yyyy; //other data that gets affected because you step out of bounds 

you wrote:
but after that(shown in program i.e. if condition) the date changes to 0
me wrote:
You are forgetting the '\0'
strcpy(ID,"ABCDEFGH12345678"); ¿where do you think that the '\0' went?
Edit: it's actually undefined behaviour. But based on your description I say that you just wrote on the first byte of 'dd'. And you are using a little-endian machine.

So, stop going out of bounds.
If the problem still persists, point to the line that causes it.
me wrote:
Debugger
Conditional break point. 'dd changes'
Or step by step execution, while watching the variables.
Last edited on
Its changing while getting id.

Still not solved
Last edited on
still not solved help by posting code to get id without changing date.
do you understand what ne555 is trying to say?

if you want to read 16 characters you must have an array size of 17 due to the trailing '\0'.

So ID[17];!

Read this: http://www.cplusplus.com/reference/clibrary/cstdio/gets/
Pages: 12