Everytime I run this program and enter a new record, it gets terminated on these statements:

stud.pcent=((stud.mhin+stud.meng+stud.mmat+stud.msci+stud.msst)*100)/(mm.hin+mm.eng+mm.sst+mm.sci+mm.mat);
			fwrite(&stud,recsize,1,file);

The whole program is this. It is only partially complete.

#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define UARROW 72
#define DARROW 80
#define ENTER 13
#define ESCAPE 27
int key;
char s=219;
FILE *file,*tmp;
long int recsize;
void getkey();
struct profile
{
	char fname[15],lname[15],month[10];
	int date,year,grade,roll,admin;
	float meng,msst,mmat,msci,mhin,pcent;
}stud;
struct max
{
	float hin,eng,sci,sst,mat;
}mm;

void addrec()
{
	fseek(file,0,SEEK_END);
	int ctr=0;
	while(ctr==0)
	{
			gotoxy(5,2);
			cout<<"You are in the record entry module. Follow the steps to enter the record successfully.";
			gotoxy(10,4);
			cout<<"Enter first name: ";
			gets(stud.fname);
			gotoxy(10,5);
			cout<<"Enter last name: ";
			gets(stud.lname);
			gotoxy(10,6);
			cout<<"Enter grade: ";
			cin>>stud.grade;
			gotoxy(10,7);
			cout<<"Enter roll number: ";
			cin>>stud.roll;
			gotoxy(10,8);
			cout<<"Enter scholar number: ";
			cin>>stud.admin;
			gotoxy(10,10);
			cout<<"Date of birth: Date: ";
			cin>>stud.date;
			gotoxy(25,11);
			cout<<"Month(type the full name): ";
			gets(stud.month);
			gotoxy(25,12);
			cout<<"Year: ";
			cin>>stud.year;
			gotoxy(10,14);
			cout<<"Marks entry: Hindi: ";
			cin>>stud.mhin;
			gotoxy(23,15);
			cout<<"English: ";
			cin>>stud.meng;
			gotoxy(23,16);
			cout<<"Maths: ";
			cin>>stud.mmat;
			gotoxy(23,17);
			cout<<"Science: ";
			cin>>stud.msci;
			gotoxy(23,18);
			cout<<"Social Studies: ";
			cin>>stud.msst;
			gotoxy(10,20);
			cout<<"Record Has been saved";
			getch();
			stud.pcent=((stud.mhin+stud.meng+stud.mmat+stud.msci+stud.msst)*100)/(mm.hin+mm.eng+mm.sst+mm.sci+mm.mat);
			fwrite(&stud,recsize,1,file);
			clrscr();
			gotoxy(10,10);
			cout<<"Press ENTER to add another record.\n";
			gotoxy(10,11);
			cout<<"Press ESCAPE to go back.\n";
			getkey();
			switch(key)
			{
				case ENTER:
					ctr=0;
					break;
				case ESCAPE:
					ctr=1;
			}
	}
}












void disprec()
{
	rewind(file);
	while(fread(&stud,recsize,1,file)==1)
	{
		cout<<"\n\t"<<stud.roll<<"\n\t"<<stud.grade<<"\n\t";
		puts(stud.fname);
	}
}
void drawbox(int boxa,int boxb,int boxc,int boxd)
{
	int i,j;
	char s=205,c=201,q=186,d=187,e=200,f=188;
	gotoxy(boxa,boxb);
	cout<<c;
	gotoxy(boxa,boxd-1);
	cout<<e;
	gotoxy(boxc-1,boxb);
	cout<<d;
	gotoxy(boxc-1,boxd-1);
	cout<<f;
	for(i=boxa+1;i<boxc-1;i++)
	{
		for(j=boxb;j<boxd;j++)
		{
			gotoxy(i,j);
			cout<<s;
		}
	}
	for(i=boxa;i<boxc;i++)
	{
		for(j=boxb+1;j<boxd-1;j++)
		{
			gotoxy(i,j);
			cout<<q;
		}
	}
	for(i=boxa+1;i<boxc-1;i++)
	{
		for(j=boxb+1;j<boxd-1;j++)
		{
			gotoxy(i,j);
			cout<<" ";
		}
	}
	gotoxy(boxa+1,boxb+1);

}
void menu_main(int ch)
{
	textcolor(7);
	textbackground(0);
	int q;
	clrscr();
	
		gotoxy(30,1);
		cout<<"Welcome to KPercent5.0\n\n\n\n\n";
		cout<<"                         Add Records\n\n";
		cout<<"                         Display Records\n\n";
		cout<<"                         Search Records\n\n";
		cout<<"                         Set defaults\n\n";
		cout<<"                         Exit\n\n";
		
	
	gotoxy(11,2*(ch)+4);
	cout<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s;
	gotoxy(41,2*(ch)+4);
	cout<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s;
	gotoxy(10,24);
	cout<<"Press UP and DOWN ARROWS to navigate. Press ENTER to select";
}
void execute_main(int ch)
{
	clrscr();
	switch(ch)
	{
		case 1:
			addrec();
			break;
		case 2:
			disprec();
			break;
		case 3:
			cout<<"Search";
			break;
		case 5:
			exit(1);
		case 4:
			cout<<"Set defaults";
	}
	getch();
}
void getkey()
{
	key=getch();
	if(key==0)
		key=getch();
}
void main()
{
int i=1;
clrscr();
file=fopen("F:\PROFILES.DAT","rb+");
if(file==NULL)
{
	file=fopen("F:\PROFILES.DAT","wb+");
	if(file==NULL)
	{
		puts("CANNOT OPEN THE SOURCE FILE.....!!!!!");
		getch();
		exit(2);
	}
}
else
{
	cout<<"FILE OPENED";
	getch();
}
recsize=sizeof(stud);


while(1)
{
	menu_main(i);
	getkey();
	switch(key)
	{
		case UARROW:
			i=i-1;
			break;
		case DARROW:
			i=i+1;
			break;
		case ENTER:
			execute_main(i);
			break;
	}
	if(i==0)
		i=5;
	if(i==6)
		i=1;
}
}

Recommended Answers

All 3 Replies

Make sure the denominator in that equation is not 0. Print out the value of the variables so that you can verify them.

Also, get rid of those gets() calls such as on line 36 because they can let you enter more characters than the array can hold; the extra characters will be written to some unknown memory location and possibly crash the program. Replace gets() with getline() since you are writing c++ program.

Ditto what AD said.

Everytime I run this program and enter a new record, it gets terminated on these statements:

It'll terminate (I assume that means "crash") on one of the two statements. Your first step is to figure out which one. Divide by zero would make it crash on the first.

Thankyou.. I checked it and initialies the values so that the denominator is not 0. But in which header file is getline()defined..

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.