From: "ADJ" Newsgroups: comp.os.msdos.djgpp Subject: SUBJECT:(HELP ME)My Computer is Stupied or I am Stupied ??? (Too mush lines? its source code!) Date: 20 May 1997 20:48:31 GMT Organization: ADJ Lines: 335 Message-ID: <01bc655e$cfb4e000$79297dc2@qwerty> NNTP-Posting-Host: dialup-121.dublin.iol.ie To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk SUBJECT:(HELP ME)My Computer is Stupied or I am Stupied ??? Hi, I have stupied problem. try to debug it on Addrec function. cin.getlin doesnt get string first time!!! ----------------------------------------------- ----------------------------------------------------- By the way I am working in Win95, using gcc version 2.7.2.1, and RHide1.2 ----------------------------------------------------- #include #include #include #include #include #include "mylib.h" /* enum bool{true,false}; */ typedef struct{ unsigned int nCode; char szFirstName[60]; char szSurName[60]; int nAge; }RecorD; class MyRecord { public: MyRecord(); void setFileName(char *); void AddRec(); void DelRec(); void DelAllRec(); void ViewRec(); void ViewAllRec(); void SearchRec(); void SortRec(); void Exit(); /* This function have to sort file save it only then exit */ void RecInFile(); protected: RecorD Rec; char szFileName[60]; int Search_4_Code(unsigned int ); }; MyRecord::MyRecord() { int i; Rec.nCode=0x0; for(i=0;i<60;i++) Rec.szFirstName[i]=Rec.szSurName[i]=0x0; Rec.nAge=0x0; } void MyRecord::setFileName(char *FName) { strcpy(szFileName,FName); } void MyRecord::AddRec() { FILE *tempfp; bool bOkCode=true; char szTemp[60]; int counteri=0; do { // if(bOkCode==false) { cout << endl << "Please enter a NUMBER between 1 and 30000" << endl; // } bOkCode=true; cout << endl << endl; cout << "Enter Code:\t\t"; /******************************************************************/ /* HERE DOESNT WANNA GETLINE FIRST TIME !!! **********************/ /******************************************************************/ cin.getline(szTemp,60); if(isitNumber(szTemp)==false) bOkCode=false; Rec.nCode=atoi(szTemp); if(bOkCode==true) { if((Search_4_Code(Rec.nCode))!=-1) { cout << endl << "Sorry But Record with " << Rec.nCode <<"code already exist." << endl << Search_4_Code(Rec.nCode); bOkCode=false; } if(Rec.nCode<=0) bOkCode=false; if(Rec.nCode>30000) bOkCode=false; } }while(bOkCode==false); cout << "Enter FirstName:\t"; cin.getline(Rec.szFirstName,60); cout << "Enter LastName:\t"; cin.getline(Rec.szSurName,60); bOkCode=true; do { if(bOkCode==false) cout << "Pleasae Enter Age between 1 and 120." << endl; bOkCode=true; cout << "Enter Age:\t\t"; fflush(stdin); cin.getline(szTemp,4); if(isitNumber(szTemp)==false) // isit number? if number set bObCode to FALSE bOkCode=false; Rec.nAge=atoi(szTemp); fflush(stdin); if(bOkCode==true) { if(Rec.nAge<=0) bOkCode=false; if(Rec.nAge>=120) bOkCode=false; } }while(bOkCode==false); if((tempfp=fopen(szFileName,"a+"))==NULL) { cout << "ERROR No.2 Cannot open append to file. Press any key."; getch(); exit(-1); } fwrite(&Rec, sizeof(Rec), 1, tempfp); // write struct Record to file fclose(tempfp); // close file } void MyRecord::DelRec() { } void MyRecord::DelAllRec() { } void MyRecord::ViewRec() { } void MyRecord::ViewAllRec() { } void MyRecord::SearchRec() { } void MyRecord::SortRec() { } void MyRecord::Exit() { } void MyRecord::RecInFile() { } int MyRecord::Search_4_Code(unsigned int unCode) { FILE *tempfp; bool bFound=false; int nCounter=-1; int nRead; RecorD Records; if((tempfp=fopen(szFileName,"r"))==NULL) { cout << "\nERROR No.3 Cannot open " << szFileName << " file. Press any key.\n"; getch(); exit(-1); } rewind(tempfp); do { ++nCounter; if(feof(tempfp)) break; nRead=fread(&Records, sizeof(Records), 1, tempfp); if(nRead==1) { if(Rec.nCode==unCode) bFound=true; else bFound=false; } else bFound=false; }while(bFound==false); fclose(tempfp); if(bFound==false) return -1; else return nCounter; } void New_File(MyRecord *); void Open_File(MyRecord *); void main(void) { MyRecord Record; char cChoice; do { clrscr(); cout << "Welcome to Simple DataBase!" << endl; cout << "---------------------------" << endl << endl << endl; cout << "1. New File" << endl << "2. Open File" << endl << "3. EXIT" << endl << "So? " << endl; cChoice=getch(); putch(cChoice); }while(cChoice!='1'&&cChoice!='2'&&cChoice!='3'); switch(cChoice) { case('1'): New_File(&Record); break; case('2'): Open_File(&Record); break; case('3'): exit(1); } do { clrscr(); cout << "---------------------\n"; cout << "1. Add Record\n"; cout << "2. Delete Record\n"; cout << "3. Delete All Records\n"; cout << "4. View Record\n"; cout << "5. View All Records\n"; cout << "6. Search for Record\n"; cout << "7. Sort Record\n"; cout << "8. EXIT\n"; cout << "So?\n"; switch(cChoice=getche()) { case('1'): Record.AddRec(); break; case('2'): Record.DelRec(); break; case('3'): Record.DelAllRec(); break; case('4'): Record.ViewRec(); break; case('5'): Record.ViewAllRec(); break; case('6'): Record.SearchRec(); break; case('7'): Record.SortRec(); break; case('8'): Record.Exit(); break; } }while(cChoice!='8'); exit(1); } void New_File(MyRecord *Record) { char strTempFileName[60]; bool bFileExist; FILE *fp1temp; do { cout << endl << "\t" << "Please Enter File Name: "; cin >> strTempFileName; if((isFileExist(strTempFileName))) { bFileExist=true; cout << "Sorry...but " << strTempFileName << " file already exist" << endl; } else { bFileExist=false; if((fp1temp=fopen(strTempFileName,"w"))==NULL) { cout << endl << "ERROR No.1 Cannot Create New File" << endl; exit(-1); } fclose(fp1temp); } }while(bFileExist==true); Record->setFileName(strTempFileName); /* set filename in class Record */ } void Open_File(MyRecord *Record) { char strTempFileName[60]; bool bFileExist; FILE *fptemp; do { cout << endl << "\t" << "Please Enter File Name: "; cin >> strTempFileName; if(!isFileExist(strTempFileName)) { bFileExist=false; cout << "Sorry...Cannot open " << strTempFileName << " file" << endl; } else { bFileExist=true; if((fptemp=fopen(strTempFileName,"r"))==NULL) { cout << endl << "ERROR No.1 Cannot Open " << strTempFileName << " File " << endl; exit(-1); fclose(fptemp); } } }while(bFileExist==false); Record->setFileName(strTempFileName); }