nasty bug help

I have a nasty bug in this piece of code not sure how to fix it.
I'm focused on the first part not the part where I return values(have not started to change that yet)

I enter in data and then select 3 times ether 'year' 'month' or 'day'
the way year month or day is selected will be the output of the data.
so if selected order was year month day then output would be "year month day" in that order. but I'm not sure how to do this so I've decided to use an array to store the order of what is selected..

other function will catch the value of 13 ( if selected was "year month day" )
and print data.

BUT!!! after you select the first 2 their counters are both '1' so when you select the third the place in array will be changed.

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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
int optionForDateFormat()
{
	Time temp;
	int day = 1;
	int month = 2;
	int year = 3;

	int dayMonthYear = 8;
	int dayYearMonth = 9;
	int monthDayYear = 10;
	int monthYearDay = 11;
	int yearDayMonth = 12;
	int yearMonthDay = 13;

	int choice = 0;
	int i = 0;
	int j = 0;
	int monthCounter = 0;
	int dayCoutner = 0;
	int yearCounter = 0;

	int selectedMonth = 0;
	int selectedDay = 0;
	int selectedYear = 0;

	char formatPrint[3];


	displayDateFormatOptions();

	cout << "\t\t\t\t\t\t\t\t\t\t";
	choice = _getch();

	while( choice != 27 && i < 3 )  //loops three times.
	{
		switch( choice )
		{
			while ( temp.usedMonth == true && temp.usedDay == true && temp.usedYear == true )
			{
				case 'd':
				case 'D':
					if( dayCoutner <= 0  )
					{
						cout << "Day " << endl;
						temp.usedDay = true;
						selectedDay++;

						if( monthCounter == 0 && yearCounter == 0 )
						{
							cout << "elemet in array 0 is now 1";
							formatPrint[0] = '1';
						}
						if( monthCounter == 1 )
						{
							cout << "elemet in array 1 is now 2";
							formatPrint[1] = '2';
						}
							else if( yearCounter == 1 )
							{
								cout << "elemet in array 1 is now 2";
								formatPrint[1] = '2';
							}
						if( monthCounter == 2 )
						{
							cout << "elemet in array 2 is now 3";
							formatPrint[2] = '3';
						}
							else if( yearCounter == 2 )
							{
								cout << "elemet in array 2 is now 3";
								formatPrint[2] = '3';
							}
						cout << endl;
					}
						if( dayCoutner > 0  )
						{
							cout << "Error: used day ";
							temp.usedDay = false;
							i--;
						}
					dayCoutner++;
					break;
				case 'm':
				case 'M':
					if( monthCounter <= 0 )
					{
						cout << "month " << endl;
						temp.usedMonth = true;
						selectedMonth++;

						if( dayCoutner == 0 && yearCounter == 0 )
						{
							cout << "elemet in array 0 is now 2";
							formatPrint[0] = '2';
						}
						if( dayCoutner == 1 )
						{
							cout << "elemet in array 1 is now 3";
							formatPrint[1] = '3';
						}
						else if( yearCounter == 1 )
						{
							cout << "elemet in array 1 is now 3";
							formatPrint[1] = '3';
                  }
						if( dayCoutner == 2 )
						{
							cout << "elemet in array 2 is now 1";
							formatPrint[2] = '1';
						}
						else if( yearCounter == 2 )
						{
							cout << "elemet in array 2 is now 1";
							formatPrint[2] = '1';
                  }
						cout << endl;
					}
						if( monthCounter > 0 )
						{
							cout << "Error: used month ";
							temp.usedMonth = false;
							i--;
						}
					monthCounter++;
					break;
				case 'y':
				case 'Y':
					if( yearCounter <= 0  )
					{
						cout << "year " << endl;
						temp.usedYear = true;
						selectedYear++;

						if( dayCoutner == 0 && monthCounter == 0 )
						{
							cout << "elemet in array 0 is now 3";
							formatPrint[0] = '3';
						}
						if( dayCoutner == 1 )
						{
							cout << "elemet in array 1 is now 1";
							formatPrint[1] = '1';
						}
						else if( monthCounter == 1 )
						{
							cout << "elemet in array 1 is now 1";
							formatPrint[1] = '1';
                  }
						if( dayCoutner == 2 )
						{
							cout << "elemet in array 2 is now 1";
							formatPrint[2] = '2';
						}
						else if( monthCounter == 2 )
						{
							cout << "elemet in array 2 is now 1";
							formatPrint[2] = '2';
						}
						cout << endl;
					}
						if( yearCounter > 0  )
						{
							cout << "Error: used year ";
							temp.usedYear = false;
							i--;
						}
					yearCounter++;
					break;

				default:
					cout <<" ERROR: Invalid choice";
					i--;
			}
		}
		i++;
		choice = _getch();
	}


	cout << "array is: ";
	for(int f = 0; f < 4; f++ )
	{
		cout << formatPrint[f];
	}

	getch();
	int k = 0;
	cout << endl;

	while( j < 3 && k < 3 )
	{
		if(  selectedDay == 1 ) //problem is here...
		{
			if( selectedMonth == 1 )
			{
				if(selectedYear == 1 )
				{
					k++;  //send data to print function or seperator function
					return dayMonthYear;  // 8
				}
         }
		  else if( selectedYear == 1 )
		  {
			  if( selectedMonth == 1 )
			  {
				  cout << "stufff"; getch();
				  k++;
				  return dayYearMonth; // 9
			  }
		  }
		}

		else if( selectedMonth == 1 )
		{
			if( selectedDay == 1 )
			{
				if( selectedYear == 1 )
				{
					cout << "stufff"; getch();
					k++;  //send data to print function or seperator function
					return monthDayYear;  //10
            }
			}
			if( selectedYear == 1 )
			{
				if( selectedDay == 1 )
				{
					cout << "stufff"; getch();
					k++;
					return monthYearDay; // 11
            }
			}
		}
		else if( selectedYear == 1  )
		{
			if(selectedDay == 1 )
			{
				if( selectedMonth == 1 )
				{
					cout << "stufff"; getch();
					k++;
					return yearDayMonth; //12
            }
			}
			if( selectedMonth == 1 )
			{
				if(selectedDay == 1 )
				{
					cout << "stufff"; getch();
					k++;
					return yearMonthDay; // 13
				}
         }

		}
		j++;
	}
	getch();
	return - 1;
}
The while loop on line 38 is rather strange. Does it compile? What is it supposed to do?

