| | |
problem again
![]() |
•
•
Join Date: Aug 2005
Posts: 9
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> #include <dos.h> #include <stdio.h> #include <graphics.h> #include <process.h> #include <string.h> #include <ctype.h> struct ddate //structure to store the date. with the built in 1 it is { //difficult to check for a valid date int month; //if there is an easy way please tell me int year; int day; }; void border () // function that makes the page border { setbkcolor (1); setcolor (15); rectangle (0, 0, 639, 479); rectangle (5, 5, 634, 474); struct time t; struct date d; rectangle (535, 5, 634, 55); rectangle (530, 5, 634, 60); gotoxy (72, 2); gettime (&t); printf("%2d:%02d\n",t.ti_hour, t.ti_min); gotoxy (70, 3); getdate(&d); printf("%d/%d/%d", d.da_day, d.da_mon, d.da_year); // x1 y1 x2 y2 line ( 5, 370, 634, 370); line ( 5, 375, 634, 375); settextstyle (1,0,0); setusercharsize (1,2,2,5); outtextxy (50, 450, "Press 0 To Exit"); outtextxy (400,450, "Press F1 for help"); } void clear (int col, int row, int end = 65) //clears the line for a specific { //area for (int i=col; i<=end; i++) { gotoxy (i, row); cout << " "; } } void clear_box () //clears the textbox at the bottom { // of the page border for (int i=5; i<=76; i++) { for (int j=25; j<=28; j++) { gotoxy (i,j); cout << " "; } } } void add_disp () // generates the display for the addition of students { clrscr (); cleardevice (); border (); settextstyle (1,0,3), outtextxy (200, 20, "Add Student Details"); rectangle (85, 105, 554, 330); gotoxy (15, 9); cout << "Name: "; gotoxy (15, 10); cout << "Father's Name: "; gotoxy (15, 11); cout << "Mother's Name: "; gotoxy (15, 12); cout << "Date of Birth: "; gotoxy (43, 12); cout << "Place of Birth: "; gotoxy (15, 13); cout << "Sex: "; gotoxy (43, 13); cout << "Admission no: "; gotoxy (15, 14); cout << "Class: "; gotoxy (32, 14); cout << "Section: "; gotoxy (50, 14); cout << "Roll No: "; gotoxy (15, 15); cout << "Bus No: "; gotoxy (30, 15); cout << "Boarding Point: "; gotoxy (15, 16); cout << "Phone Nos: (R) "; gotoxy (26, 17); cout << "(O) "; gotoxy (26, 18); cout << "(M)"; gotoxy (15, 19); cout << "Email: "; bar (50,450, 540, 470); } struct general { char name[25]; char fath_name[25]; char moth_name[25]; ddate DOB; char POB[15]; char sex; long admno; int cls; char section; int rollno; int busno; char board_point[20]; char no_res[10]; char no_off[10]; char no_mob[10]; char email[25]; }; void main() { clrscr(); int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, ""); cleardevice (); border (); // declaration of variables char ch; general temp; int valid = 0; add_disp(); do //loop to accept name until a valid entry is inputed { clear_box (); clear (27,9); gotoxy (5,25); cout << "Enter Full Name Of The Student"; gotoxy (21,9); gets (temp.name); valid = 1; if (temp.name[0] == 27) return; if ( strlen(temp.name) > 25 || strlen(temp.name) == 0 ) { clear_box (); gotoxy (5,25); cout << "Name Cannot Be Blank Or Longer Than 25"; getch (); valid = 0; } } while (!valid); do { clear_box (); clear (30,10); gotoxy (5,25); cout << "Enter Father's Name"; gotoxy (30,10); gets (temp.fath_name); valid = 1; if ( strlen(temp.fath_name) > 25 || strlen(temp.fath_name) == 0 ) { clear_box (); gotoxy (5,25); cout << "Name Must Not Be Blank Or Longer Than 25"; getch (); valid = 0; } } while (!valid); do { clear_box (); clear (30,11); gotoxy (5,25); cout << "Enter Mother's Name"; gotoxy (30,11); gets (temp.moth_name); valid = 1; if ( strlen(temp.moth_name) > 25 || strlen(temp.moth_name) == 0 ) { clear_box (); gotoxy (5,25); cout << "Name Must Not Be Blank Or Longer Than 25"; getch (); valid = 0; } } while (!valid); do //this is where the problem occurs try entering a char for the date { clear_box (); clear (30,12,42); gotoxy (5,25); cout << "Enter the Date Of Birth In The Format dd mm yyyy"; gotoxy (30,12); cin >> temp.DOB.day; gotoxy (33,12); cin >> temp.DOB.month; gotoxy (36,12); cin >> temp.DOB.year; valid = 1; 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) { temp.DOB.day=0; temp.DOB.month=0; temp.DOB.year=0; clear_box (); gotoxy (5,25); cout << "dd Must Be Between 1-31"; gotoxy (5,26); cout << "mm Must Be Between 1-12"; gotoxy (5,27); cout << "yyyy Must Be Between 1980-2005"; getch (); valid = 0; } } while (!valid); do { clear_box (); clear (59,12); gotoxy (5,25); cout << "Enter The Place Of Birth"; gotoxy (59,12); gets (temp.POB); valid = 1; toupper (temp.section); if ( strlen (temp.POB) > 15 || strlen (temp.POB) <=0 ) { clear_box (); gotoxy (5,25); cout << "Place Of Birth Cannot Be Blank Or More Than 15"; getch (); valid = 0; } } while (!valid); do { clear_box (); clear (20,13,42); gotoxy (5,25); cout << "Enter M for Male or F for Female"; gotoxy (20,13); cin >> temp.sex; temp.sex = toupper (temp.sex); valid = 1; if ( temp.sex != 'M' && temp.sex != 'F' ) { clear_box (); gotoxy (5,25); cout << "Enter Only M or F"; getch (); valid = 0; } } while (!valid); do //here agn entering a char causes it to hang { temp.admno=0; clear_box (); clear (57,13); gotoxy (5,25); cout << "Enter Admission Number"; gotoxy (57,13); cin >> temp.admno; valid = 1; if ( temp.admno > 1000000 ) { clear_box (); gotoxy (5,25); cout << "Admission Number Too Long"; getch (); valid = 0; } } while (!valid); do //here too { temp.cls=0; clear_box (); clear (22,14,31); gotoxy (5,25); cout << "Enter Class"; gotoxy (22,14); cin >> temp.cls; valid = 1; if ( temp.cls > 13 || temp.cls <= 0 ) { clear_box (); gotoxy (5,25); cout << "Class Must Be Between 1-12"; getch (); valid = 0; } } while (!valid); do { temp.section = ' '; char tem[1]; clear_box (); clear (41,14,49); gotoxy (5,25); cout << "Enter section"; gotoxy (41,14); cin >> tem; //this is done //because section temp.section = toupper (tem[0]); // must contain only 1 // character valid = 1; // but if the user enters // more than 1 it wud be // a prob if ( tem[0] < 65 || tem[0] < 91 || strlen (tem) > 1 ) { clear_box (); gotoxy (5,25); cout << "Section Must Be Between A-Z"; getch (); valid = 0; } } while (!valid); do { temp.rollno = 0; clear_box (); clear (59,14); gotoxy (5,25); cout << "Enter Roll No"; gotoxy (59,14); cin >> temp.rollno; valid = 1; if ( temp.rollno <= 0 ) { clear_box (); gotoxy (5,25); cout << "Roll Number Cannot Be Blank"; getch (); valid = 0; } } while (!valid); do { temp.busno = 0; clear_box (); clear (23,15,29); gotoxy (5,25); cout << "Enter The Bus Number or 0 For Own Transport"; gotoxy (23,15); cin >> temp.busno; valid = 1; if ( temp.busno < 0 || temp.busno > 15 ) { clear_box (); gotoxy (5,25); cout << "Bus Nuumber Must Be Between 0-15"; getch (); valid = 0; } } while (!valid); do { clear_box (); clear (46,15); gotoxy (5,25); cout << "Enter Boarding Point"; gotoxy (46,15); gets (temp.board_point); valid = 1; if ( strlen ( temp.board_point) > 15 ) { clear_box (); gotoxy (5,25); cout << "Boarding Point Cannot Be Longer Than 15"; getch (); valid = 0; } } while (!valid); do // even though this is a char, this too gives the same error { //temp.no_res = {" "}; //WHATS WRONG //WITH THE SYNTAX clear_box (); clear (30,16); gotoxy (5,25); cout << "Enter Residence Phone Number"; gotoxy (30,16); gets (temp.no_res); valid = 1; if ( strlen (temp.no_res) > 10 || strlen (temp.no_res) <=0 ) { clear_box (); gotoxy (5,25); cout << "Number Cannot Be Blank Or Longer Than 10"; getch (); valid = 0; } } while (!valid); do // even though this is a char, this too gives the same error { clear_box (); clear (30,17); gotoxy (5,25); cout << "Enter Office Phone Number"; gotoxy (30,17); gets (temp.no_res); valid = 1; if ( strlen (temp.no_off) > 10 ) { clear_box (); gotoxy (5,25); cout << "Number Cannot Be Longer Than 10"; getch (); valid = 0; } } while (!valid); do // even though this is a char, this too gives the same error { clear_box (); clear (30,18); gotoxy (5,25); cout << "Enter Mobile Phone Number"; gotoxy (30,18); gets (temp.no_mob); valid = 1; if ( strlen (temp.no_mob) > 10 ) { clear_box (); gotoxy (5,25); cout << "Number Cannot Be Longer Than 10"; getch (); valid = 0; } } while (!valid); do { clear_box (); clear (26,19); gotoxy (5,25); cout << "Enter Email Address"; gotoxy (26,19); gets (temp.email); valid = 1; if ( strlen (temp.email) > 25 ) { clear_box (); gotoxy (5,25); cout << "Email Address Cannot Be Longer Than 25"; getch (); valid = 0; } } while (!valid); gotoxy (5,25); cout << "Are You Sure You Want To Save This Record (Y / N): "; gotoxy (56,25); cin >> ch; toupper (ch); if (ch == 'N') return; //f.add (temp); clear_box (); gotoxy (5,25); cout << "Record Saved"; setfillstyle (0,1); //bar (50,450, 540, 470); clear_box (); getch (); }
Last edited by cyber_aziz; Aug 8th, 2005 at 7:27 pm. Reason: oops forgot to comment
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!
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Cellphones, PDAs and Handheld Devices)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
Other Threads in the C Forum
- Previous Thread: Need Help With Piglatin Program In C
- Next Thread: Global Variables help
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate lowest match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h





