Reputation: 47
I'm working my last programming assignment of the semester (my last computer program ever I guess seeing as I changed majors ha) and have kind of run into a roadblock. The assignment asks us to write two programs, one program reads in some information from a file and creates a hash table which it saves to an output file. The second program is supposed to allow the user to enter a key and the program is to search that output file for the key and return the information it contains if found.
I'm still in the planning stages of this program, I like to write them all out on paper before I actually start coding since for some reason it kind of helps me figure out what's going on better, but I think my program that creates the hash table should work since it's almost identical to a program we had to write a couple weeks ago that simply created the hash table (fingers crossed), but I'm having a problem on the search program. My search program really only has one method to search the file for the key entered by the user. It may be easier to explain my specific issue if I include the code for the search function that I used in the program I mentioned that only created the hash table.
void Hash::findItem(int key) {
int index = hash(key);
bool wasFound = false;
record* ptr = hashTable[index];
while(ptr != NULL) {
if(ptr->key == key) {
wasFound = true;
key = ptr->key;
}
ptr = ptr->next;
}
if(wasFound == true) {
cout << key << " " << name << " " << code << " " << cost << " " << index << endl;
}
else {
cout << "The key " << key << " was not found in the table." << endl;
}
}
Hash is the name of the class I used in my program that creates the table, and record is the name of the struct I used to hold the information for each record. Everything else should be pretty self-explanatory.
So here's where I'm running into trouble. Pretty much everything in this code was actually declared and defined in my first program, but this method/function (not sure on the correct terminology, I'm taking 3 programming classes in 3 different languages so I live in a constant state of confusion) will only be used in my search program. I'm not sure how to go about connecting the two programs (if that makes sense). Like, how can I make the things I created in my first program usable for my search program?
I apologize for rambling, I'm no better at short and sweet than I am at programming. And hopefully that's pretty clear. If I need to include any additional information please let me know and I'll be happy to oblige. Thanks in advance for any help you fine folks are able to provide.
Note I used "inheritance" as one of the tags for this post, I'm not really sure that this is an issue with inheritance, but I felt like it might be. If you feel otherwise please let me know and I can remove it as I want to be sure this doesn't wind up in the wrong place and wast anyone's time.
Upvotes: 0
Views: 601
Reputation: 8313
Let say you have 4 files:
all cpp files should include the common.h
now you can compile as follows: you can add -g
to each line if you want to debug.
g++ -c common.cpp -o common.o
g++ -c main1.cpp -o main1.o
g++ -c main2.cpp -o main2.o
g++ main1.o common.o -o main1
g++ main2.o common.o -o main2
the first 3 lines are for compile source file to object file.
the last 2 are to link objects to executable.
note you only need to have 4 files...
Upvotes: 2