This is the program I wrote for my CS project. Compiler is TurboC++. I initially added a lot of garbage code in it and am having a hard time cleaning it up. Can you guys please suggest improvements in the code below. Thanks in advance.

//STUDENT MANAGEMENT
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
class student
{
	char name[50];
	int r_no;
	int classs;
	float marks;
	char grade;
	public:
	 int modify1();
	 void modification();
	 void getdata();
	 void display();
	 void search();
	 int getrno()
	 {
		return r_no;
	 }
};
student ob1[10],ob2,ob3[10],ob4;
void student::getdata()
{
		char ch;
		cin.get(ch);
		cout<<"\n\n\t\t\tEnter the roll number: ";
		cin>>r_no;
		cout<<"\n\t\t\tEnter name:";
		cin>>name;
		cout<<"\n\t\t\tEnter class:";
		cin>>classs;
		cout<<"\n\t\t\tEnter the marks:";
		cin>>marks;
		if(marks>=75)       grade='A';
		else if(marks>=60)  grade='B';
		else if(marks>=50)  grade='C';
		else if(marks>=40)  grade='D';
		else grade='F';
}
void student::display()
{
		cout<<" \nRoll no :"<<r_no;
		cout<<" \nName:"<<name ;
		cout<<"\nclass:"<<classs<<"\n"<<name<<"\t..has passed exams with  "<<marks<<"\t and grade:  "<<grade;
}

void student::modification()
{
	char clas[4];
	cout<<" Roll no:"<<r_no<<"\n";
	cout<<"Name: "<<name<<"\t class:"<<clas<<"\t marks"<<marks<<"\n";
	cout<<"\n Enter new details";
	char nm[20]="  " , cl[4]="  ";
	float mks;
	cout<<"\n new name:(press . to retain old one) ";
	cin>>nm;
	cout<<"\n new class :(press . to retain old one) ";
	cin>>cl;
	cout<<"\n new marks:(press -1 to retain old one) ";
	cin>>mks;
	if(strcmp(nm,".")!=0)
		strcpy(name,nm);
	if (strcmp(cl,".")!=0)
		strcpy(clas,cl);
	if(mks!=-1)
	{
		marks=mks;
		if(marks>=75)       grade='A';
		else if(marks>=60)  grade='B';
		else if(marks>=50)  grade='C';
		else if(marks>=40)  grade='D';
		else grade='F';

	}

}
void main()
{
 clrscr();
 last:;
 clrscr();
int rn,tr,n,p,i=0,record=0;
long pos;
char ans;
do
{
clrscr();
cout<<"\n\n\n\t\t\tMenu for displaying\entering data\n\n";
cout<<"\t\t\t1.Enter the details\n\n";
cout<<"\t\t\t2.Displaying the details\n\n";
cout<<"\t\t\t3.searching details\n\n";						cout<<"\t\t\t4.Modification \n\n";
cout<<"\t\t\t5.Deletion\n\n";
cin>>tr;
clrscr();
switch(tr)

	{
	case 1:
	{
	cout<<"\n\t\t\t Enter the NOs of students to be added\n";
	 cin>>n ;
	p=n;
	ofstream cpp("name.txt",ios::app);
	for( i=0;i<n;i++)
	{
		ob1[i].getdata();
		cpp.write((char*)&ob1[i],sizeof(ob1[i]));
		record++;
	}
	cpp.close();
    clrscr();
	cout<<"\n\n\n Do you want to continue(y/n)";
	cin>>ans;
	break ;
	}
	case 2:
	{

		ifstream cpp("name.txt",ios::app);
		cpp.seekg(0);
		cout<<"\nHow many details u have entered?\n";
		cin>>p;
		{
		for(i=0;i<p;i++)
		 {
		    cpp.read((char*)&ob1[i],sizeof(ob1[i]));
			ob1[i].display();
			cout<<"\n\n\n";
		}
		}
		cout<<"\n\n\t\t\t\n Do you want to go back to main menu?(y/n)";
		cin>>ans;
		cpp.close();
		break;
	}
	case 3:
	{
	char found='n';
	ifstream cpp("name.txt",ios::in);
	cout<<"\t\t\tEnter the roll no. to search \n";
	cin>>rn;
	cpp.seekg(0);
	while(!cpp.eof())
	{
		pos=cpp.tellg();
			cpp.read((char*)&ob2,sizeof(ob2));
			if(ob2.getrno()==rn)
				{
					found='y';
					ob2.display();
					break;
				}
			else cpp.seekg(pos,ios::cur);
	}
		    if(cpp.eof()) cout<<"End of file";
					if(found=='n')
				cout<<"\nRecord not found!\n";
				cout<<"\n\t\t\t\n\n Do you want to go back to main menu?(y/n)";
				cin>>ans;
				break;
	}
	case 4:
	{
	char found='f';
	ofstream cpp("name.txt",ios::out||ios::app);
	ifstream cp("name.txt",ios::in||ios::app);
	cout<<"\t\t\tEnter the roll no. to be modified \n";
	cin>>rn;
	while(!cpp.eof())
	{
		pos=cp.tellg();
		cp.read((char*)&ob2,sizeof(ob2));
		if(ob2.getrno()==rn)
		{
			ob2.modification();
			cp.seekg(pos);
			cpp.write((char*)&ob2,sizeof(ob2));
			found='t';
			break;
		}
	}
	if(found=='f')
       cout<<"\nrecord not found\n";
	cpp.close();
	cp.close();
	ifstream cppp("name.txt",ios::in||ios::app);
	cppp.seekg(0);
	cout<<"\nWhich file you have modified(number)?\n";
	cin>>p;
	cout<<"\nNow the file contains\n";
	{
	     clrscr();
		for(i=0;i<p;i++)
		{
			cppp.read((char*)&ob3[i],sizeof(ob3[i]));
			ob3[i].display();
		}
	}
	cout<<"\n\t\t\t\n Do you want to go back to main menu?(y/n)";
	cin>>ans;
	clrscr();
	cpp.close();
	break;
    }
	case 5:
	{
	char found='f',confirm='n';
	ifstream cpp("name.txt",ios::in||ios::app);
	ofstream cp("temp.txt",ios::out||ios::app);
	cout<<"\nEnter the roll No. of the student to be deleted....\n";
	cin>>rn;
	while(!cpp.eof())
	{
		cpp.read((char*)&ob2,sizeof(ob2)) ;
		if(ob2.getrno()==rn)
		{
			ob2.display();
			found='t';
			cout<<"\nAre you sure want to delete this record?\n";
			cin>>confirm;
			if(confirm=='n')
			cp.write((char*)&ob2,sizeof(ob2)) ;
		}
		else
		cp.write((char*)&ob2,sizeof(ob2)) ;
	}
	if(found=='f')
	cout<<"\nRecord not found!\n";
	cpp.close();
	cp.close();
	remove("name.txt");
	rename("temp.txt","stu.txt");
	cpp.open("name.txt",ios::in);
	cpp.seekg(0);
	cout<<"\nWhich file you have Deleted(number)?\n";
	cin>>p;
	cout<<"\nNow the file contains\n";
	for(int i=0;i<p;i++)
	{
		cpp.read((char*)&ob3[i],sizeof(ob3[i]));
		if(cpp.eof()) break;
		ob4.display();

	}
	cpp.close();
	break;
    }

	}

	}
	while(ans=='Y'||ans=='y');
	getch();
}

