Tuesday 23 June 2015

Simple mini projects in C++

1.Student reportcard system in C++:

Source code:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<fstream.h>
#include<iomanip.h>
// CLASS USED IN PROJECT
class student
{
int rno;
char name[50],dept[10];
char s1[50],s2[50],s3[50],s4[50],s5[50],s6[50];
float per;
char grade;
int s1_marks,s2_marks,s3_marks,s4_marks,s5_marks,s6_marks;
void calculate()
{
per=(s1_marks+s2_marks+s3_marks+s4_marks+s5_marks+s6_marks)/6.0;
if(per>=91 && per<=100)
grade='S';
else if(per>=81 && per<=90)
grade='A';
else if(per>=71 && per<=80)
grade='B';
else if(per>=61 && per<=70)
grade='C';
else if(per>=55 && per<=60)
grade='D';
else if(per>=50 && per<=54)
grade='E';
else
grade='U';
}
public:
void getdata()
{
cout<<"\n Enter your Department";
cin>>dept;
cout<<"\nEnter The roll number of student ";
cin>>rno;
cout<<"\n\nEnter The Name of student ";
gets(name);
cout<<"\n enter your subjects one by one";
cin>>s1;
cout<<"\n";
cin>>s2;
cout<<"\n";
cin>>s3;
cout<<"\n";
cin>>s4;
cout<<"\n";
cin>>s5;
cout<<"\n";
cin>>s6;
cout<<"\n";
cout<<"Enter your marks in "<<s1;
cin>>s1_marks;
cout<<"Enter your marks in "<<s2;
cin>>s2_marks;
cout<<"Enter your marks in "<<s3;
cin>>s3_marks;
cout<<"Enter your marks in "<<s4;
cin>>s4_marks;
cout<<"Enter your marks in "<<s5;
cin>>s5_marks;
cout<<"Enter your marks in "<<s6;
cin>>s6_marks;
calculate();
}
void showdata()
{
cout<<"\nRoll number of student : "<<rno;
cout<<"\nName of student :"<<name;
cout<<"\nMarks in "<<s1<<"     "<<s1_marks;
cout<<"\nMarks in "<<s2<<"     "<<s2_marks;
cout<<"\nMarks in "<<s3<<"     "<<s3_marks;
cout<<"\nMarks in "<<s4<<"     "<<s4_marks;
cout<<"\nMarks in "<<s5<<"     "<<s5_marks;
cout<<"\nMarks in "<<s6<<"     "<<s6_marks;
cout<<"\nPercentage of student is :\t"<<setprecision(2)<<per;
cout<<"\nGrade of student is :  "<<grade;
}
void show_tabular()
{
cout<<rno<<"\t  "<<name<<"\t   "<<s1_marks<<" \t  "<<s2_marks<<"\t"
<<s3_marks<<"    "<<s4_marks<<"    "<<s5_marks<<"     "<<s6_marks<<"    "<<setprecision(1)<<per<<"          "<<grade<<endl;

}
int retrno()
{ return rno; }
}; //class ends herE
fstream fp;
student st;

// function to write in file

void write_student()
{
fp.open("student.dat",ios::out|ios::app);
st.getdata();
fp.write((char*)&st,sizeof(student));
fp.close();
cout<<"\n\nstudent record Has Been Created ";
getch();
}
// function to read all records from file
void display_all()
{
clrscr();
cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
fp.open("student.dat",ios::in);
while(fp.read((char*)&st,sizeof(student)))
{
st.showdata();
cout<<"\n\n====================================\n";
getch();
}
fp.close();
getch();
}

