Add and delete functions

This is a discussion on Add and delete functions within the C++ Programming forums, part of the General Programming Boards category; I am a little mifted in my add and delete functions. This program is suppose to add and delete video ...

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

    Question Add and delete functions

    I am a little mifted in my add and delete functions. This program is suppose to add and delete video titles and their info from the database/file. The program allows me to "add", but it somehow transforms it into wierd characters that look like an "I" from a foriegn language. On top of that It only copies the title at the end of each string of wierd characters.

    As for the delete function, the delete reflects in the output screen but not in the file where the information is located. Is there another way I can delete a record?

    Code:
    void add_Video(int& numMovies, Video inventory[])
    {
       char video_title[31];	//	initialize to add a title of movie
    
       char video_star[51];		//	initialize to add a name of movie stars
    
       char video_type[10];		//	intialize to add a the category the movie is in
    
       char video_length[7];	//	intialized to add a the length of the movie
    
       char videoNum[4];		//	initialized to add the number of copies in the store's inventory
    
       cout << " Enter the title of the movie you would like to add to the list:" << endl << endl;
       gets(video_title);
       cout << endl;
    
       for (int num = 0; num < numMovies; num++)
       {
    	   if (strcmp(inventory[numMovies].title, video_title)==0)
    	   {
    		   cout << "That title is already in the stores files." << endl;
    	       cout << "If you wish to change the number of copies, first delete the old file." << endl << endl;
    	   }
    
           if(strcmp(inventory[numMovies].title, video_title)!=0)
    	   {
    		   cout << " Enter the name(s) of the actor/actress in the movie :  " << endl << endl;
               gets(video_star);
               cout << endl;
    
               cout << " Enter the type of movie it is: " << endl << endl;
               cout << "Action    Comedy    Adventure" << endl;
               cout << "Children  Drama     Western" << endl << endl;
               gets(video_type);
               cout << endl;
    
               cout << " Enter the length of the movie in minutes (130min): " << endl << endl;
               gets(video_length);
               cout << endl;
    
               cout << " Enter the number of copies in inventory: " << endl << endl;
               gets(videoNum);
               cout << endl;
    
               numMovies++;
               strcpy(inventory[numMovies].title, video_title);  // Copy ALL fields to DB
               strcpy(inventory[numMovies].star, video_star);
               strcpy(inventory[numMovies].type, video_type);
               strcpy(inventory[numMovies].length, video_length);
               strcpy(inventory[numMovies].numCopies, videoNum);
               cout << "The title just added to the list is: " << endl;  // For debug purposes
               cout << inventory[numMovies].title << endl;
    
    		   }		// end Else statement
    	   } // end for loop
    
    }  //  end add function
    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;
    	}
    
    	else
    	{
    		cout << "There is nothing to delete." << endl << endl;
    	}
    
    
    	for (int deleteNum = 0; deleteNum < numMovies; deleteNum++)
    	{
    
    		if (strcmp(inventory[deleteNum].title,title_entry)==0)
    		{
    			number = deleteNum;
    
    			strcpy (inventory[numMovies+1].title, inventory[deleteNum].title);
    			strcpy (inventory[numMovies+1].star, inventory[deleteNum].star);
    			strcpy(inventory[numMovies+1].type,inventory[deleteNum].type);
    			strcpy(inventory[numMovies+1].length,inventory[deleteNum].length);
    			strcpy(inventory[numMovies+1].numCopies,inventory[deleteNum].numCopies);
    		    numMovies--;
    		    cout << endl << strupr(title_entry) << " has been deleted from the collection." << endl << endl;
    		}
    
    	}
    	
    	if (number < 0)
    	{
    	    cout << endl << "Video title " << strupr(title_entry) << " was not found in the collection." << endl << endl;
    	}
    
    }	//	End Delete function
    Last edited by Ana Val sazi; 06-16-2002 at 08:18 AM.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    85
    Gosh! this board is for C, not C++.

    + In the function add_Video():
    in the FOR loop, you need to change array INDEX numMovies to num in:
    if (strcmp(inventory[numMovies].title, video_title) ==0);
    inventory[numMovies] change it to inventory[num].

    I also wonder how you did in MAIN() function about
    define variables and actual parameters?
    Did ya allocate enough space for those variables
    in main(). Go post your main() function!
    DV007
    Last edited by dv007; 06-12-2002 at 10:00 AM.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    gets(video_title);
    Use cin.get (C++) or fgets (C/C++) instead. The gets function is not save (buffer overflow when you enter more then 30 characters).
    Code:
    // reads up to 30 characters
    cin.get(video_title, 31);
    And here a small example passing an integer to a function and change it:
    Code:
    void add(int *n)
    {
      (*n)++;
    }
    
    int main()
    {
      int n = 1;
      add(&n);
      cout << n << endl;
      return 0;
    }
    Maybe this will help...

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    13
    Thanks for your help as well as bringing a couple of things to my attention.

    The cin.get advice was helpful but did not solve my dilema in the extra machine language being outputed to the fstream and file. Neither did cin.ignore help.

    However on a happier note I did get my delete function working! I changed it to
    Code:
     strcpy(inventory[deleteNum].title, inventory[numMovies-1])
    This actually does delete the specific record as well as move the files up one. Unfortunately it actually means that in the output file there is a double record still kept of the last record. How do I solve this problem? Any ideas?
    The answer, my friend, is blowing in the wind...

  5. #5
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Ana Val sazi wrote:
    Code:
    strcpy(inventory[deleteNum].title,inventory[numMovies-1])
    strcpy copys string from src to destination. I don't think Video is a string. Did you mean that you moved all the Video's strings from the end to inventory[deleteNum]? This could of course work if the order doesn't matter(usually it does). Then you just need to decrease numMovies by one: numMovies--;

    If the order matters, you can use some cleverer data structure, maybe one in the STL.

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

    OOps!

    May I should have clarified a couple of things.

    Video is a struct containing variables for title, movie star, etc...

    Here, I guess I'll just post a the first portion of my main...

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <fstream>
    #include <stdio.h>
    
    using namespace std;
    
    struct Video
    {                   // defining fields of the record
       char title[31];	//	initialize for title of movie
       char star[51];	//	initialize for name of movie stars
       char type[10];	//	intialize for the category movie is in
       char length[7];	//	intialized for the length of the movie
       char numCopies[4];	//	initialized for the number of copies
    };     
    
    void print_Menu();					//	Prints a menu to the screen
    void add_Video(int&, Video[]);		//	Adds a video to the list in the array
    void delete_Video(Video[], int&);	//	Deletes a selected movie from the inventory file
    void print_List(int, Video[]);		//	Prints to the screen a list of the videos in the array
    void output_Records(int&, Video[]);
    void space();
    void search(Video[], int);
    
    int main()
    {
    
       int numMovies;           // initialize variable to designate a record in the array
       Video inventory[100];            // This is the array of videos
       char choice;					//	initialize for user's selection from the menu
       char again;
       fstream videofile;                // Declare variable which refers to the file
    
       system("cls");
       videofile.open("a:\\videofile.txt");	//	opening file to be read
       videofile >> numMovies;
    
       for(int num=0; num < numMovies; num++)//	Reads from file into array
       {
    	   videofile.ignore(100,'\n');
    	   videofile.get(inventory[num].title,31);
    	   videofile.ignore(100,'\n');
           videofile.get(inventory[num].star,51);
    	   videofile.ignore(100,'\n');
    	   videofile.get(inventory[num].type,10);
    	   videofile.ignore(100,'\n');
    	   videofile.get(inventory[num].length,7);
    	   videofile.ignore(100,'\n');
    	   videofile.get(inventory[num].numCopies,4);
      
       }
    
       if (!videofile)
       {
    	   cout << "Unable to open input file.\n";
       }
    
       while  ((again != 'N') && (again != 'n'))
       {
    	   print_Menu();
    	   fflush (stdin);
    	   space();
    	   cout << "What is your choice?" << endl;
           cin >> choice;
    
          switch (toupper(choice))
    	  {
        	 case 'A':
                 
                 fflush(stdin);		//	Clears buffer for next input
    			 space();
    			 add_Video(numMovies, inventory);
    		     break;
    
    		 case 'D':
                 if(numMovies < 0)
    			 {
    				 space();
    				 cout << "There are no movies in the file" << endl;
    				 break;
    			 }
    The answer, my friend, is blowing in the wind...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. help with classes and member functions
    By syner in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21