/* Write a C++ program to do the following :
Inputs a line of text.
Tokenizes the line into separate words.
Inserts the words into a binary search tree(BST).
Do a postorder traversal of the tree and print it.
Do a preorder traversal of the tree and print it.
Do an inorder traversal of the tree and print it.
Print the heights and the number of leafs in each of the three binary search trees.*/
#include "Tree.h"
int main()
{
BTreeType<string> wordTree;
char *line;
char *words;
ifstream inData;
int n = 0;
inData.open("Text.txt");
getline(inData, line.c_str());
words = strtok(line, " .");
while (words != NULL)
{
wordTree.insert(words);
words = strtok(line, " ,.");
n++;
}
inData.close();
cout << "Preorder Transversal:" << endl;
wordTree.preOrderTraversal();
cout << endl;
cout << "Postorder Transversal:" << endl;
wordTree.postOrderTraversal();
cout << endl;
cout << "Inorder Transversal:" << endl;
wordTree.inOrderTraversal();
cout << endl;
system("pause");
return 0;
}
.c_str() is a member function for std::string. You are trying to call it on line which is a char*.
std::string.c_str() returns a constchar*.
There are 2 versions of getline that apply here. The first is the non-member function istream& getline (istream& is, string& str); (http://www.cplusplus.com/reference/string/string/getline/). In this case, you need to pass a reference to a std::string.
The second version is the member function istream& istream::getline (char* s, streamsize n ); (http://www.cplusplus.com/reference/istream/istream/getline/). In this case you need to pass both a char* and a length. The char* must point to memory that can be written to.
You seem to be conflating these 2 methods.
I would suggest you replace char *line; with std::string line; and replace getline(inData, line.c_str()); with getline(inData, line);.
Thanks for the input. Unfortunately, that is how my instructor told me to do it, thus why I did it that way.
When I change those line to string line, I get the error message 'char *strtok(char *,const char *)': cannot convert argument 1 from 'std::string' to 'char *'. Any suggestions?
don't mix and match the C and C++ as a beginner. Stick to char* or string, but not a mix.
if your school is using C and char*, just use that, and strtok will be ok. Use the getline for char*, as said above, looks like
char charptr[101];
cin.getline(charptr, 100); //whatever max length you want
arrays of char are cleaner. if you use a pure pointer, you need to manage the memory, which you didn't do above.
char *annoying;
annoying = new char[100]; //get memory from OS
… use it
delete[] annoying; //give it back to OS
the name of an array of char is a char* to the compiler:
char x[10];
x, without any [] notation, just x, is treated like a char* for the most part when passed to the C string functions.
I suspect the instructor wanted the return value of std::string::c_str() to be passed as the first parameter to strtok(), which is pretty idiotic, because strtok() modifies that string, so it would result in undefined behavior.