Vous êtes sur la page 1sur 10

12/9/2015

C++programtocreateastudentdatabase|MyComputerScience

[DBMS]

[SQL]

Home

[NETWORKING]

SoftwarePractical

[ELECTRONICS]

HardwarePractical

[DIGITAL]

DataStructure

[DISCRETEMATH]

Architecture

[AUTOMATA]

FreeEBooks

[ABOUTUS]

[CAREER]

Forums

QuickSearch

XEROACCOUNTING

SOFTWARE

Quick, Easy & Free to Sign Up. Used by 200,000+ Aussie Businesses.

Adsby Google

C++Program

C++Programming

C++Code

StudentDatabase

C++ program to create a student database


FiledUnder:C++Assignments

Subscribetogetdailyupdates,solve
assignmentsandprojects

MonthlySubscription
Newsletter:$10.00USDmonthly

MyComputerScie
1,863likes

LikePage

Share

Bethefirstofyourfriendstolikethis

Start Download Now

Convert From Doc to PDF, PDF to Doc


Simply With The Free On-line App!

C om ple te ly Solv e dC ,C ++Pr ogr a m s A s s ignm e nt.

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

1/10

12/9/2015

C++programtocreateastudentdatabase|MyComputerScience
ProblemStatement:
WriteaC++programtocreateastudentdatabase,studentrecordsshouldbewrittenintoafileandshould
bereadfromthatfilewheneverinformationisneeded.

LifetimeMembership
Plottingofaunitstepstartingatt=2and
goingtoright.
CprogramtoimplementCircularQueue

LogicalApproach:
The above program is done using an Abstract Data Type (ADT) called class. A class is the mechanism to
createobjects.Intheprogram,aclassnamedstudentiscreated.Allthestudentrecordsthatareenteredby
user are stored in a file Student.dat(We can give any name) so that the records are not lost after the
programisexecuted.

TheConceptofCircularQueues
CProgramtoimplementqueueusing
Linkedlist
Cprogramtoimplementaqueueusing
anarray
TheConceptofQueues

Itcontainsthefollowingdatamembers:
&#61548roll_no
name[]
dept_name[]
marks[]

Cprogramforimplementationofastack
usingthelinkedlist
Cprogramtoimplementastackusingan
array
ConceptofStack
Computerdatastructuresandinteractive
datastructurevisualizations

Itcontainsthefollowingmemberfunctions:
get_input()Itisusedtogetalltheinputs.
print_data()Itisusedtoprintalltheinformation.
calculate()Thisfunctionisusedtocalculateastudentsgradeonthefollowingbasis:

Calculationofgrade:

SignalandSystem:SolvedMATLAB
AssignmentII
SignalandSystem:SolvedMATLAB
AssignmentI
CProgramtoacceptdataandstorethe
givendataintofileprintthedata.

1. If average marks is greater than or equal to 90 then grade = EX and print High first class with
distinction.

2. If average marks is greater than or equal to 80 but less than 90 then grade = A and print High first
class.

3.Ifaveragemarksisgreaterthanorequalto70butlessthan80thengrade=BandprintFirstclass.

4.Ifaveragemarksisgreaterthanorequalto60butlessthan70thengrade=CandprintSecondclass.

5.Ifaveragemarksisgreaterthanorequalto50butlessthan60thengrade=DandprintThirdclass.

6.Elsethestudentisfail.

&#61548filein()ItisusedtoreadthestudentrecordsfromfileStudent.dat.
&#61548fileout()ItisusedtowritethestudentrecordsintofileStudent.dat.Ifpreviousdataexists
newrecordsareappended.

thesumofindividualdigits.|My
AvisitorfromUnitedStatesviewed
ComputerScience"40minsago
"Cprogramtocounttotalnumberof
lines,wordsandcharactersinafile|
MyComputerScience"43minsago
AvisitorfromTrenton,Michigan
viewed"Cprogramtoimplement
LAGRANGE'SINTERPOLATION
METHOD.|MyComputerScience"
AvisitorfromUnitedStatesviewed
49minsago
"CprogramtoimplementGauss
Seidelmethodtosolveasystemof
equations.|MyComputerScience"
AvisitorfromDhakaviewed"C
52minsago
programtoacceptastringandcount
thenumberofsmallcase
letters,capitalcase
letters,vowels,blankspacesand
AvisitorfromParanava,Parana
specialcharacters.|MyComputer
viewed"Cprogramtoimplement

&#61548countstudent()Itisusedtocountnumberofstudentsrecordedinthefile.

User can insert number of student records according to his or her choice which are written into the file
Student.dat. The file is opened in append mode so that previous data(if exists) are not deleted and new
records are inserted at the end. After the calculation of average mark and grade for each student, the
studentrecordsarereadfromthefileandareprintedinthereverseorder.

Algorithm:
CreatinganADTwithnamestudent

Step1:CreatinganADTstudent

Step2:DeclaringprivateDatamembers
introll_no//Declaringaninteger
charname[20]//Declaringacharacterarrayofsize20
chardept_name[40]//Declaringacharacterarrayofsize40
intmarks[5]//Declaringacharacterarrayofsize5

Step3:Declaringpublicmemberfunctions
get_input()

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

2/10

12/9/2015

get_input()

C++programtocreateastudentdatabase|MyComputerScience

calculate()
print_data()
filein()
fileout()
countstudent()

Thisfunctionisusedtogetalltheinputs