Recommended Answers

All 5 Replies

Your class (.h) and functions to the class (.cpp) should be in separate files and then included in main (:

Your class (.h) and functions to the class (.cpp) should be in separate files and then included in main (:

Thanks, but I have no experience in seperating codes in two files. How can I do that (School didn't teach me that).

Me personally, I wouldn't have like inputs etc inside the class methods e.g:

void student::getdata()
{
		char ch;
		cin.get(ch);
		cout<<"\n\n\t\t\tEnter the roll number: ";
		cin>>r_no;
		cout<<"\n\t\t\tEnter name:";
		cin>>name;
		cout<<"\n\t\t\tEnter class:";
		cin>>classs;
		cout<<"\n\t\t\tEnter the marks:";
		cin>>marks;
		if(marks>=75)       grade='A';
		else if(marks>=60)  grade='B';
		else if(marks>=50)  grade='C';
		else if(marks>=40)  grade='D';
		else grade='F';
}

This should be in main, and, your class should store just the pay roll number, name, class and marks and then another method that calculates what the mark was and returns this accordingly.

Also, your class should always be in capital letter "Student" not "student"

Dude, thats from icbse.com isnt it ?? I saw the exact same code - found it shit and left it... You shouldnt be copying code and calling it your own !!!

P.S - Best way to improve a code - MAKE IT YOURSELF !!!!
That way not only are u satisfied but also the code is perfected to meet ur satisfaction!!

commented: Good advice :) +3

-------------------- Closed ---------------------

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.