C++ में उपयोगकर्ताओं को अद्वितीय आईडी देना


मैंने C++ में बनाने, पढ़ने, अद्यतन करने और हटाने के लिए एक क्रूड बनाया है। अभी मैं जिस समस्या का सामना कर रहा हूं वह यह है कि जब मैं डेटा दर्ज करता हूं तो व्यक्ति की आईडी 1, 2, 3 इत्यादि से शुरू नहीं होती है।

आईडी यादृच्छिक संख्याओं जैसे 1586, 1587 इत्यादि से प्रारंभ होती है

कृपया अपने कंपाइलर को चलाएं, आप समस्या को देख पाएंगे।

मैंने क्या प्रयास किया है:

सी++
  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

यह यादृच्छिक नहीं है, इसे फ़ाइल से पढ़ा जा रहा है:

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

केवल जब आप एक नया छात्र जोड़ते हैं, तो आप एक “नया” मान निर्धारित करते हैं – लेकिन आप यह जांच नहीं करते हैं कि क्या वह मान पहले ही उपयोग किया जा चुका है! इसलिए यदि आप शुरुआत करते समय मौजूदा छात्रों को पढ़ते हैं, फिर एक नया छात्र जोड़ते हैं, तो आपके पास शून्य आईडी वाले दो छात्र होंगे (और संभवतः अन्य फ़ाइलों में संबंधित जानकारी जो उदाहरण के लिए छात्रों को ग्रेड से संबंधित करने के लिए उस आईडी का उपयोग करती है)।

आपको सबसे बड़ी मौजूदा आईडी ढूंढनी होगी, और अगली आईडी के रूप में 0 को “समझदार” मानने के बजाय उससे एक बड़ी आईडी वाले छात्र को जोड़ना होगा।

コメント

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