Thread: Trailing Record

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    13

    Trailing Record

    I have a delete function that will delete the specified record. In turn it will take the last record and place it in the the deleted spot. However it does not erase the record at the end, causing a duplicate entry in the videofile.txt
    While the the last record is not read, because the number of movies has been decremented, it will be read when another movie is added. How to I delete this "trailing record"?
    On a side note, this program deals with a struct and chars, if that helps anything.

    Code:
    void delete_Video(Video inventory [], int& numMovies)
    {
    
    	fstream videofile;					//	declaring
    	videofile.open("a:\\videofile.txt");	//	opening file to be read
    
    	char title_entry[31];
    	int number = -1;
    
    	if (numMovies!=0)
    	{
    	system("cls");
    	cout << "Please enter the name of the title you would like to delete:" << endl;
        fflush(stdin);		//	Pauses screen to allow user to input
    	gets(title_entry);	//	User inputs the movie title they wish to delete
    	cout << endl;
    
    	for (int deleteNum = 0; deleteNum < numMovies; deleteNum++)
    	{
    
    		if (strcmp(inventory[deleteNum].title,title_entry)==0)
    		{
    			number = deleteNum;
    
    			strcpy (inventory[deleteNum].title, inventory[numMovies-1].title);
    			strcpy (inventory[deleteNum].star, inventory[numMovies-1].star);
    			strcpy(inventory[deleteNum].type,inventory[numMovies-1].type);
    			strcpy(inventory[deleteNum].length,inventory[numMovies-1].length);
    			strcpy(inventory[deleteNum].numCopies,inventory[numMovies-1].numCopies);
    		    numMovies--;
    		    cout << endl << strupr(title_entry) << " has been deleted from the collection." << endl << endl;
    		}
    
    	}	//	End For Loop
    	
    	if (number < 0)
    	{
    	    cout << endl << "Video title " << strupr(title_entry) << " was not found in the collection." << endl << endl;
    	}		//	End Second IF Statment
    
    	}	//	End First IF Statement
    
    	else
    	{
    		cout << "There is nothing to delete." << endl << endl;
    	}
    
    }	//	End Delete function
    The answer, my friend, is blowing in the wind...

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    shouldn't the 1 in this "inventory[numMovies-1].title" be incremented until it == the diference between deletenum and nummovies? i'm shooting from the hip though

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    13

    Do You mean...

    Do You mean I should write it like this;
    Code:
    inventory[deleteNum].title ==inventory[numMovies-1].title)
    and place it after the for loop is completed? (Because, If I understand my own writting.... deleteNum will==numMovies when the for loop is finnished.)
    The answer, my friend, is blowing in the wind...

  4. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    sorry about my previous explanation (sometimes i'm ashamed to call my self an english speaker)

    this is what i thought, but i don't have a lot of experience so don't chase any wild ducks on my account

    Code:
    for (int i=0; i<totalnum-deletenum; ++i)
    {
    x=i-1;
    strcpy (inventory[deleteNum+x].title, inventory[deletenum-i].title);
    strcpy (inventory[deleteNum+x].star, inventory[numMovies-i].star);
    strcpy(inventory[deleteNum+x].type,inventory[numMovies-i].type);
    strcpy(inventory[deleteNum+x].length,inventory[numMovies-i].length);
    strcpy(inventory[deleteNum+x].numCopies,inventory[numMovies-i].numCopies);
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When you say delete the item, do you mean from the array in memory, or the file on disk?

    - If it's the array:
    I presume you are using an array (called inventory) that is statically sized. If so, you can't remove an element from it. I'd suggest just blanking out the text of the inventory on the element you want to delete/hide (the last one in this case).

    Your strcpy() lines look OK to me. Therefore, don't do this. it's wrong:
    Code:
    inventory[deleteNum].title ==inventory[numMovies-1].title)
    - If it's the file on disk:
    I see that you are opening videofile.txt, but you are not making any attempt to write to it (at least, not in the code you've posted).


    >fflush(stdin);
    >gets(title_entry);
    These are both bad practice. There are better ways to flush and get input. I suggest you learn them soon

    Hope this helps....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    13

    in file

    I can delete the record in the memory, I can not delete the record in the file. In fact with the suggestive code displayed on this page
    The answer, my friend, is blowing in the wind...

  7. #7
    Unregistered
    Guest
    You can't erase the data in an array element once it is there, but you can effectively ignore it by keeping track the last element which contains useful information. Say you have an array of char:

    char dah[4];
    dah[0] = 'p';
    dah[1] = 'h';
    dah[2] = 't';
    dah[3] = 'i';

    you can replace h with i by doing this:

    dah[1] = dah[3];

    but dah[3] still has the char i in it after the change. To show this do this:

    for(i = 0; i < 4; i++)
    cout << dah[i];

    You can shift the value in dah[2] to dah[1] and the value in dah[3] to dah[2], but the value in dah[3] will still be there after the shifting.

    dah[1] = dah[2];
    dah[2] = dah[3];
    for(i = 0; i < 4; i++)
    cout << dah[i];

    However, if you keep track of the number of pertinent elements then you can ignore dah[3] after the deletion.

    int index = -1;
    char do[4];
    int i;
    do[0] = 'p';
    index++;
    do[1] = 'h';
    index++;
    do[2] = 't';
    index++;
    do[3] = 'i';
    index++;

    for(i = 0; i <= index; i++)
    cout << do[i];

    //delete h and replace with last element in do[]
    do[1] = do[index];
    //now change index to indicate the "deletion"
    index--;

    //now display just the "pertinent" values in do[]
    for(i = 0; i <= index; i++)
    cout << do[i];

    ------------------------------------------------------
    Another way to accomplish the same thing is to use an array of struct, and have a struct member called flag to act as a flag. If flag has a given value then display the data members of the struct. If flag has a different value then don't display the data members of the struct. Just make sure the flag is set appropriately for each element by explicitly setting it each time you make any changes.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    13

    Tinkering around

    THanks for the help you all are providing. Ive tinkered around with both blight2c's and hammer's idea and this is what I came up with;
    Code:
    strcpy(inventory[deleteNum].title," ");
    This will delete the specific record that the user selects. unfortunately it will also Not read any record that falls after that... any ideas anyone?
    The answer, my friend, is blowing in the wind...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. behind and confused
    By steviecrawf in forum C Programming
    Replies: 1
    Last Post: 11-09-2001, 12:51 PM