Empty an Array

This is a discussion on Empty an Array within the C++ Programming forums, part of the General Programming Boards category; I have an array that I need to update after using a function. So I need to know how to ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    7

    Empty an Array

    I have an array that I need to update after using a function. So I need to know how to empty it before I call on the function that reads everything into the array. Simple enough to solve?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Use memset. Simply pass the array, size and 0 and the array will be "emptied."
    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.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Unless it's a char array, which by convention uses \0 to mark the end, then there is no concept of an 'empty' value for all the other numeric types. Pointers would of course use NULL to indicate emptyness.

    The easiest thing to do is
    a) pass a parameter indicating the max size of the array
    b) return a result indicating the number of array entries actually used.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    7
    The Array contains String, Float and int. And Im kind of a newbie at c++ so I don't exactly know what your talking about..

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    An array cannot contain different types at the same time. Do you mean that you have more than one array? Or maybe it isn't a normal array?

    Try posting the code that declares the array(s) or explain in much more detail what you're doing.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Of course, memset would be a bad idea if it's an array of classes. So posting the code would eb the optimal idea, I believe.
    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.

  7. #7
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Since this is C++, I might recommend using vectors (search that in google), because they're much easier to use. But everyone else's way would work too.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    7
    Code:
    void ReadIntoArray(void){
    
    				varuread.open("varulager.txt");
    
    				if(!varuread){
    					cout<<"Filen kunde inte öppnas";
    					exit(1);
    				}
    				
    				AntalVaror=0;
    
    				getline(varuread, tmp);
    
    				while (!varuread.eof()){
    
    					AntalVaror=AntalVaror+1;				
    					Register[AntalVaror] = 	parseRow(tmp);	
    					getline(varuread, tmp);		
    				}
    						
    			
    			   varuread.close();
    		//	   cout << AntalVaror << " 1: " << Register[1].aname << "\n" << endl;
    			
    				
    				
    
    }
    
    
    void NyVara(int artnr, string artname, float artpris){
    
    		string s1,s2;
    		ofstream outFile;
    
    
    
    					
    
    					outFile.open("varulager.txt", ios::app);
    
    					if (!outFile) {
    						cerr << "Can't open output file " << "varulager.txt" << endl;
    						exit(1);
    					}
    
    
    						
    						outFile << artnr << " " << artname << " " << artpris <<  endl;
    						
    		
    					
    						outFile.close();
    						AntalVaror=AntalVaror+1;
    						
    						
    
    
    
    }
    I want to empty the Register[] after AntalVaror=AntalVaror+1 in the NyVara function and then call the function again so it updates itself.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think your question has already been answered.

    a) Use a std::vector and clear it with clear().
    b) (More work) Keep a variable that shows how many items the array actually contains. To clear, set this variable to 0. Simply don't access items beyond and including that index.

    In addition, this is how you should read from a file:
    Code:
    while (getline(varuread, tmp)){ //eof does not work as you expect, see FAQ	
        Register[AntalVaror] = parseRow(tmp);	
        AntalVaror=AntalVaror+1; //indexing starts from 0, increase after accessing
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Code:
    void ReadIntoArray(void){
    	varuread.open("varulager.txt");
    
    	if(!varuread){
    		cout<<"Filen kunde inte öppnas";
    		exit(1);
    	}
    	
    	AntalVaror=0;
    	getline(varuread, tmp);
    
    	while (!varuread.eof()){
    		AntalVaror=AntalVaror+1;				
    		Register[AntalVaror] = 	parseRow(tmp);	
    		getline(varuread, tmp);		
    	}
    
    	   varuread.close();
    //	   cout << AntalVaror << " 1: " << Register[1].aname << "\n" << endl;
    }
    
    void NyVara(int artnr, string artname, float artpris){
    	string s1,s2;
    	ofstream outFile;
    
    	outFile.open("varulager.txt", ios::app);
    	if (!outFile) {
    		cerr << "Can't open output file " << "varulager.txt" << endl;
    		exit(1);
    	}
    
    	outFile << artnr << " " << artname << " " << artpris <<  endl;
    	outFile.close();
    	AntalVaror=AntalVaror+1;
    }
    And this is how you should indent.
    Further,
    Code:
    AntalVaror=AntalVaror+1;
    Is the same as
    Code:
    AntalVaror++;
    And
    Code:
    	if (!outFile) {
    		cerr << "Can't open output file " << "varulager.txt" << endl;
    		exit(1);
    	}
    This is generally considered bad practice. Your function should return whether it failed or not, and preferably an error value to say what went wrong and the caller (main usually) should handle the error.
    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.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Code:
    	if (!outFile) {
    		cerr << "Can't open output file " << "varulager.txt" << endl;
    		exit(1);
    	}
    This is generally considered bad practice. Your function should return whether it failed or not, and preferably an error value to say what went wrong and the caller (main usually) should handle the error.
    Unless you're talking to esbo.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Esbo is history. No more esbo now
    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. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 05:27 PM
  5. making an empty 2-D array
    By starX in forum C Programming
    Replies: 4
    Last Post: 02-08-2002, 01:09 AM

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