I have a problem about the code that skip the ID Number can you analyze the problem just enter 2 for adding and you can see the result

Recommended Answers

All 2 Replies

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<math.h>

char inf[][50]={"Id Number:\n","Full Name:\n","Address:\n","Birthday:\n","Civil Status:\n","Gender:\n","Education:\n","RatePerHour:\n","Date of Hire:\n"};
char *record[50][50];
int count;

void AddRecord();
void ViewRecord();
void DeleteRecord();
void Payslip();

void main(){
char yn;int menu;
	do{
	clrscr();
		cout<<"ABC Company\n";
		cout<<"Information System\n";
		cout<<"<MENU>";
		cout<<"\n[1] View Record\n[2]Add Record\n[3]Delete Record\n[4]Payslip\n->";

		cin>>menu;
		switch (menu){
			case 1:	cout<<"View Records\n[Enter 0 to go back Main Menu]\n";
								ViewRecord();
				break;
			case 2:	cout<<"Add Record\n[Enter 0 to go back Main Menu]\n";
								AddRecord();
				break;
			case 3: 	cout<<"Delete Record\n[Enter 0 to go back Main Menu]";
								DeleteRecord();
				break;
			case 4:	cout<<"Payslip\n[Enter 0 to go back Main Menu]";
								Payslip();
				break;
			default:	cout<<"Invalid Input";
		}
	cout<<"do you like to conitue:";
	cin>>yn;
	}while(yn=='y' || yn=='Y');

}
void ViewRecord(){
	char view[10];int x, y;
	if(record[0][0]!=NULL){
		cout<<"Enter Employee Number\n";
		cin>>view;
		if (view!=0){
			for(x=0;x<count;x++){
				if(strcmp(record[x][0],view)==0){
					for(y=0;y<9;y++){
						cout<<inf[y];
						cout<<record[x][y]<<endl;
					}
				}else
					cout<<"Record not found";
			}
		}
	}else
		cout<<"\n\tNo record found\n";
}
void AddRecord(){
int x,y,cmps;char ny,n;

cout<<endl;
	do{
		if(record[0][0]!=NULL){
			for(y=0;y<9;y++){
				cout<<inf[y];
				cin.getline(record[count][y],40);
					for(x=count-1;x>=0;x--){
						if(strcmp(record[x][0],record[count][0])==0){
							cout<<"Record already Exist\n";
							break;
						}else if(record[count][0]==0){
							count=count-1;
							break;
						}else{
							cin.getline(record[x][y],50);
						}
					}
					count=count+1;
			}
		}else{
				for(n=0;n<9;n++){
				cout<<inf[n];
				cin.getline(record[0][n],50);
				}
				count=1;
		}
		cout<<"Do you want to add Data[y\\n]:";cin>>ny;
	}while(ny=='y'||ny=='Y');

}
void DeleteRecord(){
	char view[10];int x, y;char yn;
	cout<<"Enter Employee Number\n";
	cin>>view;
	if (view!=0){
		for(x=0;x<count;x++){
			if(strcmp(record[x][0],view)==0){
				cout<<"\nAre you sure to delete this item[y\'n]";
				cin>>yn;
				if(yn=='y'||yn=='Y'){
					for(y=0;y<9;y++){
						for(x;x<count;x++){
							if((x+1)!=count)
								record[x][y]=record[x+1][y];
							else
								delete record[x][y];
						}
					}
				}
			}else
				cout<<"Record not found";
		}
	}
}
void Payslip(){
	char view[10];int x, y;
	char dates[50];float rate;float gp,np;
	int hrs;
	cout<<"Enter Pay date:";
	cin.getline(dates,40);
	cout<<"Enter Employee Number\n";
	cin>>view;
	if (view!=0){
		for(x=0;x<count;x++){
			if(strcmp(record[x][0],view)==0){
					cout<<inf[1];
					cout<<record[x][1]<<endl;
					cout<<inf[7];
					cout<<record[x][7]<<endl;

					rate=atof(record[x][7]);
					cout<<"Enter Numbers of hours work:";
					cin>>hrs;
					np=hrs*rate;
					cout<<"Gross Pay: "<<np<<endl;
					cout<<"SSS=2%, Tax=3%, Others=3%\n";
					gp=np-((np*0.02)+(np*0.03)+(np*0.03));
					cout<<"Gross Pay: "<<gp<<endl;
			}else
				cout<<"Record not found";
		}
	}


}

My compiler can't handle the old headers and conio.h, so I had to change them and also change it from void main to int main to get it to compile. I also changed the variable name count because I used namespace std for the whole program because of the cout statements.

I voted "hard to analyze" because I think you need to describe the problem more clearly and tell us what part of the code to focus on.

Your original code (unchanged) is above.

I think you should make use of cin.ignore(1000); or something like that ...
For more information read the following thread about it:
http://www.daniweb.com/forums/thread90228.html
The error is definitely in the function 'void AddRecord()' ...

I posted function where the error resides in:

void AddRecord(){
int x,y,cmps;char ny,n;

cout<<endl;
	do{
		if(record[0][0]!=NULL){
			for(y=0;y<9;y++){
				cout<<inf[y];
				cin.ignore(2);
				cin.getline(record[count][y],40);
					for(x=count-1;x>=0;x--){
						if(strcmp(record[x][0],record[count][0])==0){
							cout<<"Record already Exist\n";
							break;
						}else if(record[count][0]==0){
							count=count-1;
							break;
						}else{
							cin.getline(record[x][y],50);
						}
					}
					count=count+1;
			}
		}else{
				for(n=0;n<9;n++){
				cout<<inf[n];
				cin.ignore(2);
				cin.getline(record[0][n],50);
				}
				count=1;
		}
		cout<<"Do you want to add Data[y\\n]:";cin>>ny;
	}while(ny=='y'||ny=='Y');

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.