I need to insert data in a search binary tree reading a text file. It works when I use spaces between the data, but it doesn’t work if I use commas like I want to.
For example I would want my text file to look like this:
New-York,c3,Luke
London,c5,Nathan
Toronto,c1,Jacob
...
My text file right now look like something like this:
New-York c3 Luke
London c5 Nathan
Toronto c1 Jacob
...
I’d also want my program not to think a space means it needs to look at the next data, only with commas.
This is how my code looks like:
void fillTree( BinarySearchTree *b)
{
ifstream file;
file.open("data.txt");
string city;
string tag;
string name;
Person p;
if(!file) {
cout<<"Error. " << endl;
}
while(file >> city >> tag >> name)
{
p.setCity(city);
p.setTag(tag);
p.setName(name);
cout << p.getCity() << " " << p.getTag() << " " << p.getName() << endl;
(*b).insert(p);
}
file.close();
}
What would I need to change in my code so I could use commmas instead of spaces? I feel like it would look more neat in the text file with commas instead of spaces.
I’m pretty sure I’d need to edit something in this block of code, but if it could be somewhere else please let me know.
use
getline ( file, value, ',' );to read string until coma occurs.