Thread: Writing past ...

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

    Writing past ...

    I'm getting some funny characters in my text file as well as on my screen.
    Iv'e noticed that it dosen't even write any info until after the alloted space...and I'm not even using all of the space. Please take a look at the enclosed file, where I tried to input TITANIC's info, to see what I mean. Thank You
    Code:
    void add_Video(int& numMovies, Video inventory[])
    {
       char video_title[30];	//	initialize to add a title of movie
       char video_star[50];		//	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;
       fflush (stdin);
       cin.get(video_title,30);
       cout << endl;
    
       for (int num = 0; num < numMovies; num++)
       {
    	   if (strcmp(inventory[num].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;
    	       fflush (stdin);
               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;
    		   cin.ignore();
               gets(videoNum);
               cout << endl;
    
               numMovies++;
               strncpy(inventory[numMovies].title, video_title);  // Copy ALL fields to DB
               strncpy(inventory[numMovies].star, video_star);
               strncpy(inventory[numMovies].type, video_type);
               strncpy(inventory[numMovies].length, video_length);
               strncpy(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
    Last edited by Ana Val sazi; 06-27-2002 at 08:19 PM.
    The answer, my friend, is blowing in the wind...

  2. #2
    Unregistered
    Guest
    drop the fflush(stdin) stuff. fflush() is for ostreams only. You need to use ignore() if you want to clear an istream buffer, and you might want to do so if you are mixing >> and getline() or get() in the same program.

    use >> or getline() to enter strings.

    use getline() rather than >> to input any string that may contain whitespace such as a title or a name, etc.

    use one less than the size of the buffer to read to when indicating maximum size for getline() to read in:

    char title[30];
    cout << "enter the name of a movie" << endl;
    cin.getline(title, 29, '\n');

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Can you post the code which is reading the data from the file.

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

    Posting code

    Code:
    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,30);
    	   videofile.ignore(100,'\n');
           videofile.get(inventory[num].star,50);
    	   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);
      
       }
    and

    Code:
    	  output_Records(numMovies, inventory);
    
       }
    
    
       videofile.close();		// Writing out to the text file
    
       return 0;
    
    } // end of main
    Code:
    //***********************************************************************
    
    
    void output_Records(int& numMovies, Video inventory [])   // Reading from file into the array
    {
    	fstream videofile;					//	declaring
    	videofile.open("a:\\videofile.txt");	//	opening file to be read
    
        videofile << numMovies << endl;
    
        for(int num=0; num < numMovies; num++)
    	{
    		videofile << strupr(inventory[num].title) << endl;
    		videofile << strupr(inventory[num].star) << endl;
    	    videofile << strupr(inventory[num].type) << endl;
    	    videofile << strupr(inventory[num].length) << endl;
    	    videofile << strupr(inventory[num].numCopies) << endl;
    	}
    }
    //***********************************************************************
    Hope this clears up things
    Last edited by Ana Val sazi; 06-27-2002 at 08:21 PM.
    The answer, my friend, is blowing in the wind...

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    208
    I figured I would post this here instead of starting a new thread.
    For sum reason my program is skipping the cin.getline() function.
    It does everyhing else corectly but it won't let me input the commnet I want to.

    Code:
    void main()
    { cout<<"\nEnter Suscribers First Name: ";
      cin>>first;
      cout<<"\nEnter Suscribers Last Name: ";
      cin>>last;
      cout<<"\nEnter Suscribers Phone Number: ";
      cin>>phone;
      cout<<"\nEnter Suscribers House Number: ";
      cin>>housenum;
      cout<<"\nEnter Suscribers Street: ";
      cin>>street;
      cout<<"\nEnter Suscriber Number: ";
      cin>>susnum;
      cout<<"\nEnter Any Special Comments About This Person (Must Be Less The 20 Chars)\n";
      cin.getline(comment, 20, '\n');
      strcpy(filename,last);
      strncat(filename,".txt",4);
      write();
      system ("PAUSE");//this is where the problem is it skips the input
    }

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Kas, Look up cin.ignore()

    AVS, you need to list the flags for the fstream object when you open the file, i.e. ios::in.

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

    Hi Kas

    You need to declare common;
    ie
    char common[20];
    ...before you can use it effectively.
    I recently switched all gets to cin.getline
    Code:
    fflush (stdin);
       cin.getline(video_title,30,'\n');
    I also used fflush to flush the buffer scince I had problems using cin.ignore ( it didn't allow for the next input).

    p.s. I was told by someone earlier that it is bad practice to mix cin types. perhaps using either cin>> or cin.getline might help. Double check to make sure you have also included the appropriate library if you have not already done so.
    Last edited by Ana Val sazi; 06-29-2002 at 03:35 AM.
    The answer, my friend, is blowing in the wind...

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

    p.s.s

    What would be the purpose of putting a pause at the end of your menu? Is the a portion of a code intended for a loop?
    The answer, my friend, is blowing in the wind...

  9. #9
    Unregistered
    Guest
    >> removes leading whitespace from the input buffer and terminates at first trailing whitespace but it doesn't remove the terminating whitespace from the input buffer

    get() has three parameters, all of which can be defaulted. get does not remove whitespace, and it does not remove the terminating char from the input buffer

    getline() has three parameters, only the last of which can be defaulted. getline() does not ignore whitespace, but it does remove the terminating char from the input buffer.

    in order to clear the input buffer of the terminating char for >> or get(), assuming you wish to do so, you should use ignore(). ignore() removes all char in the input buffer until a given number of char have been removed, or until the first terminating char is found, whichever comes first. The terminating char is removed from the input buffer. Both the number of char to be ignored and the terminating char can be defaulted.

    If you use a >> before a get() or getline() you will end up with the terminating whitespace left in the input buffer by >> in the variable used by get(). If that whitespace happens to be the terminating char for get() or getline(), then there will be no input into the buffer used by get() or getline. Likewise, calling get() before getline() will leave the terminating char in the input buffer and can appear in the buffer used by getline() or cause termination of getline() without input into the buffer used by getline() if it is a terminating for getline(). Therefore, when mixing different istream methods in a program, you need to be aware of what is in the input buffer, and clear it appropriately using ignore().

    EOF is a whitespace char that terminates all istream methods automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing functions
    By jlmac2001 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2003, 12:06 AM
  2. Sizeof - Writing past the end of an array
    By milkydoo in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2003, 02:20 AM
  3. writing mathematical operations in C
    By p_s_ross in forum C Programming
    Replies: 2
    Last Post: 06-13-2003, 03:04 AM
  4. Experienced coders read. ( Common/efficient writing )
    By knave in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2003, 09:07 PM
  5. Writing Bitmap Files
    By HappyDude in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 05:48 PM