Having a problem allocating space for an array of structures

This is a discussion on Having a problem allocating space for an array of structures within the C++ Programming forums, part of the General Programming Boards category; When the code runs through the first time, it stores everything good into the first spot of the array but ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Having a problem allocating space for an array of structures

    When the code runs through the first time, it stores everything good into the first spot of the array but when it runs through the second time to get the input from the second line of the text file, it overwrites the stuff from the first run through.

    I think it has to do with

    NewArray = new Book;

    but I don't know how to fix the logic on it.

    TEXT FILE
    Code:
    Harry Potter IV | J.K. Rowling | 747583 | DRAMA | PRINTED | 614 | false |
    Lord Of The Rings | Jesse Moreland | 10011445 | DRAMA | AUDIO | true | 120 | Johnathon D. Smith |
    CODE
    Code:
    		// Loops until the end of file
    		while(!myfile.eof())
    		{	
    
    			// Read line of text file
    			char temp[128];
    			char * temp2 = "n";
    			myfile.getline(temp, 128, '\n');
    
    			// Allocate space in Book Array for another element
    			NewArray[counter] = new Book;
    
    			// Set all elements of struct book to read in file. I have to read in the whole line
    			// and set it to all the individual elements of book.
    			int i = 0;
    			while(i < 5)
    			{
    				temp2 = strtok( (i > 0 ? 0 : temp), "|");
    
    				if(i == 0)
    					strcpy(NewArray[counter].title, temp2);
    
    				else if (i == 1)
    					strcpy(NewArray[counter].author_name, temp2);
    
    				else if (i == 2)
    					strcpy(NewArray[counter].isbn, temp2);
    
    				else if (i == 3)
    				{
    					if(NewArray[counter].genre == 0)
    						NewArray[counter].genre = HORROR;
    
    					else if(NewArray[counter].genre == 1)
    						NewArray[counter].genre = SCIFI;
    
    					else if(NewArray[counter].genre == 2)
    						NewArray[counter].genre = COMEDY;
    
    					else if(NewArray[counter].genre == 3)
    						NewArray[counter].genre = DRAMA;
    
    					else if(NewArray[counter].genre == 4)
    						NewArray[counter].genre = ACTION;
    				}
    				else if (i == 4)
    					if(strcmp(temp2, " PRINTED ") == 0)
    					{
    						for(int j = 0; j < 2; j++)
    						{
    							temp2 = strtok(0, "|");
    							if( j == 0 )
    								NewArray[counter].media.hardcopy.num_pages = atof(temp2);
    							else if( j == 1)
    								NewArray[counter].media.hardcopy.paperback = temp2;
    						}
    					}
    					else if(strcmp(temp2, " AUDIO ") == 0)
    					{
    						for(int j = 0; j < 3; j++)
    						{
    							temp2 = strtok( 0, "|");
    							if( j == 0)
    								NewArray[counter].media.electronic.cd = temp2;
    							else if ( j == 1)
    								NewArray[counter].media.electronic.listening_time = atof(temp2);
    							else if (j == 2)
    								strcpy(NewArray[counter].media.electronic.narrator_name, temp2);
    						}
    					}
    					//increment i
    					i++;
    			}

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    NewArray[counter] = new Book;
    Perhaps you should increment counter.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Your gonna get an extra run checking eof like that; eof isn't set until after an attempt to read past the end of the file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,992
    Why are you not using std::string and std::vector?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with array of pointers to structures and such
    By bungkai in forum C Programming
    Replies: 6
    Last Post: 07-09-2011, 05:31 PM
  2. Problem with array of structures
    By Sonia in forum C Programming
    Replies: 10
    Last Post: 10-12-2010, 04:37 AM
  3. allocating for structures
    By Bladactania in forum C Programming
    Replies: 2
    Last Post: 02-16-2009, 09:46 PM
  4. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  5. Allocating memory of VL structures
    By darsatdusk in forum C Programming
    Replies: 8
    Last Post: 07-09-2008, 04:43 PM

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