1

I have got an assignment project to make a program using C++ which maintains a list of Students (their name, age, and GPA) in a text file. The program has the following functionality:

  • Insertion of record
  • Deletion of record
  • Searching of record
  • Updating a record

When deleting a record my code takes a string name as input and removes it from the text file. However the next two lines in the file (age & gpa for that student) are left. How do I remove those too?

Following is the code for my program.

#include <iostream>
#include <fstream>

using namespace std;
void writing();
void deleting();
void searching();

class student {

public:
  int age = 0;
  string name;

  void SetGpa(float x)
  {
    gpa = x;
  }
  float GetGpa()
  {
    return gpa;
  }

private:
  float gpa = 0.0;

};


int main()
{
  int opt;
  cout << "Please Enter an option number to continue:\n ";
  cout << "\nPress 1 for New Record insertion";
  cout << "\nPress 2 for Record Deletion";
  cout << "\nPress 3 for Searching a Record";
  cout << "\nPress 4 for Updating a Record";
  cout << "\nEnter option Number: ";
  cin >> opt;

  switch (opt)
  {
  case 1:
  {
    writing();
    break;
  }

  case 2:
  {
    deleting();
    break;
  }

  case 3:
  {
    searching();
    break;
  }
  case 4:
  {
    deleting();
    writing();
    cout << "Record has been updated! ";
    break;
  }
  }
}

void writing()
{
  float a;
  student moiz;
  cout << "Please enter name of student: ";
  cin >> moiz.name;
  cout << "Please enter the age of student: ";
  cin >> moiz.age;
  cout << "Pleae enter the Gpa of student: ";
  cin >> a;
  moiz.SetGpa(a);

  ofstream myfile;
  myfile.open("record.txt", ios::app | ios::out);
  myfile << endl << moiz.name << endl;
  myfile << moiz.age << endl;
  myfile << moiz.GetGpa();
  myfile.close();
}

void deleting()
{

  string line, name;
  cout << "Please Enter the name of record you want to delete: ";
  cin >> name;
  ifstream myfile;
  ofstream temp;
  myfile.open("record.txt");
  temp.open("temp.txt");
  while (getline(myfile, line))
  {
    if (line != name)
      temp << line << endl;
  }
  cout << "The record with the name " << name << " has been deleted if it exsisted" << endl;
  myfile.close();
  temp.close();
  remove("record.txt");
  rename("temp.txt", "record.txt");
}

void searching()
{
  ifstream fileInput;
  fileInput.open("record.txt");
  string line, search;
  cout << "Please enter the term to search: ";
  cin >> search;
  for (unsigned int curLine = 0; getline(fileInput, line); curLine++)
  {
    if (line.find(search) != string::npos)
    {
      cout << "found: " << search << " on line: " << curLine << endl;
    }
    else
    {
      cout << "Error! Not found on Line" << curLine << endl;
    }
  }
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
String Name
  • 203
  • 1
  • 10
  • 21
  • 4
    You didn't ask any question nor presented any problem. You need to be more specific. And you should be really specific, not to get rain of downvotes on a homework question. People do help with those here, but a significant effort on the asking part must be shown. – luk32 Apr 25 '14 at 14:53
  • I have shown the asking part in the very start of my question. Currently this program deletes Only the name from the text file what I want is it should delete Name+Age+Gpa from the text file. – String Name Apr 25 '14 at 15:00
  • OK, I tried... Maybe my english is too bad, however: "*Deleting a record from text file in C++*", "*I have got an assignment project to make a program using c++ which will do the following actions.*","*I am done with the project only the problem is I want to delete the whole record[...]*", no question to me. You say what you want, not what your problem is. I just tried to introduce you to some community standards, since you seem to be new. Try doing something, then say what doesn't work, with examples, then try asking why. I would be fine with anything, I just dislike new users getting `-5`s. – luk32 Apr 25 '14 at 15:06
  • Okay my problem is that in Deleting () function when the Name string gets deleted I want to delete the next 2 lines of AGE and GPA as well. NOW CAN YOU PLEASE HELP ME? – String Name Apr 25 '14 at 15:24
  • @StringName See my answer, it should solve your problem. It would have been a good idea to post only the code with the problem in question, and be a bit clearer about your problems in the question. – πάντα ῥεῖ Apr 25 '14 at 15:52
  • @StringName I've tried to simplify your question. For what it's worth, I thought it was reasonably clear before. IMO it's inappropriate for you to have been downvoted so harshly. – RJFalconer Apr 25 '14 at 15:57

1 Answers1

2

You can add an else clause to your statement checking the name, and introduce a counter of how many of the following lines should be skipped after name was found:

int skip = 0;
while (getline(myfile, line)) {
    if ((line != name) && !(skip > 0)) {
       temp << line << endl;
    }
    else { 
      if(skip == 0) {
          skip = 2; // Skip the next two lines also
      }
      else {
          --skip;
      }
   }
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190