Tuesday, January 25, 2022

Tic Tack Toe Game in C++

This is a Character based User Interface or CUI based simple computerized solution to manage the system activities. This prototype code saves time and energy of its user by automating certain tasks. This system is either file handling type or ROM type, and stores activities in a coder-defined file or on ROM memory temporarily.

C++ is a typed language, so no need of code explanation as everything will be cleared by itself by reading the code. If you know C++, you know this code. It’s that simple!

Note to students, draw a class diagram and flow chart of system by reading the code itself. The Code::Blocks IDE 20.03+MinGW and Cygwin tool are used to test the code.

Note that some systems are very simple and need enhancement but they are complete on their own in other terms.

 

TicTackToeGame.cpp

 

 

 

 

#include<iostream>

#include<stdlib.h>

#include<windows.h>

using namespace std;

main()

{

    char ch;

    do

    {

        system("CLS");

        int i=0;

        string arr[3][3]= {{"1","2","3"},{"4","5","6"},{"7","8","9"}};

 

        do

        {

            cout<<endl<<endl;

 

            int a;

 

 

            cout<<arr[0][0]<<"  | "<<arr[0][1]<<"  | "<<arr[0][2]<<endl;

            cout<<"___|____|___"<<endl;

            cout<<arr[1][0]<<"  | "<<arr[1][1]<<"  | "<<arr[1][2]<<endl;

            cout<<"___|____|___"<<endl;

            cout<<arr[2][0]<<"  | "<<arr[2][1]<<"  | "<<arr[2][2]<<endl;

            cout<<"   |    |    "<<endl;

 

            cout<<"\nEnter player A"<<endl;

            cin>>a;

            switch(a)

            {

            case 1:

                if(arr[0][0]!="A"&&arr[0][0]!="B")

                {

                    arr[0][0]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 2:

                if(arr[0][1]!="A"&&arr[0][1]!="B")

                {

                    arr[0][1]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 3:

                if(arr[0][2]!="A"&&arr[0][2]!="B")

                {

                    arr[0][2]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 4:

                if(arr[1][0]!="A"&&arr[1][0]!="B")

                {

                    arr[1][0]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 5:

                if(arr[1][1]!="A"&&arr[1][1]!="B")

                {

                    arr[1][1]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 6:

                if(arr[1][2]!="A"&&arr[1][2]!="B")

                {

                    arr[1][2]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 7:

                if(arr[2][0]!="A"&&arr[2][0]!="B")

                {

                    arr[2][0]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 8:

                if(arr[2][1]!="A"&&arr[2][1]!="B")

                {

                    arr[2][1]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            case 9:

                if(arr[2][2]!="A"&&arr[2][2]!="B")

                {

                    arr[2][2]="A";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                }

                break;

            default:

                cout<<"Invalid input please try again in next turn"<<endl;

                Sleep(3000);

 

            }

            if(arr[0][0]==arr[0][1]&&arr[0][1]==arr[0][2]||arr[1][0]==arr[1][1]&&arr[1][1]==arr[1][2]||arr[2][0]==arr[2][1]&&arr[2][1]==arr[2][2]

                    ||arr[0][0]==arr[1][0]&&arr[1][0]==arr[2][0]||arr[0][1]==arr[1][1]&&arr[1][1]==arr[2][1]||arr[0][2]==arr[1][2]&&arr[1][2]==arr[2][2]

                    ||arr[0][0]==arr[1][1]&&arr[1][1]==arr[2][2]||arr[0][2]==arr[1][1]&&arr[1][1]==arr[2][0])

            {

                cout<<"***************Congratulations**************\n        Player A won the Game"<<endl;

                break;

            }

            cout<<"\nEnter player B"<<endl;

            cin>>a;

            switch(a)

            {

            case 1:

                if(arr[0][0]!="A"&&arr[0][0]!="B")

                {

                    arr[0][0]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 2:

                if(arr[0][1]!="A"&&arr[0][1]!="B")

                {

                    arr[0][1]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 3:

                if(arr[0][2]!="A"&&arr[0][2]!="B")

                {

                    arr[0][2]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 4:

                if(arr[1][0]!="A"&&arr[1][0]!="B")

                {

                    arr[1][0]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 5:

                if(arr[1][1]!="A"&&arr[1][1]!="B")

                {

                    arr[1][1]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 6:

                if(arr[1][2]!="A"&&arr[1][2]!="B")

                {

                    arr[1][2]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 7:

                if(arr[2][0]!="A"&&arr[2][0]!="B")

                {

                    arr[2][0]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 8:

                if(arr[2][1]!="A"&&arr[2][1]!="B")

                {

                    arr[2][1]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            case 9:

                if(arr[2][2]!="A"&&arr[2][2]!="B")

                {

                    arr[2][2]="B";

                }

                else

                {

                    cout<<"Already Filled try in next turn"<<endl;

                    Sleep(1000);

                }

                break;

            default :

                cout<<"Invalid input please try again in next turn"<<endl;

                Sleep(3000);

 

            }

            if(arr[0][0]==arr[0][1]&&arr[0][1]==arr[0][2]||arr[1][0]==arr[1][1]&&arr[1][1]==arr[1][2]||arr[2][0]==arr[2][1]&&arr[2][1]==arr[2][2]

                    ||arr[0][0]==arr[1][0]&&arr[1][0]==arr[2][0]||arr[0][1]==arr[1][1]&&arr[1][1]==arr[2][1]||arr[0][2]==arr[1][2]&&arr[1][2]==arr[2][2]

                    ||arr[0][0]==arr[1][1]&&arr[1][1]==arr[2][2]||arr[0][2]==arr[1][1]&&arr[1][1]==arr[2][0])

            {

                cout<<"***************Congratulations**************\n        Player B won the Game"<<endl;

                break;

            }

 

            system("CLS");

            i++;

        }

        while(i<5);

        cout<<"Press Y if you want to contine... "<<endl;

        cin>>ch;

    }

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

}

 

 

 

 

For more C/C++ codes related information click here.

…till next post, bye-bye and take care.

 

 

 

Monday, January 24, 2022

Student Report Management System in C++

This is a Character based User Interface or CUI based simple computerized solution to manage the system activities. This prototype code saves time and energy of its user by automating certain tasks. This system is either file handling type or ROM type, and stores activities in a coder-defined file or on ROM memory temporarily.

C++ is a typed language, so no need of code explanation as everything will be cleared by itself by reading the code. If you know C++, you know this code. It’s that simple!

Note to students, draw a class diagram and flow chart of system by reading the code itself. The Code::Blocks IDE 20.03+MinGW and Cygwin tool are used to test the code.

Note that some systems are very simple and need enhancement but they are complete on their own in other terms.

 

StudentReportManagementSystem.cpp

 

 

 

 

#include<iostream>

#include<fstream>

#include<iomanip>

 

using namespace std;

 

// the class that stores data

class student

{

    int rollno;

    char name[50];

    int eng_marks, math_marks, sci_marks, lang2_marks, cs_marks;

    double average;

    char grade;

 

public:

    void getdata();

    void showdata() const;

    void calculate();

    int retrollno() const;

}; //class ends here

 

void student::calculate()

{

    average=(eng_marks+math_marks+sci_marks+lang2_marks+cs_marks)/5.0;

    if(average>=90)

        grade='A';

    else if(average>=75)

        grade='B';

    else if(average>=50)

        grade='C';

    else

        grade='F';

}

 

void student::getdata()

{

    cout<<"\nEnter student's roll number: ";

    cin>>rollno;

    cout<<"\n\nEnter student name: ";

    cin.ignore();

    cin.getline(name,50);

    cout<<"\nAll marks should be out of 100";

    cout<<"\nEnter marks in English: ";

    cin>>eng_marks;

    cout<<"\nEnter marks in Math:  ";

    cin>>math_marks;

    cout<<"\nEnter marks in Science:  ";

    cin>>sci_marks;

    cout<<"\nEnter marks in 2nd language:  ";

    cin>>lang2_marks;

    cout<<"\nEnter marks in Computer science:  ";

    cin>>cs_marks;

    calculate();

}

void student::showdata() const

{

    cout<<"\nRoll number of student : "<<rollno;

    cout<<"\nName of student : "<<name;

    cout<<"\nEnglish : "<<eng_marks;

    cout<<"\nMaths : "<<math_marks;

    cout<<"\nScience : "<<sci_marks;

    cout<<"\nLanguage2 : "<<lang2_marks;

    cout<<"\nComputer Science :"<<cs_marks;

    cout<<"\nAverage Marks :"<<average;

    cout<<"\nGrade of student is :"<<grade;

}

int  student::retrollno() const

{

    return rollno;

}

//function declaration

void create_student();

void display_sp(int);//display particular record

void display_all(); // display all records

void delete_student(int);//delete particular record

void change_student(int);//edit particular record

//MAIN

int main()

{

    char ch;

    cout<<setprecision(2);

    do

    {

        char ch;

        int num;

        system("cls");

        cout<<"\n\n\n\tMENU";

        cout<<"\n\n\t1.Create student record";

        cout<<"\n\n\t2. Search student record";

        cout<<"\n\n\t3. Display all students records ";

        cout<<"\n\n\t4.Delete student record";

        cout<<"\n\n\t5.Modify student record";

        cout<<"\n\n\t6.Exit";

        cout<<"\n\n\What is your Choice (1/2/3/4/5/6) ";

        cin>>ch;

        system("cls");

        switch(ch)

        {

        case '1':

            create_student();

            break;

        case '2':

            cout<<"\n\n\tEnter The roll number ";

            cin>>num;

            display_sp(num);

            break;

        case '3':

            display_all();

            break;

        case '4':

            cout<<"\n\n\tEnter The roll number: ";

            cin>>num;

            delete_student(num);

            break;

        case '5':

            cout<<"\n\n\tEnter The roll number ";

            cin>>num;

            change_student(num);

            break;

        case '6':

            cout<<"Exiting, Thank you!";

            exit(0);

        }

    }

    while(ch!='6');

    return 0;

}

//write student details to file

void create_student()

{

    student stud;

    ofstream oFile;

    oFile.open("student.dat",ios::binary|ios::app);

    stud.getdata();

    oFile.write(reinterpret_cast<char *> (&stud), sizeof(student));

    oFile.close();

    cout<<"\n\nStudent record Has Been Created ";

    cin.ignore();

    cin.get();

}

// read file records

void display_all()

{

    student stud;

    ifstream inFile;

    inFile.open("student.dat",ios::binary);

    if(!inFile)

    {

        cout<<"File could not be opened !! Press any Key to exit";

        cin.ignore();

        cin.get();

        return;

    }

    cout<<"\n\n\n\t\tDISPLAYING ALL RECORDS\n\n";

    while(inFile.read(reinterpret_cast<char *> (&stud), sizeof(student)))

    {

        stud.showdata();

        cout<<"\n\n====================================\n";

    }

    inFile.close();

    cin.ignore();

    cin.get();

}

//read specific record based on roll number

void display_sp(int n)

{

    student stud;

    ifstream iFile;

    iFile.open("student.dat",ios::binary);

    if(!iFile)

    {

        cout<<"File could not be opened... Press any Key to exit";

        cin.ignore();

        cin.get();

        return;

    }

    bool flag=false;

    while(iFile.read(reinterpret_cast<char *> (&stud), sizeof(student)))

    {

        if(stud.retrollno()==n)

        {

            stud.showdata();

            flag=true;

        }

    }

    iFile.close();

    if(flag==false)

        cout<<"\n\nrecord does not exist";

    cin.ignore();

    cin.get();

}

// modify record for specified roll number

void change_student(int n)

{

    bool found=false;

    student stud;

    fstream fl;

    fl.open("student.dat",ios::binary|ios::in|ios::out);

    if(!fl)

    {

        cout<<"File could not be opened. Press any Key to exit...";

        cin.ignore();

        cin.get();

        return;

    }

    while(!fl.eof() && found==false)

    {

        fl.read(reinterpret_cast<char *> (&stud), sizeof(student));

        if(stud.retrollno()==n)

        {

            stud.showdata();

            cout<<"\n\Enter new student details:"<<endl;

            stud.getdata();

            int pos=(-1)*static_cast<int>(sizeof(stud));

            fl.seekp(pos,ios::cur);

            fl.write(reinterpret_cast<char *> (&stud), sizeof(student));

            cout<<"\n\n\t Record Updated";

            found=true;

        }

    }

    fl.close();

    if(found==false)

        cout<<"\n\n Record Not Found ";

    cin.ignore();

    cin.get();

}

//delete record with particular roll number

void delete_student(int n)

{

    student stud;

    ifstream iFile;

    iFile.open("student.dat",ios::binary);

    if(!iFile)

    {

        cout<<"File could not be opened... Press any Key to exit...";

        cin.ignore();

        cin.get();

        return;

    }

    ofstream oFile;

    oFile.open("Temp.dat",ios::out);

    iFile.seekg(0,ios::beg);

    while(iFile.read(reinterpret_cast<char *> (&stud), sizeof(student)))

    {

        if(stud.retrollno()!=n)

        {

            oFile.write(reinterpret_cast<char *> (&stud), sizeof(student));

        }

    }

    oFile.close();

    iFile.close();

    remove("student.dat");

    rename("Temp.dat","student.dat");

    cout<<"\n\n\tRecord Deleted ..";

    cin.ignore();

    cin.get();

}

 

 

 

 

For more C/C++ codes related information click here.

…till next post, bye-bye and take care.