اذهب إلى المحتوى
  • 0

حذف أسطر من ملف موظفين بحسب العمر في ++C

Abdo Atala

السؤال

ازاي احذف موظفين اللي بين عمر كذا وكذا في كود زي دا

*/

#include <iostream>

#include <iomanip>



using namespace std;

int main()

{

    FILE *fp, *ft;

    char another, choice;



    struct employee

    {

        char first_name[50], last_name[50];

        int Age;

        long Salary;

    };



    struct employee e;

    int xAge;

    long int recsize;



    fp=fopen("users.txt","rb+");



    if (fp == NULL)

    {

        fp = fopen("users.txt","wb+");



        if (fp==NULL)

        {

            puts("Cannot open file");

            return 0;

        }

    }







    recsize = sizeof(e);



    while(1)

    {

        system("cls");



        cout << "\t\t====== Employee DATABASE MANAGEMENT SYSTEM ======";

        cout <<"\n\n                                          ";

        cout << "\n\n";

        cout << "\n \t\t\t 1. Add New Employee ";

        cout << "\n \t\t\t 2. List Employees  ";

        cout << "\n \t\t\t 3. Modify Employee's Salary ";

        cout << "\n \t\t\t 4. Delete Employee ";

        cout << "\n \t\t\t 5. Exit  ";

        cout << "\n\n";

        cout << "\t\t\t Select Your Choice :=> ";

        fflush(stdin);

        choice = getchar();

        switch(choice)

        {

        case '1' :

            fseek(fp,0,SEEK_END);

            another ='Y';

            while(another == 'Y' || another == 'y')

            {

                system("cls");

                cout << "Enter the First Name : ";

                cin >> e.first_name;

                cout << "Enter the Last Name : ";

                cin >> e.last_name;

                cout << "Enter the Employee Age : ";

                cin >> e.Age;

                cout << "Enter the Employee Salary : ";

                cin >> e.Salary;

                fwrite(&e,recsize,1,fp);

                cout << "\n Add Another Record (Y/N) ";

                fflush(stdin);

                another = getchar();

            }

            break;

        case '2':

            system("cls");

            rewind(fp);

            cout << "=== View the Records in the Database ===";

            cout << "\n";

            while (fread(&e,recsize,1,fp) == 1)

            {

                cout << "\n";

                cout <<"\n" << e.first_name << setw(10)  << e.last_name;

                cout << "\n";

                cout <<"\n" <<e.Age <<  setw(8)  << e.Salary;

            }

            cout << "\n\n";

            system("pause");

            break;



        case '3' :

            system("cls");

            another = 'Y';

            while (another == 'Y'|| another == 'y')

            {

                cout << "\n Enter the Age of the Employee : ";

                cin >> xAge;



                rewind(fp);

                while (fread(&e,recsize,1,fp) == 1)

                {

                    if (e.Age=xAge)

                    {

                        cout << "Enter New Employee Salary   : ";

                        cin >> e.Salary;

                        fseek(fp, - recsize, SEEK_CUR);

                        fwrite(&e,recsize,1,fp);

                        break;

                    }

                    else

                        cout<<"record not found";

                }

                cout << "\n Modify Another Record (Y/N) ";

                fflush(stdin);

                another = getchar();

            }

            break;





        case '4':

            system("cls");

            another = 'Y';

            while (another == 'Y'|| another == 'y')

            {

                cout << "\n Enter the Age of the Employee to delete : ";

                cin >> xAge;



                ft = fopen("temp.dat", "wb");



                rewind(fp);

                while (fread (&e, recsize,1,fp) == 1)



                    if (e.Age!=xAge)

                    {

                        fwrite(&e,recsize,1,ft);

                    }

                fclose(fp);

                fclose(ft);

                remove("users.txt");

                rename("temp.dat","users.txt");



                fp=fopen("users.txt","rb+");



                cout << "\n Delete Another Record (Y/N) ";

                fflush(stdin);

                another = getchar();

            }



            break;



        case '5':

            fclose(fp);

            cout << "\n\n";

            cout << "\t\t     THANK YOU";

            cout << "\n\n";

            exit(0);

        }

    }





    system("pause");

    return 0;

}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

إذا كنت تقصد عدم إظهار الموظفين ضمن قائمة العرض في الخيار الثانية يمكن ذلك عبر التحقق من عمر المستخدم الحالي داخل while ثم إما طباعته أو تجاوزه باستخدام continue حسب الشرط المطلوب، بطريقة تشبه التالي

case '2':
  ...
  while (fread(&e,recsize,1,fp) == 1)
  {
    if(e.Age > 10 && e.Age < 20) continue;
      ...

  }
  ...
  break;

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 16 دقائق مضت قال Abdo Atala:

اقصد case 4 ازاي احذف الموظف اللي ما بين عمر 23 و25 مثلا

يجب أولا إدخال قيمتي العمر كالتالي

cin >> minAge;
cin >> maxAge;

ثم تغيير شرط الحذف ضمن while ليتم إضافة الموظف فقط في حال حقق شرط العمر كالتالي

if (e.Age < minAge || e.Age > minAge) {
  ...
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...