首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >学生管理系统C++

学生管理系统C++
EN

Stack Overflow用户
提问于 2014-04-23 03:49:04
回答 1查看 7K关注 0票数 0

我有这个程序在C++的学生管理系统,一切都很好,除了一个地方,我试图删除一个学生根据他的名册号码。它应该做什么:在询问记录号之后,搜索记录并删除它在做什么:它将删除所有与该卷号不匹配的记录。

这是我的代码:

代码语言:javascript
运行
复制
    #include <iostream>

    #include <cstdio>

    #include <cstring>

    #include <cstdlib>

    #include <conio.h>

    #include <iomanip>



using namespace std;
int main() {

    FILE *fp, *ft;

    char another, choice;



struct student {

        char first_name[50], last_name[50];
        int roll_num; //new code added
        char course[100];
        int section;
    };

    struct student e;
    char xfirst_name[50], xlast_name[50];
    int xroll_num ; // new code added
    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====== STUDENT INFORMATION SYSTEM ======";
     cout <<"\n\n                                          ";
     cout << "\n\n";
     cout<<" \n\t\t\t======================";
     cout << "\n \t\t\t  1. Add    Records";
     cout << "\n \t\t\t  2. List   Records";
     cout << "\n \t\t\t  3. Modify Records";
     cout << "\n \t\t\t  4. Delete Records";
     cout << "\n \t\t\t  5. Exit   Program";
     cout<<" \n\t\t\t======================";
     cout << "\n\n";
     cout << "\t\t\t Select Your Choice ::";
     fflush(stdin);
     choice = _getche();
     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 Course    : ";
                cin >> e.course;
                cout << "Enter the Section   : ";
                cin >> e.section;
                cout << "Enter the roll number :";
                cin >> e.roll_num;
                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 <<"\nName     :: " <<e.first_name <<" "<<e.last_name;
           //cout << "\n";
           cout <<"\nRoll Number :: " << e.roll_num ;
           cout <<"\nCourse   :: " <<e.course ;
           cout <<"\nSection  :: "<<e.section;
           }
           cout << "\n\n";
           system("pause");
           break;

       case '3' :
            system("cls");
          another = 'Y';
          while (another == 'Y'|| another == 'y')
          {
         //     cout << "\n Enter the last name of the student : ";
              cout << "\n Enter the Roll number of the student : ";
              cin >> xroll_num;

            rewind(fp);
            while (fread(&e,recsize,1,fp) == 1)
            {
                //if (strcmp(e.last_name,xlast_name) == 0)
                if(e.roll_num == xroll_num )
                {
                cout << "Enter the new Firt Name : ";
                cin >> e.first_name;
                cout << "Enter the new Last Name : ";
                cin >> e.last_name;
                cout << "Enter the new Roll Number : ";
                cin >> e.roll_num;
                cout << "Enter the new Course    : ";
                cin >> e.course;
                cout << "Enter the new Section   : ";
                cin >> e.section;
                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 last name of the student to delete : ";
              cout <<"\n Enter the roll number of the student to delete : ";
              cin >> xroll_num;

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

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

                // if (strcmp(e.last_name,xlast_name) != 0)
                    if(e.roll_num == xroll_num )
                {
                    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 FOR USING THIS SOFTWARE";
              cout << "\n\n";
              exit(0);
          }
          }
     system("pause");
return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-23 04:01:29

它删除与该卷号不匹配的所有其他记录。

您只需要编写与临时文件匹配的记录,然后使用该文件覆盖users.txt文件

代码语言:javascript
运行
复制
if (e.roll_num == xroll_num) {
  fwrite(&e, recsize, 1, ft);
}

我想你真正想做的是

代码语言:javascript
运行
复制
if (e.roll_num != xroll_num) {
  fwrite(&e, recsize, 1, ft);
}

您可能应该阅读一个好的C++ i/o教程,因为您的代码大多是C。请考虑将student结构编写为简单的文本,而不是将其大量写入文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23234394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档