problem again

Reply

Join Date: Aug 2005
Posts: 9
Reputation: cyber_aziz is an unknown quantity at this point 
Solved Threads: 0
cyber_aziz cyber_aziz is offline Offline
Newbie Poster

problem again

 
0
  #1
Aug 8th, 2005
hello ppl
this is my 2nd post and as i said earlier, im writing a cpp program for an exam project.
the project is designing a school database system

neways the problem is that the the compiler hangs and terminates whenever a wrong type of data is entered i mean if (ch is an integer and text is entered)
chk out the code below for example. its a snippet of the whole prog but ive edited it so tht it can run independently for testing.

i have tried many things to stop this from happening like clearing the values of the variables every time a wrong input is given but... it dosent work
and also if there are places where something simpler could have been done please tell me

PLEASE HELP

thanx in advance

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include <dos.h>
  4. #include <stdio.h>
  5. #include <graphics.h>
  6. #include <process.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10. struct ddate //structure to store the date. with the built in 1 it is
  11. { //difficult to check for a valid date
  12. int month; //if there is an easy way please tell me
  13. int year;
  14. int day;
  15. };
  16.  
  17. void border () // function that makes the page border
  18. {
  19. setbkcolor (1);
  20. setcolor (15);
  21. rectangle (0, 0, 639, 479);
  22. rectangle (5, 5, 634, 474);
  23.  
  24. struct time t; struct date d;
  25. rectangle (535, 5, 634, 55);
  26. rectangle (530, 5, 634, 60);
  27. gotoxy (72, 2);
  28. gettime (&t);
  29. printf("%2d:%02d\n",t.ti_hour, t.ti_min);
  30. gotoxy (70, 3);
  31. getdate(&d);
  32. printf("%d/%d/%d", d.da_day, d.da_mon, d.da_year);
  33.  
  34. // x1 y1 x2 y2
  35. line ( 5, 370, 634, 370);
  36. line ( 5, 375, 634, 375);
  37.  
  38. settextstyle (1,0,0);
  39. setusercharsize (1,2,2,5);
  40. outtextxy (50, 450, "Press 0 To Exit");
  41. outtextxy (400,450, "Press F1 for help");
  42.  
  43. }
  44.  
  45. void clear (int col, int row, int end = 65) //clears the line for a specific
  46. { //area
  47. for (int i=col; i<=end; i++)
  48. {
  49. gotoxy (i, row);
  50. cout << " ";
  51. }
  52. }
  53.  
  54.  
  55. void clear_box () //clears the textbox at the bottom
  56. { // of the page border
  57. for (int i=5; i<=76; i++)
  58. {
  59. for (int j=25; j<=28; j++)
  60. {
  61. gotoxy (i,j);
  62. cout << " ";
  63. }
  64. }
  65. }
  66.  
  67.  
  68. void add_disp () // generates the display for the addition of students
  69. {
  70. clrscr ();
  71. cleardevice ();
  72.  
  73. border ();
  74. settextstyle (1,0,3),
  75. outtextxy (200, 20, "Add Student Details");
  76.  
  77. rectangle (85, 105, 554, 330);
  78.  
  79. gotoxy (15, 9);
  80. cout << "Name: ";
  81. gotoxy (15, 10);
  82. cout << "Father's Name: ";
  83. gotoxy (15, 11);
  84. cout << "Mother's Name: ";
  85. gotoxy (15, 12);
  86. cout << "Date of Birth: ";
  87. gotoxy (43, 12);
  88. cout << "Place of Birth: ";
  89. gotoxy (15, 13);
  90. cout << "Sex: ";
  91. gotoxy (43, 13);
  92. cout << "Admission no: ";
  93. gotoxy (15, 14);
  94. cout << "Class: ";
  95. gotoxy (32, 14);
  96. cout << "Section: ";
  97. gotoxy (50, 14);
  98. cout << "Roll No: ";
  99. gotoxy (15, 15);
  100. cout << "Bus No: ";
  101. gotoxy (30, 15);
  102. cout << "Boarding Point: ";
  103. gotoxy (15, 16);
  104. cout << "Phone Nos: (R) ";
  105. gotoxy (26, 17);
  106. cout << "(O) ";
  107. gotoxy (26, 18);
  108. cout << "(M)";
  109. gotoxy (15, 19);
  110. cout << "Email: ";
  111. bar (50,450, 540, 470);
  112. }
  113.  
  114.  
  115. struct general
  116. {
  117. char name[25];
  118. char fath_name[25];
  119. char moth_name[25];
  120. ddate DOB;
  121. char POB[15];
  122. char sex;
  123. long admno;
  124. int cls;
  125. char section;
  126. int rollno;
  127. int busno;
  128. char board_point[20];
  129. char no_res[10];
  130. char no_off[10];
  131. char no_mob[10];
  132. char email[25];
  133. };
  134.  
  135.  
  136.  
  137.  
  138. void main()
  139. {
  140. clrscr();
  141.  
  142.  
  143. int gdriver = DETECT, gmode, errorcode;
  144. initgraph(&gdriver, &gmode, "");
  145.  
  146. cleardevice ();
  147. border ();
  148.  
  149. // declaration of variables
  150. char ch;
  151. general temp;
  152. int valid = 0;
  153. add_disp();
  154.  
  155. do //loop to accept name until a valid entry is inputed
  156. {
  157. clear_box ();
  158. clear (27,9);
  159. gotoxy (5,25);
  160. cout << "Enter Full Name Of The Student";
  161. gotoxy (21,9);
  162. gets (temp.name);
  163. valid = 1;
  164.  
  165. if (temp.name[0] == 27)
  166. return;
  167.  
  168. if ( strlen(temp.name) > 25 || strlen(temp.name) == 0 )
  169. {
  170. clear_box ();
  171. gotoxy (5,25);
  172. cout << "Name Cannot Be Blank Or Longer Than 25";
  173. getch ();
  174. valid = 0;
  175. }
  176. } while (!valid);
  177.  
  178. do
  179. {
  180. clear_box ();
  181. clear (30,10);
  182. gotoxy (5,25);
  183. cout << "Enter Father's Name";
  184. gotoxy (30,10);
  185. gets (temp.fath_name);
  186. valid = 1;
  187. if ( strlen(temp.fath_name) > 25 || strlen(temp.fath_name) == 0 )
  188. {
  189. clear_box ();
  190. gotoxy (5,25);
  191. cout << "Name Must Not Be Blank Or Longer Than 25";
  192. getch ();
  193. valid = 0;
  194. }
  195. } while (!valid);
  196.  
  197. do
  198. {
  199. clear_box ();
  200. clear (30,11);
  201. gotoxy (5,25);
  202. cout << "Enter Mother's Name";
  203. gotoxy (30,11);
  204. gets (temp.moth_name);
  205. valid = 1;
  206. if ( strlen(temp.moth_name) > 25 || strlen(temp.moth_name) == 0 )
  207. {
  208. clear_box ();
  209. gotoxy (5,25);
  210. cout << "Name Must Not Be Blank Or Longer Than 25";
  211. getch ();
  212. valid = 0;
  213. }
  214. } while (!valid);
  215.  
  216. do //this is where the problem occurs try entering a char for the date
  217. {
  218. clear_box ();
  219. clear (30,12,42);
  220.  
  221. gotoxy (5,25);
  222. cout << "Enter the Date Of Birth In The Format dd mm yyyy";
  223. gotoxy (30,12);
  224. cin >> temp.DOB.day;
  225. gotoxy (33,12);
  226. cin >> temp.DOB.month;
  227. gotoxy (36,12);
  228. cin >> temp.DOB.year;
  229. valid = 1;
  230. if ( temp.DOB.day > 31 || temp.DOB.day <=0 || temp.DOB.month > 12 || temp.DOB.month <=0 || temp.DOB.year > 2005 || temp.DOB.year < 1980)
  231. {
  232. temp.DOB.day=0;
  233. temp.DOB.month=0;
  234. temp.DOB.year=0;
  235. clear_box ();
  236. gotoxy (5,25);
  237. cout << "dd Must Be Between 1-31";
  238. gotoxy (5,26);
  239. cout << "mm Must Be Between 1-12";
  240. gotoxy (5,27);
  241. cout << "yyyy Must Be Between 1980-2005";
  242. getch ();
  243. valid = 0;
  244. }
  245. } while (!valid);
  246.  
  247. do
  248. {
  249. clear_box ();
  250. clear (59,12);
  251. gotoxy (5,25);
  252. cout << "Enter The Place Of Birth";
  253. gotoxy (59,12);
  254. gets (temp.POB);
  255. valid = 1;
  256. toupper (temp.section);
  257.  
  258. if ( strlen (temp.POB) > 15 || strlen (temp.POB) <=0 )
  259. {
  260. clear_box ();
  261. gotoxy (5,25);
  262. cout << "Place Of Birth Cannot Be Blank Or More Than 15";
  263. getch ();
  264. valid = 0;
  265. }
  266. } while (!valid);
  267.  
  268. do
  269. {
  270. clear_box ();
  271. clear (20,13,42);
  272. gotoxy (5,25);
  273. cout << "Enter M for Male or F for Female";
  274. gotoxy (20,13);
  275. cin >> temp.sex;
  276. temp.sex = toupper (temp.sex);
  277. valid = 1;
  278.  
  279. if ( temp.sex != 'M' && temp.sex != 'F' )
  280. {
  281. clear_box ();
  282. gotoxy (5,25);
  283. cout << "Enter Only M or F";
  284. getch ();
  285. valid = 0;
  286. }
  287. } while (!valid);
  288.  
  289. do //here agn entering a char causes it to hang
  290. {
  291. temp.admno=0;
  292. clear_box ();
  293. clear (57,13);
  294. gotoxy (5,25);
  295. cout << "Enter Admission Number";
  296. gotoxy (57,13);
  297. cin >> temp.admno;
  298. valid = 1;
  299.  
  300. if ( temp.admno > 1000000 )
  301. {
  302. clear_box ();
  303. gotoxy (5,25);
  304. cout << "Admission Number Too Long";
  305. getch ();
  306. valid = 0;
  307. }
  308. } while (!valid);
  309.  
  310. do //here too
  311. {
  312. temp.cls=0;
  313. clear_box ();
  314. clear (22,14,31);
  315. gotoxy (5,25);
  316. cout << "Enter Class";
  317. gotoxy (22,14);
  318. cin >> temp.cls;
  319. valid = 1;
  320.  
  321. if ( temp.cls > 13 || temp.cls <= 0 )
  322. {
  323. clear_box ();
  324. gotoxy (5,25);
  325. cout << "Class Must Be Between 1-12";
  326. getch ();
  327. valid = 0;
  328. }
  329. } while (!valid);
  330.  
  331. do
  332. {
  333. temp.section = ' ';
  334. char tem[1];
  335. clear_box ();
  336. clear (41,14,49);
  337. gotoxy (5,25);
  338. cout << "Enter section";
  339. gotoxy (41,14);
  340. cin >> tem; //this is done
  341. //because section
  342. temp.section = toupper (tem[0]); // must contain only 1
  343. // character
  344. valid = 1; // but if the user enters
  345. // more than 1 it wud be
  346. // a prob
  347. if ( tem[0] < 65 || tem[0] < 91 || strlen (tem) > 1 )
  348. {
  349. clear_box ();
  350. gotoxy (5,25);
  351. cout << "Section Must Be Between A-Z";
  352. getch ();
  353. valid = 0;
  354. }
  355. } while (!valid);
  356.  
  357. do
  358. {
  359. temp.rollno = 0;
  360. clear_box ();
  361. clear (59,14);
  362. gotoxy (5,25);
  363. cout << "Enter Roll No";
  364. gotoxy (59,14);
  365. cin >> temp.rollno;
  366. valid = 1;
  367.  
  368. if ( temp.rollno <= 0 )
  369. {
  370. clear_box ();
  371. gotoxy (5,25);
  372. cout << "Roll Number Cannot Be Blank";
  373. getch ();
  374. valid = 0;
  375. }
  376. } while (!valid);
  377.  
  378. do
  379. {
  380. temp.busno = 0;
  381. clear_box ();
  382. clear (23,15,29);
  383. gotoxy (5,25);
  384. cout << "Enter The Bus Number or 0 For Own Transport";
  385. gotoxy (23,15);
  386. cin >> temp.busno;
  387. valid = 1;
  388.  
  389. if ( temp.busno < 0 || temp.busno > 15 )
  390. {
  391. clear_box ();
  392. gotoxy (5,25);
  393. cout << "Bus Nuumber Must Be Between 0-15";
  394. getch ();
  395. valid = 0;
  396. }
  397. } while (!valid);
  398.  
  399. do
  400. {
  401. clear_box ();
  402. clear (46,15);
  403. gotoxy (5,25);
  404. cout << "Enter Boarding Point";
  405. gotoxy (46,15);
  406. gets (temp.board_point);
  407. valid = 1;
  408.  
  409. if ( strlen ( temp.board_point) > 15 )
  410. {
  411. clear_box ();
  412. gotoxy (5,25);
  413. cout << "Boarding Point Cannot Be Longer Than 15";
  414. getch ();
  415. valid = 0;
  416. }
  417. } while (!valid);
  418.  
  419. do // even though this is a char, this too gives the same error
  420. {
  421. //temp.no_res = {" "}; //WHATS WRONG
  422. //WITH THE SYNTAX
  423. clear_box ();
  424. clear (30,16);
  425. gotoxy (5,25);
  426. cout << "Enter Residence Phone Number";
  427. gotoxy (30,16);
  428. gets (temp.no_res);
  429. valid = 1;
  430.  
  431. if ( strlen (temp.no_res) > 10 || strlen (temp.no_res) <=0 )
  432. {
  433. clear_box ();
  434. gotoxy (5,25);
  435. cout << "Number Cannot Be Blank Or Longer Than 10";
  436. getch ();
  437. valid = 0;
  438. }
  439. } while (!valid);
  440.  
  441. do // even though this is a char, this too gives the same error
  442. {
  443. clear_box ();
  444. clear (30,17);
  445. gotoxy (5,25);
  446. cout << "Enter Office Phone Number";
  447. gotoxy (30,17);
  448. gets (temp.no_res);
  449. valid = 1;
  450.  
  451. if ( strlen (temp.no_off) > 10 )
  452. {
  453. clear_box ();
  454. gotoxy (5,25);
  455. cout << "Number Cannot Be Longer Than 10";
  456. getch ();
  457. valid = 0;
  458. }
  459. } while (!valid);
  460.  
  461. do // even though this is a char, this too gives the same error
  462. {
  463. clear_box ();
  464. clear (30,18);
  465. gotoxy (5,25);
  466. cout << "Enter Mobile Phone Number";
  467. gotoxy (30,18);
  468. gets (temp.no_mob);
  469. valid = 1;
  470.  
  471. if ( strlen (temp.no_mob) > 10 )
  472. {
  473. clear_box ();
  474. gotoxy (5,25);
  475. cout << "Number Cannot Be Longer Than 10";
  476. getch ();
  477. valid = 0;
  478. }
  479. } while (!valid);
  480.  
  481. do
  482. {
  483. clear_box ();
  484. clear (26,19);
  485. gotoxy (5,25);
  486. cout << "Enter Email Address";
  487. gotoxy (26,19);
  488. gets (temp.email);
  489. valid = 1;
  490.  
  491. if ( strlen (temp.email) > 25 )
  492. {
  493. clear_box ();
  494. gotoxy (5,25);
  495. cout << "Email Address Cannot Be Longer Than 25";
  496. getch ();
  497. valid = 0;
  498. }
  499. } while (!valid);
  500.  
  501. gotoxy (5,25);
  502. cout << "Are You Sure You Want To Save This Record (Y / N): ";
  503. gotoxy (56,25);
  504. cin >> ch;
  505.  
  506. toupper (ch);
  507. if (ch == 'N')
  508. return;
  509.  
  510. //f.add (temp);
  511.  
  512. clear_box ();
  513. gotoxy (5,25);
  514. cout << "Record Saved";
  515.  
  516. setfillstyle (0,1);
  517. //bar (50,450, 540, 470);
  518. clear_box ();
  519. getch ();
  520. }
Last edited by cyber_aziz; Aug 8th, 2005 at 7:27 pm. Reason: oops forgot to comment
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: problem again

 
0
  #2
Aug 8th, 2005
Simple. Always get stuff from the user as a string. Then check it and get from it what you need. If you don't find it - shoot an error and let a new input be given. There are some lines of code that you pasted there.. it's quite long for somebody to look through it and simplify it. After some time, when you become a better programmer go through the program yourself and make it better!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC