0

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";
    */
  • 3
    Start by reviewing [Why !.eof() inside a loop condition is always wrong.](https://stackoverflow.com/q/5605125/9254539) and post [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve). – David C. Rankin May 16 '20 at 02:20
  • 2
    What does stepping through the code in a debugger tell you? If you don't know how to use it, this would be a perfect opportunity to learn. The debugger is one of the most powerful tools that a programmer can have in their toolbox, and it's never too early to start learning how to use that tool. – Ken White May 16 '20 at 02:32
  • Are you allowed to use ````std::vector```` or ````std::string```` or classes or STL algorithms? You are still using heaviliy C-code. What are you allowed to use? – Armin Montigny May 16 '20 at 07:57
  • I think the infinite loop was due to the '|' marks I used as break points in the text file it self. I modified it a bit and now the code is running it just goes in an extra loop because of the '\n' at the end of the file and I'm still looking for ways to fix this bug. – Ahmed Osama May 16 '20 at 12:55

0 Answers0