Thread: printing array

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    printing array

    I'm trying to print the name and number of votes of an entry if it exists. But, i can't figure out how to only print the entries that exist.

    Here is my code:
    Code:
    struct candidate
    {
    	string firstName;
    	int votes;
    	candidate *next;	
    };
    int main(int argc, char* argv[])
    {
    	ifstream inFile ( argv[1] );
    
    	if ( !inFile.is_open() )
    	{
    		cout << "Could not open file." << endl;
    	}
    	else
    	{
    		candidate *array1[12];
    		char name[20];
    		string currentEntry;			
    		
    		//Read names in from file. Hash them into table, or increase votes if entry is already in the table.
    		while(!inFile.eof())
    		{
    			int flag = 0;
    			int hashSpot;
    
    			inFile.getline(name, 20);
    			hashSpot = Hash(name,12);
    			currentEntry = name;			
    
    						
    			//If entry is not found, hash it into table.
    			if(flag == 0)
    			{					
    				array1[Hash(name,12)] = new candidate;
    				array1[Hash(name,12)]->firstName = name;
    				array1[Hash(name,12)]->votes = 1;
    				array1[Hash(name,12)]->next = NULL;					
    			}
    			else
    			{
    					
    			}
    						
    		}		
    		
    		//Print names		
    		for(int i = 0; i < 12 ; i++)
    		{			
    			cout << endl;
    			cout << 3 << ": ";
    			if(array1[i]->votes != NULL)
    			{
    				cout << array1[3]->firstName << " Votes: " << array1[3]->votes;
    			}
    			cout << endl;			
    		}		
    	}
    	
    
    	cin.get();
    	return 0;
    }
    The problem is that if statement. I need it to only print an entry if there is an entry there, but i can't figure out what to put in that if statement.
    I guess i'm a little confused with the array of pointers, and what the values of the array positions are before any entries. If i insert an entry, is the array pointing to that entry, or is it actually the entry.



    Any help would be appreciated.

    PS: This is suppose to be a hash table using chaining. Also, i can individually print the entries that i know were hashed, so they seem to be being created and hashed successfully.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use inFile.eof() to control a loop - read fAQ

    votes is integer, so probably simple

    if(array1[i]->votes >0)
    but you should be sure that votes is initialized to 0 at creation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    "if(array1[i]->votes >0)"

    I tired that, but the program still crashes.

    "but you should be sure that votes is initialized to 0 at creation"

    Do you mean initialize it right after the array is created. Like:
    Code:
    candidate *array1[12];
    for(int i = 0; i < 12; i++)
    {
    	array1[i]->votes = 0;
    }
    This is how i tried initializing it.
    Isn't the array suppose to be empty (pointing to null) until i put an entry in there? Then, instead of pointing to null, it would point to an entry. So, how could i set votes to 0 if the entry hasn't been created yet? Maybe i'm initializing it incorrectly or something.

    Thanks

    PS: This is the example i am trying to follow: http://www.sparknotes.com/cs/searchi.../section1.html
    IDE - Visual Studio 2005
    Windows XP Pro

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this
    Code:
    candidate *array1[12];
    array is not empty - it is just not initialized
    this
    Code:
    candidate *array1[12] = {0};
    should initialize all members to NULL

    so you could check
    Code:
    if(array[i])
    { 	
       //do something
    why you do not use std::vector instead?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >				array1[Hash(name,12)] = new candidate;
    >				array1[Hash(name,12)]->firstName = name;
    >				array1[Hash(name,12)]->votes = 1;
    >				array1[Hash(name,12)]->next = NULL;
    Why not use hashSpot?
    Code:
    				array1[hashSpot] = new candidate;
    				array1[hashSpot]->firstName = name;
    				array1[hashSpot]->votes = 1;
    				array1[hashSpot]->next = NULL;
    >The problem is that if statement. I need it to only print an entry if there is an entry there
    You could initialize the array of pointers as null pointers (using either NULL or 0}:
    Code:
    		candidate *array1[12] = {NULL};
    Then the if() statement would be:
    Code:
    			if(array1[i] != NULL)

  6. #6
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ahh, so i was initializing it wrong.
    Thanks guys.

    "why you do not use std::vector instead?"
    I haven't learned that yet, but i'll look into it and see if i can figure out how it works.

    "Why not use hashSpot?"
    Thank you for pointing that out. I didn't even notice.
    IDE - Visual Studio 2005
    Windows XP Pro

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and you may want to look at std::list as well
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Ahh, so i was initializing it wrong.
    If you initialize the first element, the rest are default initialized (in this case to the null pointer). In this case, it's equivalent to:
    Code:
    		candidate *array1[12] = {NULL, NULL, NULL, NULL, NULL, NULL,
    		NULL, NULL, NULL, NULL, NULL, NULL};
    For an int array, they would default to 0 after the first element. For a double array, they would default to 0.0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing half a char array
    By theoneandonly in forum C Programming
    Replies: 19
    Last Post: 11-11-2006, 07:27 AM
  2. Replies: 3
    Last Post: 11-03-2003, 08:55 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM