-
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.
-
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
-
"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
-
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?
-
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)
-
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.
-
and you may want to look at std::list as well
-
>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.