// function to read specific record from file
void display_sp(int n)
{
int flag=0;
fp.open("student.dat",ios::in);
while(fp.read((char*)&st,sizeof(student)))
{
if(st.retrno()==n)
{
clrscr();
st.showdata();
flag=1;
}
}
fp.close();
if(flag==0)
cout<<"\n\nrecord not exist";
getch();
}
// function to modify record of file
void modify_student()
{
int no,found=0;
clrscr();
cout<<"\n\n\tTo Modify ";
cout<<"\n\n\tPlease Enter the roll number of student";
cin>>no;
fp.open("student.dat",ios::in|ios::out);
while(fp.read((char*)&st,sizeof(student)) && found==0)
{
if(st.retrno()==no)
{
st.showdata();
cout<<"\nPlease Enter The New Details of student"<<endl;
st.getdata();
int pos=-1*sizeof(st);
fp.seekp(pos,ios::cur);
fp.write((char*)&st,sizeof(student));
cout<<"\n\n\t Record Updated";
found=1;
}
}
fp.close();
if(found==0)
cout<<"\n\n Record Not Found ";
getch();
}
// function to delete record of file
void delete_student()
{
int no;
clrscr();
cout<<"\n\n\n\tDelete Record";
cout<<"\n\nPlease Enter The roll number of student You Want To Delete";
cin>>no;
fp.open("student.dat",ios::in|ios::out);
fstream fp2;
fp2.open("Temp.dat",ios::out);
fp.seekg(0,ios::beg);
while(fp.read((char*)&st,sizeof(student)))
{
if(st.retrno()!=no)
{
fp2.write((char*)&st,sizeof(student));
}
}
fp2.close();
fp.close();
remove("student.dat");
rename("Temp.dat","student.dat");
cout<<"\n\n\tRecord Deleted ..";
getch();
}
// function to display all students grade report
void class_result()
{
clrscr();
fp.open("student.dat",ios::in);
if(!fp)
{
cout<<"ERROR!!! FILE COULD NOT BE OPEN\n\n\n Go To Entry Menu to create File";
cout<<"\n\n\n Program is closing ";
getch();
exit(0);
}
cout<<"\n\n\t\t\t\tALL STUDENTS RESULT \n\n";
cout<<"===========================================================================\n";
cout<<"Roll No.  Name     S1     S2    S3    S4    S5     S6    %            Grade\n";
cout<<"===========================================================================\n";
while(fp.read((char*)&st,sizeof(student)))
{
st.show_tabular();
}
fp.close();
getch();
}
// function to display result menu
void result()
{
int ans,rno;
char ch;
clrscr();
cout<<"\n\n\nRESULT MENU";
cout<<"\n\n\n1. Class Result\n\n2. Student Report Card\n\n3. Back to Main Menu";
cout<<"\n\n\nEnter Choice (1/2)? ";
cin>>ans;
switch(ans)
{
case 1 : class_result();break;
case 2 : {
do{
clrscr();
char ans;
cout<<"\n\nEnter Roll Number Of Student : ";
cin>>rno;
display_sp(rno);
cout<<"\n\nDo you want to See More Result (y/n)?";
cin>>ans;
}while(ans=='y'||ans=='Y');
break;
}
case 3: break;
default: cout<<"\a";
}
}
// INTRODUCTION FUNCTION
void intro()
{
clrscr();
cout<<"\n\n\n\n\n\n\n\n\n";
cout<<"\t\t\tSTUDENT";
cout<<"\tREPORT CARD";
cout<<" PROJECT ";
cout<<"\n\n";
cout<<"\t\t\t\tDEVELOPED  BY\n";
cout<<"\n";
cout<<"\t\t\tYour Name";
getch();
}
// ENTRY / EDIT MENU FUNCTION
void entry_menu()
{
clrscr();
char ch2;
cout<<"\n\n\n\tENTRY MENU";
cout<<"\n\n\t1.CREATE STUDENT RECORD";
cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORDS";
cout<<"\n\n\t3.SEARCH STUDENT RECORD ";
cout<<"\n\n\t4.MODIFY STUDENT RECORD";
cout<<"\n\n\t5.DELETE STUDENT RECORD";
cout<<"\n\n\t6.BACK TO MAIN MENU";
cout<<"\n\n\tPlease Enter Your Choice (1-6) ";
ch2=getch();
switch(ch2)
{
case '1': clrscr();
write_student();
break;
case '2': display_all();break;
case '3':
int num;
clrscr();
cout<<"\n\n\tPlease Enter The roll number ";
cin>>num;
display_sp(num);
break;
case '4': modify_student();break;
case '5': delete_student();break;
case '6': break;
default:cout<<"\a";entry_menu();
}
}
// THE MAIN FUNCTION OF PROGRAM
void main()
{
char ch;
clrscr();
intro();
do
{
clrscr();
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. RESULT MENU";
cout<<"\n\n\t02. ENTRY/EDIT MENU";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tPlease Select Your Option (1-3) ";
ch=getch();
switch(ch)
{
case '1':
clrscr();
result();
break;
case '2': entry_menu();
break;
case '3':exit(0);
default :cout<<"\a";
}
}while(ch!='3');
}

2. Guessing game in C++:

Source code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<dos.h>

void main()
{
textbackground(WHITE);
textcolor(RED);
      clrscr();

 char ch,a[20],ch2;
 int num=100,rnum,guess,count,ch1,c=0;
 cout<<"\n\t\t\t******************************"<<"\n\t\t\t******************************";
 cout<<"\n\t\t\t**                          **"<<"\n\t\t\t** Welcome To High/Low Game **"<<"\n\t\t\t** ________________________ **";
 cout<<"\n\t\t\t**                          **"<<"\n\t\t\t******************************"<<"\n\t\t\t******************************";
 cout<<"\n\n\n\n------------"<<"\n Main  Menu"<<"\n------------"<<"\n\n\n 1.Rules\n\n 2.Play\n\n 3.Exit"<<"\n\n Enter your choice(1-3):";
 cin>>ch1;

 switch(ch1)
 {
  case 1:
    {
    clrscr();
    cout<<"\n\n---------------------"<<"\n General Description"<<"\n---------------------";
    cout<<"\n\n1.The Computer picks a random number from 0 to 99.\n  You must try to guess the number.";
    cout<<"\n\n2.The Computer ask you to enter your guess.You have 8\n  chances to guess the number.";
    cout<<"\n\n3.The computer checks the number,if your guess is\n  less than computer's numer than it will show a message\n  that your guess is low and ask for higher number.";
    cout<<"\n\n4.If your guess is higher than the computer's number\n  than computer will show a message that your guess is high and\n  ask for lower number.";
    cout<<"\n\n5.If your guess is equal to the computer's number,than\n  computer will print a message congratulating you\n  and will ask you to play the game again or exit.";
    cout<<"\n\n6.If you are unable to guess the number than the computer\n  will a sorry message and ask you to play the game again or exit.";
    D: cout<<"\n\n\n  Press * to goto Main Menu:";
    cin>>ch2;
    if(ch2=='*')
    {
     ch2='#';
     //goto A;
    }
    /*else
     goto B; */
   }
    break;

 case 2:
  {
   clrscr();
   cout<<"\n\n\nEnter Your Name:";
   cin>>a;
   cout<<"\n\n";
   for(int i=0;i<=100;i+=20)
   {
    cout<<"Loading......."<<i<<"%\r";
    sleep(1);
   }
   cout<<"\n\nHi "<<a<<".........!!!!";

       A: if(c>0)
    clrscr();
   randomize();
   rnum=random(num);
   cout<<"\n\nEnter Your Guess:";
   cin>>guess;
   count=8;

     B: while(guess!=rnum)
{
    count--;
    if(guess>rnum)
    {
     cout<<"\n\n"<<guess<<" is High....!!!!\a\a"<<"\n\nNow You Have "<<count<<" chance left";
     if(count==0)
     break;
     cout<<"\n\nEnter Your Guess Again:";
     cin>>guess;
     goto B;
    }
    if(guess<rnum)
    {
     cout<<"\n\n"<<guess<<" is Low...!!!\a\a"<<"\n\nNow You Have "<<count<<" chance left";
     if(count==0)
      break;
     cout<<"\n\nEnter Your Guess Again:";
     cin>>guess;
     goto B;
    }
   }
   if(guess==rnum)
   {
    cout<<"\n\n\t\tCongratulation "<<a<<" You Have Done IT.......!!!!!\a\a";
    switch(count)
    {
     case 7: cout<<"\n\nYour Score is 100 out of 100";
      break;
     case 6: cout<<"\n\nYour Score is 85 out of 100";
      break;
     case 5: cout<<"\n\nYour Score is 70 out of 100";
      break;
     case 4: cout<<"\n\nYour Score is 55 out of 100";
      break;
     case 3: cout<<"\n\nYour Score is 40 out of 100";
      break;
     case 2: cout<<"\n\nYour Score is 25 out of 100";
      break;
     case 1: cout<<"\n\nYour Score is 10 out of 100";
      break;
    }
   }
   else
    cout<<"\n\n\t\tSorry "<<a<<" Bad Luck......!!! Try Next Time\a\a\a";
   cout<<"\n\nWant to Play More(y/n):";
   cin>>ch;

   if(ch=='y'||ch=='Y')
   {
    c++;
    goto A;
   }
   else
    {
     cout<<"\n\n\n\n\t\t\t    @@@@@@@@ THANK YOU @@@@@@@@   \n\n";
     delay(10000);
     exit(0);
    }
  }
   break;
 case 3:
  {
    delay(10000);
    exit(0);

  }
 }
 getch();
}

No comments:

Post a Comment