if you want the index of the array in selectedDay/selectedMonth/selectedYear you need to assign the index i. I.e. on line 46 selectedDay = i; and similar for line 89/132.
still get same problem
this is output "

1
2
3
4
5
6
7
year
element in array 0 is now 3
Day
element in array 1 is now 2
month
element in array 1 is now 3


for the while loop on line 38 it does compile.it is suppose to exit when user has selected day year or month atleast once, each time day/year/month is selected it makes it true, if user selects month/day/month then month is set to false due to the "if else " statements.

thanks for response.
just thinking if I add the I counter in somewhere.

do you think thing would work well?
1
2
3
4
5
6
7
8
9
10
	if( dayCoutner == 1 && i == 1 )
	{
	        cout << "elemet in array 1 is now 1";
	        formatPrint[1] = '1';
	}
	if( dayCoutner == 1 && i == 2 )
	{
		cout << "elemet in array 1 is now 1";
		formatPrint[1] = '1';
	}


if I where to do this then then the code itself will be a lot longer.
Last edited on
It looks like that dayCoutner/monthCounter/yearCounter exists so that the user cannot enter day/month/year twice, correct?

Do not use this counter for anything else. There is no array 0/1/2 only one: formatPrint.


I'm not sure what the use of formatPrint is, but i bet that it correspond with selectedDay/selectedMonth/selectedYear (not ...Counter).

I'd expect somthing like this:
1
2
3
4
5
6
7
8
9
10
				case 'd':
				case 'D':
...
						formatPrint[selectedDay] = '1';
...
				case 'm':
				case 'M':
...
						formatPrint[selectedMonth] = '2';
...


The while loop on line 38 is ignored. It does nothing. You might move the condition to line 34.
Topic archived. No new replies allowed.