10 Replies - 3928 Views - Last Post: 26 May 2009 - 12:44 PM Rate Topic: -----

#1 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

How to delete existing records?

Post icon  Posted 25 May 2009 - 05:50 PM

I am unsure how to delete an existing student record. Here is my code:
#include <iostream>
#include <string>
using namespace std;

const int SIZE=30;
struct StudentRecord
	 {
	   string studentID;
	   float studentMark;
	 };

void populate(StudentRecord *,string);
void dispOne(StudentRecord *);
int linearSearch (StudentRecord*student,const string id,int totalStudents);

int main()
{
	int menu;
	StudentRecord student[SIZE] = 
				  {
				  {"P1001",78.50},
				  {"P1002",66}
				  };
	StudentRecord *recPoint;
	int totalStudents=2;
	float totalMarks;
	float averageMark;
	string id;
	int found;
	char studentGrade;
	
	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics	2. Enter mark\n"
		 << "3. Find mark	4. Delete mark   5. List marks\n"
		 << "----------------------------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  while (menu != 0)
  {
	switch (menu)
	{
		   case 1:
				totalMarks=0;
				for(int j=0;j<totalStudents;j++)
					totalMarks+=student[j].studentMark;
					averageMark=totalMarks/totalStudents;
					cout << "Number of records: " << totalStudents <<endl;
					cout << "Mean or average: " << averageMark <<endl;
				break;
		   case 2:
				cout << "Enter a Student Record: " <<endl;
				cout << "Student ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if (found!=-1)
				  {
					cout << "Record already exists." << endl << endl;
				  }
				else
				  {
					recPoint = new StudentRecord;
					populate(recPoint,id);
					dispOne(recPoint);
					student[totalStudents].studentID=recPoint->studentID;
					student[totalStudents].studentMark=recPoint->studentMark;
					totalStudents++;
				  }			
				break;
		   case 3:
				cout << "Find marks for ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if(found!=-1)
				  { 
					cout << "-------------" << endl;
					cout << "Student ID: " << student[found].studentID << endl;
					cout << "Mark/Grade: " << student[found].studentMark;
					if			  
							(student[found].studentMark >= 85)
					   studentGrade = 'H';
					else if (student[found].studentMark <85 && student[found].studentMark >=75)
					   studentGrade = 'D';
					else if (student[found].studentMark <75 && student[found].studentMark >=65)
					   studentGrade = 'C';
					else if (student[found].studentMark <65 && student[found].studentMark >=50)
					   studentGrade = 'P';
					else
					   studentGrade = 'F';
					cout << " (" << studentGrade << ")" << endl;
					cout << "-------------" << endl;
				  }
				else
				  cout << "Record does not exist." << endl << endl;		
				break;
		   case 4:
				cout << "Enter a Student Record: " <<endl;
				cout << "Student ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if (found!=-1)
				  {
					recPoint = delete StudentRecord;
					totalStudents--;
				  }
				else
				  cout << "Record does not exist.";
				break;
		   case 5:
				break;
		   default:
				cout << "Invalid selection. Please make a selection between 0-3.\n"
					 << endl;
				break;
	}	

	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics	2. Enter mark\n"
		 << "3. Find mark	4. Delete mark   5. List marks\n"
		 << "----------------------------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  }
system("Pause");
return 0;
}

void populate(StudentRecord *record, string stId)
{
	 record->studentID = stId;
	 cout << "Enter the corresponding mark: ";
	 cin >> record->studentMark;
	 return;
}

void dispOne(StudentRecord *contents)
{
	 cout << "\nThe contents of the record just created is:"
		  << "\nStudent ID: " << contents->studentID
		  << "\nMark: " << contents->studentMark << endl << endl;
	 return;
}

int linearSearch(StudentRecord*student,const string id,int totalStudents)

{
	int retValue=-1;
	for(int i=0;i<totalStudents;++i)
	{
			if(student[i].studentID == id)
			{
			  retValue=i;
			  break;
			}
	}
	return retValue;
}



The error I get is:
- 102 expected primary-expression before ';' token

Is This A Good Question/Topic? 0
  • +

Replies To: How to delete existing records?

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: How to delete existing records?

Posted 25 May 2009 - 05:56 PM

You want to delete an existing record? The only way I know of to do that explicitly would be to do
StudentRecord* blah= new StudentRecord();
delete blah;



But I think garbage collection may handle it for you in some compilers and everything gets deleted at the end of the program of course. :D

Oh and which line is 102 that's giving you the error? ;D
Was This Post Helpful? 1
  • +
  • -

#3 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

Re: How to delete existing records?

Posted 25 May 2009 - 06:17 PM

line 102 is "recPoint = delete StudentRecord;"

ok, I have replaced line 102 into "delete recPoint;"

It is error free but when I get to the process of deleting the mark, the program just corrupts and stops working?

**EDIT

Actually nevermind got it working lol
Had to add "recPoint = new StudentRecord;" above line 102.

Thanks for the help. I really appreciate it.

This post has been edited by dwade3: 25 May 2009 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

Re: How to delete existing records?

Posted 25 May 2009 - 06:45 PM

Ok, I just found a slight error in my coding.

I can add/delete all records except for student ID P1001. Even though I supposedly deleted P1001, I can still display it, and the marks for P1001 still gets accounted into the average mark.

Heres what my updated codes look like:

Globals:
const int SIZE=30;
struct StudentRecord
	 {
	   string studentID;
	   float studentMark;
	 };



Main declarations:
	StudentRecord student[SIZE] = 
				  {
				  {"P1001",78.50},
				  {"P1002",66}
				  };
	StudentRecord *recPoint;
	int totalStudents=2;


Case 4:
				cout << "Enter a Student Record:" << endl;
				cout << "Student ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if (found!=-1)
				  {
					 recPoint = new StudentRecord;
					 delete recPoint;
					 totalStudents--;
					 cout << endl << "Student Record has been deleted." << endl << endl;
				  }
				else
				  cout << "Record does not exist." << endl << endl;


What could be wrong??
Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: How to delete existing records?

Posted 25 May 2009 - 06:52 PM

What about setting the record's student ID field to an empty string, and the student mark field to zero?
Was This Post Helpful? 0
  • +
  • -

#6 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

Re: How to delete existing records?

Posted 25 May 2009 - 07:07 PM

Quote

What about setting the record's student ID field to an empty string


Sorry, I don't understand what you mean by that. Which section of the coding do you mean by that?
By the way, I have set the total marks to 0, and it helped a bit. Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

Re: How to delete existing records?

Posted 26 May 2009 - 12:32 AM

After numerous attempts I am still unsuccessful. I have discovered that the program WILL NOT delete student P1001 if there are still remaining records. For example, if there are only 2 records left (P1001 and P1002), and I enter P1001 to be deleted, it will instead delete P1002. And when I delete all records, this is what it shows on the statistics (case 1):
Number of records: 0
Average mark: -1.#IND
Standard deviation: -1.#IND

Here is my updated code (with standard deviation fixed):
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

const int SIZE=30;
struct StudentRecord
	 {
	   string studentID;
	   float studentMark;
	 };

void populate(StudentRecord *,string);
void dispOne(StudentRecord *);
int linearSearch (StudentRecord*student,const string id,int totalStudents);

int main()
{
	int menu;
	StudentRecord student[SIZE] = 
				  {
				  {"P1001",78.50},
				  {"P1002",66}
				  };
	StudentRecord *recPoint;
	int totalStudents=2;
	float totalMarks=0;
	float averageMark;
	string id;
	int found;
	char studentGrade;
	float stdDev;
	float sum;
	
	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics	2. Enter mark\n"
		 << "3. Find mark	4. Delete mark   5. List marks\n"
		 << "----------------------------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  while (menu != 0)
  {
	switch (menu)
	{
		   case 1:
				totalMarks=0;
				sum=0;
				for(int j=0;j<totalStudents;j++)
				  {
					totalMarks+=student[j].studentMark;
				  }
					averageMark=totalMarks/totalStudents;
				for(int j=0;j<totalStudents;j++)
				  {
					sum+=pow(student[j].studentMark-averageMark,2);
				  }
					stdDev=sqrt(sum/totalStudents);
				  cout << "Number of records: " << totalStudents <<endl;
				  cout << "Mean or average: " << averageMark <<endl;
				  cout << "Standard deviation: " << stdDev <<endl;
				break;
		   case 2:
				cout << "Enter a Student Record: " <<endl;
				cout << "Student ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if (found!=-1)
				  {
					cout << "Record already exists." << endl << endl;
				  }
				else
				  {
					recPoint = new StudentRecord;
					populate(recPoint,id);
					dispOne(recPoint);
					student[totalStudents].studentID=recPoint->studentID;
					student[totalStudents].studentMark=recPoint->studentMark;
					totalStudents++;
				  }			
				break;
		   case 3:
				cout << "Find marks for ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if(found!=-1)
				  { 
					cout << "-------------" << endl;
					cout << "Student ID: " << student[found].studentID << endl;
					cout << "Mark/Grade: " << student[found].studentMark;
					if			  
							(student[found].studentMark >= 85)
					   studentGrade = 'H';
					else if (student[found].studentMark <85 && student[found].studentMark >=75)
					   studentGrade = 'D';
					else if (student[found].studentMark <75 && student[found].studentMark >=65)
					   studentGrade = 'C';
					else if (student[found].studentMark <65 && student[found].studentMark >=50)
					   studentGrade = 'P';
					else
					   studentGrade = 'F';
					cout << " (" << studentGrade << ")" << endl;
					cout << "-------------" << endl;
				  }
				else
				  cout << "Record does not exist." << endl << endl;		
				break;
		   case 4:
				cout << "Enter a Student Record:" << endl;
				cout << "Student ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if (found!=-1)
				  {
					 recPoint = new StudentRecord;
					 delete recPoint;
					 totalStudents--;
					 cout << endl << "Student Record has been deleted." << endl << endl;
				  }
				else
				  cout << "Record does not exist." << endl << endl;
				break;
		   case 5:
				break;
		   default:
				cout << "Invalid selection. Please make a selection between 0-3.\n"
					 << endl;
				break;
	}	

	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics	2. Enter mark\n"
		 << "3. Find mark	4. Delete mark   5. List marks\n"
		 << "----------------------------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  }
system("Pause");
return 0;
}

void populate(StudentRecord *record, string stId)
{
	 record->studentID = stId;
	 cout << "Enter the corresponding mark: ";
	 cin >> record->studentMark;
	 return;
}

void dispOne(StudentRecord *contents)
{
	 cout << "\nThe contents of the record just created is:"
		  << "\nStudent ID: " << contents->studentID
		  << "\nMark: " << contents->studentMark << endl << endl;
	 return;
}

int linearSearch(StudentRecord*student,const string id,int totalStudents)
{
	int retValue=-1;
	for(int i=0;i<totalStudents;++i)
	{
			if(student[i].studentID == id)
			{
			  retValue=i;
			  break;
			}
	}
	return retValue;
}


Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to delete existing records?

Posted 26 May 2009 - 03:59 AM

Add this:
// show what's in the array
	for( int i=0; i < SIZE; i++ ) {
		 cout << "studentID" << i << " = " << student[i].studentID << " -- studentMark" << i << " = " << student[i].studentMark << endl;		
	}



Directly above the second instance of the menu in your program (about line 130).
Then compile and run your code

That loop will show you what is in your array after each action.

Is what you expected to have happen, happening?
Was This Post Helpful? 0
  • +
  • -

#9 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

Re: How to delete existing records?

Posted 26 May 2009 - 04:38 AM

I have added the statement above. The loop shows records from 1-30 (only 1 and 2 have student ID). When I delete a record, the student ID still shows in the loop. Does that mean it hasn't been deleted?
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to delete existing records?

Posted 26 May 2009 - 05:01 AM

The looping to 30 is because that is the size of your array.

I haven't worked through your code as it is now so I'm not sure what is happening in there so I'm not asking trick questions. Just prompting you to think about what is happening.

If the item is still in the array it does seem it hasn't been deleted at the time the loop is running.
Is that what you expect?
Where does the delete happen?
Where does the print happen?

Try doing a few different things.
Play with your code and see what is happening as different things happen.
Try to understand what your code is doing.

Try changing your code and see what the changes do.

All that loop is doing is showing you a snapshot of the array to help you understand and debug your code.
What mysteries does it bring into the bright light of day?

Even if it doesn't help you see the answers it will give you more useful info with which to ask for help.
Was This Post Helpful? 0
  • +
  • -

#11 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: How to delete existing records?

Posted 26 May 2009 - 12:44 PM

Is this the code where you expect the student to be deleted?
 cout << "Enter a Student Record:" << endl;
				cout << "Student ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if (found!=-1)
				  {
					 recPoint = new StudentRecord;
					 delete recPoint;
					 totalStudents--;
					 cout << endl << "Student Record has been deleted." << endl << endl;
				  }
				else
				  cout << "Record does not exist." << endl << endl;



What that does is when the student is found, it creates a NEW student record and deletes that one. Nothing ever happens to the one in the array. To delete the one from the array you would need something like
 if (found!=-1)
				  {
					 
					 delete student[found];
					 totalStudents--;
					 cout << endl << "Student Record has been deleted." << endl << endl;
				  }



Now if we examine the code where you add the student to the array:
case 2:
				cout << "Enter a Student Record: " <<endl;
				cout << "Student ID -> ";
				cin >> id;
				found = linearSearch(student,id,totalStudents);
				if (found!=-1)
				  {
					cout << "Record already exists." << endl << endl;
				  }
				else
				  {
					recPoint = new StudentRecord;
					populate(recPoint,id);
					dispOne(recPoint);
					student[totalStudents].studentID=recPoint->studentID;
					student[totalStudents].studentMark=recPoint->studentMark;
					totalStudents++;
				  }			
				break;



You are creating a studentrecord with the desired properties, then assigning the student IN THE ARRAY the properties you just assigned to the created students (I'm not even sure if there is a student in the array at that index). When you make the student with the data, just add him to the array:
 recPoint = new StudentRecord;
					populate(recPoint,id);
					dispOne(recPoint);
					student[totalStudents] = recPoint;
					totalStudents++;

OR 
student[totalStudents] = new StudentRecord;
					populate(student[totalStudents],id);
					dispOne(student[totalStudents]);
					totalStudents++;



Try this out and tell us how it works. I may've screwed up somewhere. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1