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.