Click here to Skip to main content
15,942,788 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a crud to create, read, update and delete in C++. The problem I am facing right now is that while I enter the data the id's of the person don't start from 1, 2, 3 and so on.

the id's start from the random numbers like 1586 , 1587 and so on

kindly make a run on your compiler you will be able to see the problem at hand.

What I have tried:

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  }
Posted
Updated 22-Mar-24 0:09am
v2
Comments
samcodeonline 22-Mar-24 6:24am    
is there someone to answer ??
Richard MacCutchan 22-Mar-24 6:38am    
Please be patient, we are all volinteers here, and answer in our own time.
Richard MacCutchan 22-Mar-24 6:37am    
I just tried your code and the ids start at 1 and go up in sequence. You need to show what actually happens when you run it.

1 solution

It's not random, it's being read from the file:
C++
 void readData() {
51      Student student;
52      ifstream read;
53      read.open("student.txt");
54      while (!read.eof()) {
55          read >> student.id;
Only when you add a new student, do you set a "new" value - but you don't check if that value has already been used! So if you read the existing students when you start up, then add a new student, you will have two students with an ID of zero (and quite possibly related info in other files which uses that ID to relate grades to students for example).

You need to find the largest existing ID, and add a student with an ID one greater than that, rather than assuming 0 is "sensible" as the next ID.
 
Share this answer
 
Comments
samcodeonline 22-Mar-24 6:29am    
can you try for a quick solution @OriginalGriff
OriginalGriff 22-Mar-24 6:48am    
What part of it is difficult for you?
You wrote the rest of the code, so it should be pretty obvious ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900