Graphics Causing Problems

I was using a function which I made for printing a message on screen, it works fine everywhere except one function. The Function which is displaying a message is error_scr() (CODE BELOW):
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
void error_scr()
	{
	   
	   clrscr();
	   cleardevice();
	   
	   settextstyle(2,0,8);
	    left = getmaxx() / 2 - 260;
	    top = getmaxy() / 2 - 30;
	    right = getmaxx() / 2 + 260;
	    bottom = getmaxy() / 2 + 30;
	   	   
	   for(i=1;i<=8;i++)
	   {	
			delay(100);
			setcolor(00);
			setfillstyle(SOLID_FILL,4);
			bar3d(left,top,right,bottom,0,0);
			setbkcolor(00);
			setbkcolor(00);
			 em_l=(left+right)/2;//Error Message Placement Left
			 em_t=(top+bottom)/2;//Error Message Placement Centre
			x=getmaxx()/2;
			setcolor(15);
			outtextxy(em_l,em_t," SORRY WRONG INPUT! PLEASE TRY AGAIN ");
			delay(200);
			cleardevice();
		}
	   clrscr();
	   cleardevice();
	   
	}


And this is the function I am calling this to where the output message is going out of the screen towards the right. But the Bar is coming just right in the center. The Function I am calling it to (CODE BELOW) Only a Part of it as its quite huge so only the first case of it:
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
261
262
263
264
265
266
 void buy()
  {	
	//initialize graphics, local variables
	initgraph(&gdriver, &gmode, "");

	// read result of initialization
	errorcode = graphresult();
	if (errorcode != grOk)  // an error occurred
	   {
		  printf("Graphics error: %s\n", grapherrormsg(errorcode));
		  printf("Press any key to halt:");
		  getch();
		  exit(1); // terminate with error code
	   }
	   
	  switch(tc)
	  {
	  case 1:
	{ 
		  
		  static int trailco_pay=0;
		  innovative();
		  detail person_innovative[70];//DECLARING SEPARATE STRUCTURE FOR INNOVATIVE DETAILS
		  
		  repay1://GOTO TAG
			
		 if(trailco_pay==3)
	   {
		   clrscr();
		   cleardevice();
		   setcolor(15);//RE SETTING OF COLOR VERY IMPORTANT
		   setbkcolor(8);
		   settextstyle(3,0,3);

		   sprintf(msg," PLEASE LEARN YOUR NUMBERS NEXT TIME! GOODBYE! ;)",bkcol);
		   outtextxy(318, 200, msg);
		   delay(3000);
		   restart();
		}
		 
		 clrscr();
		 cleardevice();
		 setbkcolor(4);
		 cout<<"\n\n CHOOSE MODE OF PAYMENT (1/2): \n\n 1. CREDIT CARD\n\n 2. NET BANKING\n\n CHOICE: ";
		 cin>>payment;
		 char bi[80];

		 switch(payment)
		{
		 case 1:
			{	
				clrscr();
				cleardevice();
				setbkcolor(RED);
				cout<< "\n\n PLEASE CHOOSE FROM BELOW: \n\n 1.VISA\n\n 2.MASTERCARD\n\n CHOICE: ";
				cin>>ccard;
				strcpy(bi, "\n       CREDIT CARD DETAILS     \n");
				break;
			}

		 case 2:
			{
				clrscr();
				cleardevice();
				setbkcolor(RED);
				cout<<"\nCHOOSE YOUR BANK FROM BELOW:\n\n 1.ICICI BANK\n\n 2.AXIS BANK\n\n 3.STATE BANK OF INDIA\n\n 4.KOTAK MAHINDRA\n\n 5.GO BACK\n\n CHOICE: ";
				cin>>netb;
				if(netb==5)
				goto repay1;
				else if((netb>5) || (netb<1))
				{
					error_scr();
					goto repay1;
				}
				strcpy(bi,"\n   NET BANKING DETAILS  \n");
				break;
			}

		 default:
		 
			  error_scr();
			  goto repay1;
		}

		redetails1:
		clrscr();
		cleardevice();
		setbkcolor(1);

		 puts(bi);

		 cout<<"\n      NAME  : ";
		 seatnumber=a[1];//The First Seat the USER SELECTS
		 gets(person_innovative[seatnumber].name);
		 for(i=0;i<strlen(person_innovative[seatnumber].name);i++)
		 {
			if((isalpha(person_innovative[seatnumber].name[i])==0) && (isspace(person_innovative[seatnumber].name[i])==0))
			{
				char mseg[]="SORRY NAME CAN CONTAIN ONLY ALPHABETS!";
				message(mseg);
				goto redetails1;
			}
		 }
		 for(i=2;i<=no_of_seat;i++)
		 {
			int exseatnumber=a[i];//THE EXTRA SEATS HE SELECTS AFTER SELECTING THE FIRST SEAT
			strcpy(person_innovative[exseatnumber].name,person_innovative[seatnumber].name);
		 }
			
		 cout<<"\n      MOBILE NUMBER  : ";
		 seatnumber=a[1];
		 gets(person_innovative[seatnumber].mob_no);
		 int mobsize=strlen(person_innovative[seatnumber].mob_no);
		 for(i=0;i<mobsize;i++)
		 {
			if((isdigit(person_innovative[seatnumber].mob_no[i])==0) || ((mobsize<10) || (mobsize>10)))
			{
				char mseg[] ="SORRY WRONG INPUT FOR MOBILE NUMBER!";
				message(mseg);
				goto redetails1;
			}
		 }
		 for(i=2;i<=no_of_seat;i++)
		 {
			int exseatnumber=a[i];
			strcpy(	person_innovative[exseatnumber].mob_no,person_innovative[seatnumber].mob_no);
		 }

		 
		 if(payment==2)
		 {
		 cout<<"\n      BANK ACCOUNT NUMBER  : ";
		 seatnumber=a[1];
		 gets(person_innovative[seatnumber].cardbank_no);
		 int cardbanksize=strlen(person_innovative[seatnumber].cardbank_no);
		 for(i=0;i<cardbanksize;i++)
		 {
			if((isdigit(person_innovative[seatnumber].cardbank_no[i])==0) || ((cardbanksize<15) || (cardbanksize>15)))
			{
				char mseg[]="SORRY WRONG INPUT FOR ACCOUNT NUMBER!";
				message(mseg);
				goto redetails1;
			}
		 }
		 for(i=2;i<=no_of_seat;i++)
		 {
			int exseatnumber=a[i];
			strcpy(person_innovative[exseatnumber].cardbank_no,person_innovative[seatnumber].cardbank_no);
		 }
		}
		
		else if(payment==1)
		{
		 cout<<"\n      CREDIT CARD NUMBER  : ";
		 seatnumber=a[1];
		 gets(person_innovative[seatnumber].cardbank_no);
		 int cardbanksize=strlen(person_innovative[seatnumber].cardbank_no);
		 for(i=0;i<cardbanksize;i++)
		 {
			if((isdigit(person_innovative[seatnumber].cardbank_no[i])==0) || ((cardbanksize<16) || (cardbanksize>16)))
			{
				char mseg[]="SORRY WRONG INPUT FOR CREDIT CARD NUMBER!";
				message(mseg);
				goto redetails1;
			}
		 }
		 for(i=2;i<=no_of_seat;i++)
		 {
			int exseatnumber=a[i];
			strcpy(person_innovative[exseatnumber].cardbank_no,person_innovative[seatnumber].cardbank_no);
		 }
		}
		 
		 cout<<"\n      E-MAIL ADDRESS  : ";
		 seatnumber=a[1];//The First Seat the USER SELECTS
		 gets(person_innovative[seatnumber].email);

				message(mseg);
				goto redetails1;
			}
		 
		 for(i=2;i<=no_of_seat;i++)
		 {
			int exseatnumber=a[i];//THE EXTRA SEATS HE SELECTS AFTER SELECTING THE FIRST SEAT
			strcpy(person_innovative[exseatnumber].email,person_innovative[seatnumber].email);
		 }
		 
		 cout<<"\n      AMOUNT: ";
		 cout<<cost;
		 cout<<"\n\n      SEATS BOOKED : ";
		 for(i=1;i<=no_of_seat;i++)
		 {
			cout<<"'"<<a[i]<<"'"<<" ";
		 }
		 cout<<"\n\n      THEATER : ";
		 cout<<theater[tc];
		 
		 cout<<"\n\n      ENTER PIN :  ";
		 pinno();
		 strcpy(person_innovative[seatnumber].pin,p);
		 for(i=2;i<=no_of_seat;i++)
		 {
			int exseatnumber=a[i];
			strcpy(person_innovative[exseatnumber].pin,person_innovative[seatnumber].pin);
		 }
		 
if(strcmp(person_innovative[seatnumber].pin,person_innovative[seatnumber].pincon)==0)
		 {
			cout<<"\n\n\t\t\t\t PIN CONFIRMED";
			cout<<"\n\n \t\t\t   PRESS 'ENTER' TO CONTINUE";
			getch();
		 }
		 
		 else
		 {
			cout<<"\n\n\t\t\t SORRY PINS DO NOT MATCH! REENTER";
			delay(850);
			trailco_pay++;
			goto redetails1;
		 }
		 
		 clrscr();
		 cleardevice();
		 setbkcolor(1);
		 setcolor(4);
		 itoa(cost,chcost,10);
		 setfillstyle(SOLID_FILL,15);
		 settextstyle(1,0,2);
		 bar3d(150,20,550,400,0,0);
		 outtextxy(270,40," BILLING INFORMATION ");

		 outtextxy(180,80," NAME : " );
		 outtextxy(240,80,person_innovative[seatnumber].name);
		 outtextxy(180,110," MOBILE   NUMBER : ");
		 outtextxy(330,110,person_innovative[seatnumber].mob_no);
		 if(payment==1)
		 outtextxy(180,140," CREDIT  CARD  NUMBER : ");
		 else if(payment==2)
		 outtextxy(180,140," BANK ACCOUNT  NUMBER : ");
		 outtextxy(380,140,person_innovative[seatnumber].cardbank_no);
		 outtextxy(180,170," TOTAL AMOUNT : Rs. ");
		 outtextxy(340,170,chcost);
		 outtextxy(180,200," THEATRE : ");
		 outtextxy(280,200,theater[tc]);
		 outtextxy(180,230," SHOW TIME : ");
		 billshowtime(stc);
		 outtextxy(180,260," SEATS BOOKED: ");
		 for(i=1;i<=no_of_seat;i++)
		 {
			static int w=300;
			char chseat[3];//SEAT NUMBER IN STRING FORMAT
			itoa(a[i],chseat,10);
			outtextxy(w,260,chseat);
			w+=30;
		 }

		 outtextxy(245,300," PRESS 'ENTER' TO CONFIRM ");
		 outtextxy(155,320," 'OR' ANY OTHER KEY TO GO BACK AND CHANGE DETAILS");
		 char con=getch();//CONFIRMATION KEY

			if(con=='\r')
			{
				char mseg[]="THANK YOU! YOUR TICKET IS BOOKED";
				message(mseg);
				restart();
			}

Please help have to submit the project on Monday and only this thing is keeping it at bay!
Last edited on
Who's responsible for teaching you programming. I'd like to have a word with them.
@hanst99
Well I'm still in school as of now, 11th so you could be a little less harsher! BTW what was the problem ?
Could you tell what libraries did you actually include ?
By the way, I don't know exactly what causes your code to crash, but I can tell that there are lots of lines, and truly you need to cut from that code... Over 200 rows of code for one single case... That's bad. If you want the code to run properly you got to rewrite it, but first, the most essential thing: write the pseudocode of the program before.


Always remember. The best programmer ALWAYS writes any code, of any difficuly in less space than any others (recursion). That's the difference among us all.

-----
Sorry for bad English.
Last edited on
Well, the problem is that your functions are 300 lines long. And if I am not mistaken those are borland functions. I don't blame you, but I have reasons to believe that the person teaching you this hasn't seen a real program in at least a decade, if not longer.
Agree with hanst, now THAT code is larger than my connect 4 multi tier AI !
I'm sorry, my English is no good. I keep reading your post but I don't understand what is your problem.
¿Crash, wrong answer, the graphics don't display properly?

About your ~250 LOC. Try to condense the code in helper functions.
By instance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void initialize_graphics(){
	//initialize graphics, local variables
	initgraph(&gdriver, &gmode, "");

	// read result of initialization
	errorcode = graphresult();
	if (errorcode != grOk)  // an error occurred
	{
		//printf("Graphics error: %s\n", grapherrormsg(errorcode));
		fprintf(stderr, "Graphics error: %s\n", grapherrormsg(errorcode) ); //error messages to the error stream
		//printf("Press any key to halt:"); 
		//getch(); //you shouldn't need this
		exit(1); // terminate with error code
	}
}
Now the thing is, ¿why the hell is the buy() function initializing graphics? Do one thing and do it well.

Next try to rewrite those jumps. The goto makes the code hard to follow.
Don't use gets(), it's dangerous.

Your interface is embeded with the logic of the program. It will be quite hard to make changes.
You are using quite a lot of magic numbers
1
2
3
4
5
6
7
		 outtextxy(180,170," TOTAL AMOUNT : Rs. ");
		 outtextxy(340,170,chcost);
		 outtextxy(180,200," THEATRE : ");
		 outtextxy(280,200,theater[tc]);
		 outtextxy(180,230," SHOW TIME : ");
		 billshowtime(stc);
		 outtextxy(180,260," SEATS BOOKED: ");
¿Isn't there an easier way? Like 'write all this text' or 'keep writing were you left, line break, center text, etc'

KISS
Exactly. As I said before, you MUST use recursion and lose some stupid lines like the ones that ne555 mentioned above.

/agree hanst99 Those are loads of borland functions. And lol. He used goto like ~10 times!!! For God's sake!
Last edited on
you MUST use recursion
¿where? ¿what for?
Everywhere. To lose the number of useless lines. If I have time, I could write that code in less than 100 lines! Using recursion.
The best programmer ALWAYS writes any code, of any difficuly in less space than any others (recursion). That's the difference among us all.


EDIT: Again sorry for my English. I hope you can understand what I'm saying.
Last edited on
Topic archived. No new replies allowed.