在 C++ 中为用户提供唯一的 id

编程


我创建了一个 CRUD 来在 C++ 中创建、读取、更新和删除。 我现在面临的问题是,当我输入数据时,该人的 ID 不是从 1、2、3 等开始的。

id 从随机数开始,如 1586 、 1587 等

请在您的编译器上运行一下,您将能够看到手头的问题。

我尝试过的:

C++
  1  #include <iostream>
  2  #include <fstream>
  3  #include<string>
  4  using namespace std;
  5  int ID = 0;
  6  struct Student {
  7      int id;
  8      string firstName;
  9      string userName;
 10      string password;
 11  };
 12  
 13  void addStudent() {
 14      Student student;
 15      cout << "\n\tEnter First Name : ";
 16      cin.get();
 17      getline(cin, student.firstName); //Nouman Habib
 18      cout << "\n\tEnter User Name : ";
 19      cin >> student.userName;
 20      cout << "\n\tEnter Password : ";
 21      cin >> student.password;
 22       
 23      ID++;
 24      student.id = ID;
 25  
 26      ofstream write;
 27      write.open("student.txt", ios::app);
 28      write << "\n" << ID;
 29      write << "\n" << student.firstName ;
 30      write << "\n" << student.userName ;
 31  
 32      write << "\n" << student.password ;
 33      
 34      write.close();
 35      write.open("id.txt");
 36      write << ID;
 37      write.close();
 38      cout << "\n\tData save to file";
 39  }
 40  
 41  void print(Student s) {
 42      cout << "\n\t---Stuent Data---";
 43      cout << "\n\tID is : " << s.id;
 44      cout << "\n\tFirst Name is : " << s.firstName;
 45      cout << "\n\tUser Name is : " << s.userName;
 46      cout << "\n\tPassword is : " << s.password;
 47       
 48  }
 49  
 50  void readData() {
 51      Student student;
 52      ifstream read;
 53      read.open("student.txt");
 54      while (!read.eof()) {
 55          read >> student.id;
 56          read.ignore();
 57          getline(read, student.firstName);
 58          read >> student.userName;
 59          read >> student.password;
 60          
 61          print(student);
 62      }
 63      read.close();
 64  }
 65  
 66  int searchData() {
 67      int id;
 68      cout << "\n\tEnter student id want to search : ";
 69      cin >> id;
 70      Student student;
 71      ifstream read;
 72      read.open("student.txt");
 73      while (!read.eof()) {
 74          read >> student.id;
 75          read.ignore();
 76          getline(read, student.firstName);
 77          read >> student.userName;
 78          read >> student.password;
 79           
 80          if (student.id == id) {
 81              print(student);
 82              return id;
 83          }
 84      }
 85  }
 86  
 87  void deleteData() {
 88      int id = searchData();
 89      cout << "\n\tYou want to delete record (y/n) : ";
 90      char choice;
 91      cin >> choice;
 92      if (choice == 'y') {
 93          Student student;
 94          ofstream tempFile;
 95          tempFile.open("temp.txt");
 96          ifstream read;
 97          read.open("student.txt");
 98          while (!read.eof()) {
 99              read >> student.id;
100              read.ignore();
101              getline(read, student.firstName);
102              read >> student.userName;
103              read >> student.password;
104               
105              if (student.id != id) {
106                  tempFile << "\n" << student.id;
107                  tempFile << "\n" << student.firstName;
108                  tempFile << "\n" << student.userName;
109                  tempFile << "\n" << student.password;
110              }
111          }
112          read.close();
113          tempFile.close();
114          remove("student.txt");
115          rename("temp.txt", "student.txt");
116          cout << "\n\tData deleted successfuly";
117      }
118      else {
119          cout << "\n\tRecord not deleted";
120      }
121  }
122  
123  void updateData() {
124      int id = searchData();
125      cout << "\n\tYou want to update record (y/n) : ";
126      char choice;
127      cin >> choice;
128      if (choice == 'y') {
129          Student newData;
130          cout << "\n\tEnter First Name : ";
131          cin.get();
132          getline(cin, newData.firstName);
133          cout << "\n\tEnter User Name : ";
134          cin >> newData.userName;
135          cout << "\n\tEnter Password : ";
136          cin >> newData.password;
137           
138          Student student;
139          ofstream tempFile;
140          tempFile.open("temp.txt");
141          ifstream read;
142          read.open("student.txt");
143          while (!read.eof()) {
144              read >> student.id;
145              read.ignore();
146              getline(read, student.firstName);
147              read >> student.userName;
148              read >> student.password;
149              
150              if (student.id != id) {
151                  tempFile << "\n" << student.id;
152                  tempFile << "\n" << student.firstName;
153                  tempFile << "\n" << student.userName;
154                  tempFile << "\n" << student.password;
155                   
156              }
157              else {
158                  tempFile << "\n"<< student.id;
159                  tempFile << "\n"<< newData.firstName;
160                  tempFile << "\n"<< newData.userName;
161                  tempFile << "\n" << newData.password;
162                   
163              }
164          }
165          read.close();
166          tempFile.close();
167          remove("student.txt");
168          rename("temp.txt", "student.txt");
169          cout << "\n\tData updated successfuly";
170      }
171      else {
172          cout << "\n\tRecord not deleted";
173      }
174  }
175  
176  int main()
177  {
178      ifstream read;
179      read.open("id.txt");
180      if (!read.fail()) {
181          read >> ID;
182      }
183      else {
184          ID = 0;
185      }
186      read.close();
187  
188      while (true) {
189          cout << "\n\t1.Add data";
190          cout << "\n\t2.See  data";
191          cout << "\n\t3.Search  data";
192          cout << "\n\t4.Delete data";
193          cout << "\n\t5.Update  data";
194  
195          int choice;
196          cout << "\n\tEnter choice : ";
197          cin >> choice;
198          switch (choice) {
199          case 1:
200              addStudent();
201              break;
202          case 2:
203              readData();
204              break;
205          case 3:
206              searchData();
207              break;
208          case 4:
209              deleteData();
210              break;
211          case 5:
212              updateData();
213              break;
214          }
215      }
216  
217  }

解决方案1

它不是随机的,它是从文件中读取的:

C++
 void readData() {
51      Student student;
52      ifstream read;
53      read.open("student.txt");
54      while (!read.eof()) {
55          read >> student.id;

仅当您添加新学生时,您才会设置“新”值 – 但您不会检查该值是否已被使用! 因此,如果您在启动时读取现有学生,然后添加一个新学生,您将有两个 ID 为零的学生(并且很可能在其他文件中使用该 ID 将成绩与学生相关联的相关信息)。

您需要找到最大的现有 ID,并添加一个 ID 比该 ID 大 1 的学生,而不是假设 0 作为下一个 ID 是“合理的”。

コメント

タイトルとURLをコピーしました