So I have been trying to make a code to modify on a text file that I have entered earlier, the user should enter the data of a student providing the name , department, residential area zip code and the number of subjects with their marks. The code stores the new data in a temporary file then it would be latter all copied to the main file. The code search for the name the user wants to edit then it searches for a similar name. If the name is similar ,the used is asked to enter the new data and if the name is different the data is copied as it is in the temp file. It worked for a couple of times alright then it started going in infinite loops and printing garbage in the temp. file and I have no clue why. ps: i use chomp function to remove '\n' from the name.
void modify_student()
{
char name[30], id[10], dep[25], /*nsub[10]*/ /*nmarks[15],*/ city[15], resd[15], zip[15];
int nsubs, nmarks[10];
ifstream mfile("students.txt");
ofstream temp("tmp.txt");
//cin.ignore();
char rq[30];
cout << "Enter the name of the required student : ";
cin.getline(rq, 25);
while (!mfile.eof())
{
mfile.getline(name, 30, '|');
//chomp(name);
//char* cname;
//cname = name; //might return to
mfile.getline(id, 10, '|');
mfile.getline(dep, 25, '|');
mfile >> nsubs;
for (int i = 0; i < nsubs; i++)
{
mfile >> marks[i];
}
mfile.getline(city, 15, '|');
mfile.getline(resd, 15, '|');
mfile.getline(zip, 15, '|');
if (strcmp(name, rq) == 0) //fix lower case //search by id
{
cout << "Enter the new name : ";
//cin.ignore();
cin.getline(name, 25);
cout << "Enter the new student's number : ";
cin.getline(id, 10);
//cin.ignore();
cout << "Enter the new department's : ";
//cin.ignore();
cin.getline(dep, 25);
temp << name << "|" << id << "|" << dep << "|"; //continue
cout << "Enter the new number of subjects : ";
cin >> nsubs;
temp << nsubs << "|";
for (int i = 0; i < nsubs; i++)
{
cout << "Enter the new mark for the " << i + 1 << " subject : ";
cin >> nmarks[i];
temp << nmarks[i] << " ";
}
temp << "|";
cout << "Enter the new city : ";
cin.ignore();
cin.getline(city, 15);
cout << "Enter the new resedential : ";
cin.getline(resd, 15);
cout << "Enter the new Zip code : ";
cin.getline(zip, 15);
temp << city << "|" << resd << "|" << zip << "|" << "\n";
}
else
{
temp << name << "|" << id << "|" << dep << "|";
temp << nsubs << "|";
for (int i = 0; i < nsubs; i++)
{
//cout << "Enter the new " << i + 1 << " subject : ";
//cin >> nmarks[i];
temp << marks[i] << " ";
}
temp << "|";
temp << city << "|" << resd << "|" << zip << "|" << "\n";
}
}
temp.close();
mfile.close();
/*ofstream mfilee("students.txt");
ifstream tempo("tmp.txt");
while (!temp.eof())
{
tempo.getline(name, 30, '|');
//chomp(name);
char* cname;
cname = name;
tempo.getline(id, 10, '|');
tempo.getline(dep, 25, '|');
tempo >> nsubs;
for (int i = 0; i < nsubs; i++)
{
tempo >> marks[i];
}
tempo.getline(city, 15, '|');
tempo.getline(resd, 15, '|');
tempo.getline(zip, 15, '|');
mfilee << name << "|" << id << "|" << dep << "|";
mfilee << nsubs << "|";
for (int i = 0; i < nsubs; i++)
{
mfilee << marks[i] << " ";
}
mfilee << "|";
mfilee << city << "|" << resd << "|" << zip << "|" << "\n";
}
mfilee.close();
tempo.close();
remove("tmp.txt");
cout << "\n Task completed";
*/