ok, heres the deal. Below I the input from my file.
3332453:Ryan:77 Easy Street
3324532:Tom:1 Victory Lane
3326854:Gary:69 Sexual Ave
3304357:Susan:11 Straight Road
3343651:Frank:96 Backwards street
What i am trying to do is read in the number, then the name, followed by the address and store them into a BST
. After checking through everything, I think my insert is working fine. It correctly inserts the Name and Address into each position, but key1 keeps being either 0 or -955472. The above numbers are all suppose to be 7 digits long (phone numbers). Below is what I would consider my closest attempt I have so far. Im not asking for someone to just give me code (although it would help) but rather explain to me why my implementation is not working, as well as how I can improve it. Thank you.
ifstream dataFile;
dataFile.open ("/Users/revaes12/Desktop/BinarySearch/BinarySearch/phone.dat.rtf");
for (int counter = 0; counter < 5; counter++)
{
getline(dataFile, tmp, ':');
person[counter].key1 = atoi(tmp.c_str());
getline(dataFile, person[counter].name1,':');
getline(dataFile, person[counter].address1);
PNTree.insert(person[counter].key1, person[counter]);
}
dataFile.close();
The prototype for the insert
call is " template <class KeyType, class DataType> bool BST<KeyType, DataType>::insert (KeyType key, DataType data)
". Also, I understand that atoi is C and not C++, but I also tried stringstream and that failed as well! Please help!
After attempting to reverse-engineer your problem , I think that PNTree.insert
is wrong. Assuming that PNTree
is a std::map<int, person_type>
, then the insert
method of that does not take two parameters of those types. The three insert members are below.
pair<iterator,bool> insert (const value_type& val);
iterator insert (iterator position, const value_type& val);
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
and value_type
is a std::pair<int, person_type>
. I presume you wanted the first one, to insert a node , in which case, the easiest thing would be:
PNTree[person[counter].key1] = person[counter];
Also note that several of those "numbers" cannot be converted to an int
, they're simply to big. You'll have to either use long long
, or std::string
to hold those.
If I were to write this code, it would look more like this .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.