get_input()
{
Readroll_no
Readname
Readdept_name
for(inti=1to5)
Readmarks[i]

Thisfunctionisusedtocalculateastudent'sgrade

calculate()
{
for(i=1to5)
sum=sum+marks[i]
avg=sum/5
if(avg>=90)
PrintEX,High1stClasswith***DISTINCTION***
elseif(avg<90ANDavg>=80)
PrintA,High1stClass
elseif(avg<80ANDavg>=70)
PrintB,1stClass
elseif(avg<70ANDavg>=60)
PrintC,2ndClass
elseif(avg<60ANDavg>=50)
PrintD,3rdClass
else
PrintFail
}

Thisfunctionisusedtoprintalltheinformation

print_data()

{
Printroll_no
Printname
Printdept_name
for(inti=1to5)
Printmarks[i]
callcalculate()toprintgrade

ThisfunctionisusedtoreadthestudentrecordsfromfileStudent.dat

filein()
{
Createastreamusingifstream

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

3/10

12/9/2015

C++programtocreateastudentdatabase|MyComputerScience
Createastreamusingifstream
OpenthefileStudent.datinbinarymode
Seekget(input)pointertospecifiedrecord
readthespecifiedrecord
}

This function is used to write the student records into file Student.dat. If previous data exists
newrecordsareappended

fileout()
{
Createastreamusingofstream
OpenthefileStudent.datinbinarymodeandappendmode
get_input()
writethestudentrecordinthefile
}

Thisfunctionisusedtocountnumberofstudentsrecordedinthefile

countstudent()
{
Createastreamusingifstream
OpenthefileStudent.datinbinarymode
Seekget(input)pointertoendofthefile
Dividecurrentpositionofthegetpointerbysizeofastudentrecordandreturnthevalue
}

MainProcedure

main()
{
students
do
{
Print"Enterdataforstudent"
s.fileout()
Print"Enteranother(Y/N):"
Readchoiceinch
}while(ch='Y'ORch==y')
n=s.countstudent()
fori=n1to0)
{
s.filein(i)
s.print_data()
}
}

Programlisting:
//C++programtocreateastudentdatabase,studentrecordsshouldbewrittenintoafileandshouldberead
fromthatfilewheneverinformationisneeded

#include<iostream>
#include<fstream>

usingnamespacestd
//Creatingaclassstudent
classstudent
{
private:

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

4/10

12/9/2015

C++programtocreateastudentdatabase|MyComputerScience
introll_no
charname[100]
chardept_name[100]
intmarks[5]
public:
voidget_input()
voidcalculate()
voidprint_data()
voidfilein(int)
voidfileout()
intcountstudent()
}
//ThisfunctionisusedtoreadthestudentrecordsfromfileStudent.dat
voidstudent::filein(intpn)
{
ifstreaminfile
infile.open("Student.dat",ios::binary)
infile.seekg(pn*sizeof(student))
infile.read((char*)this,sizeof(*this))
}
//ThisfunctionisusedtowritethestudentrecordsintofileStudent.dat.Ifpreviousdataexistsnewrecords
areappended
voidstudent::fileout()
{
ofstreamoutfile
outfile.open("Student.dat",ios::app|ios::binary)
get_input()
outfile.write((char*)this,sizeof(*this))
}
//Thisfunctionisusedtocountnumberofstudentsrecordedinthefile
intstudent::countstudent()
{
ifstreaminfile
infile.open("Student.dat",ios::binary)
infile.seekg(0,ios::end)
intn=infile.tellg()/sizeof(student)
returnn
}
//Thisfunctionisusedtogetalltheinputs
voidstudent::get_input()
{
cout<<"\nEnterrollno:"
cin>>roll_no
cout<<"\nEntername:"
getchar()
cin.getline(name,100)
cout<<"\nEnterdepartmentname:"
cin.getline(dept_name,100)
cout<<"\nEntermarksinHons.Paper1:"
cin>>marks[0]
cout<<"\nEntermarksinHons.Paper2:"
cin>>marks[1]
cout<<"\nEntermarksinPassPaper1:"
cin>>marks[2]
cout<<"\nEntermarksinPassPaper2:"
cin>>marks[3]
cout<<"\nEntermarksinCompulsuryPaper:"
cin>>marks[4]
for(inti=0i<5i++)

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

5/10

12/9/2015

C++programtocreateastudentdatabase|MyComputerScience
{
if(marks[i]>100||marks[i]<0)
{
cout<<"Invalidinputmarksgiven!!!!!\n"
system("PAUSE")
exit(0)

}
}
}
//Thisfunctionisusedtocalculateastudent'sgrade
voidstudent::calculate()
{
intsum=0,i,avg
for(i=0i<5i++)
sum+=marks[i]
avg=sum/5
cout<<"\n\n"<<"grade:"
if(avg>=90)
cout<<"EX,"<<"High1stClass***"
elseif(avg<90&&avg>=80)
cout<<"A,"<<"High1stClass"
elseif(avg<80&&avg>=70)
cout<<"B,"<<"1stClass"
elseif(avg<70&&avg>=60)
cout<<"C,"<<"2ndClass"
elseif(avg<60&&avg>=50)
cout<<"D,"<<"3rdClass"
else
cout<<"Fail!!!!!"
cout<<"\n.........."
}
//Thisfunctionisusedtoprintalltheinformation
voidstudent::print_data()
{
cout<<"\n\nRoll:"<<roll_no
cout<<"\n\nName:"<<name
cout<<"\n\nDepartment:"<<dept_name
cout<<"\n\nMarksinHons.Paper1:"<<marks[0]
cout<<"\n\nMarksinHons.Paper2:"<<marks[1]
cout<<"\n\nMarksinPassPaper1:"<<marks[2]
cout<<"\n\nMarksinPassPaper2:"<<marks[3]
cout<<"\n\nMarksinCompulsuryPaper:"<<marks[4]
calculate()
}

intmain()
{
students
intn
charch
do
{
cout<<"Enterdataforstudent\n\n"
s.fileout()//Readfromuserandwritetofile
cout<<"Enteranother(Y/N):"
cin>>ch
}while(ch=='Y'||ch=='y')

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

6/10

12/9/2015

}while(ch=='Y'||ch=='y')

C++programtocreateastudentdatabase|MyComputerScience

n=s.countstudent()
for(inti=n1i>=0i)
{
cout<<"\n\nStudent"<<i+1<<"\n"
s.filein(i)//Readfromfile
s.print_data()//Printrecordsinreverseorder
}
return0
}

Output:
Enterdataforstudent

Enterrollno:190
Entername:VijayMalakar
Enterdepartmentname:History
EntermarksinHons.Paper1:45
EntermarksinHons.Paper2:55
EntermarksinPassPaper1:67
EntermarksinPassPaper2:65
EntermarksinCompulsuryPaper:43

Enteranother(Y/N):y
Enterdataforstudent

Enterrollno:230
Entername:AmarMalhotra
Enterdepartmentname:English
EntermarksinHons.Paper1:78
EntermarksinHons.Paper2:76
EntermarksinPassPaper1:23
EntermarksinPassPaper2:33
EntermarksinCompulsuryPaper:44

Enteranother(Y/N):y
Enterdataforstudent

Enterrollno:345
Entername:LalitaSaha
Enterdepartmentname:Mathematics
EntermarksinHons.Paper1:65
EntermarksinHons.Paper2:89
EntermarksinPassPaper1:46
EntermarksinPassPaper2:76
EntermarksinCompulsuryPaper:43

Enteranother(Y/N):n

Student3

Roll:345

Name:LalitaSaha

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

7/10

12/9/2015

C++programtocreateastudentdatabase|MyComputerScience
Name:LalitaSaha
Department:Mathematics
MarksinHons.Paper1:65
MarksinHons.Paper2:89
MarksinPassPaper1:46
MarksinPassPaper2:76
MarksinCompulsuryPaper:43
grade:C,2ndClass
..........

Student2

Roll:230

Name:AmarMalhotra

Department:English

MarksinHons.Paper1:78

MarksinHons.Paper2:76

MarksinPassPaper1:23

MarksinPassPaper2:33

MarksinCompulsuryPaper:44

grade:D,3rdClass
..........

Student1

Roll:190

Name:VijayMalakar

Department:History

MarksinHons.Paper1:45

MarksinHons.Paper2:55

MarksinPassPaper1:67

MarksinPassPaper2:65

MarksinCompulsuryPaper:43

grade:D,3rdClass
..........

Discussions:
&#61548 Namespaces allow to group entities like classes,objects and functions under a name. This way
theglobalscopecanbedividedinsubscopes,eachonewithitsownname.

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

8/10

12/9/2015

C++programtocreateastudentdatabase|MyComputerScience
&#61548AllthefilesintheC++standardlibrarydeclareallofitsentitieswithinthestdnamespace.That
iswhywehavegenerallyincludedtheusingnamespacestdstatementinallprogramsthatusedanyentity
definediniostream.

&#61548Wecanopenfilesusingconstructororbyusingopen()function.Thesyntaxis:
filestreamclassstreamobject
streamobject.open(filename,mode)

&#61548Thefilemodeparametercantakeoneormoreparametersdefinedintheclassios.
Ios::app is the append mode which allows us to add data to the end of the file only. In this case,a file is
createdbythespecifiednameifitdoesnotexist.

&#61548ThemodecancombinetwoormoreparametersusingthebitwiseORoperator.

&#61548 The functions read() and write() handle the data in binary form. The binary format is more
accurateforstoringrecordsastheyarestoredintheexactinternalrepresentation

Backtomaindirectory:

C++Assignment

SoftwarePractical

Mycomputerscience
Follow

+1

+ 149

Search

Name
EmailID

Send

GetFreeProgrammingTutorials
andSolvedassignments

Enteryouremail...
Subscribe

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

9/10

12/9/2015

Home|AboutUs|AdvertiseWithUs|

C++programtocreateastudentdatabase|MyComputerScience

|PrivacyPolicy|Assignment|Desclaimer

Mycomputerscience.net Copyright20112013.AllRightsReserved.
WebsitedesignedanddevelopedbySambaranChakraborty

http://www.mycomputerscience.net/2012/08/cplusplusstudentdatabase.html

10/10

Vous aimerez peut-être aussi