Hi,how would i need to change this code to accept strings insted integers in avl tree?

template<class T>
void InsertItem(TreeType<T>& tree)
 {
    cout << "Enter number: ";
    int num;
    cin >> num;
    tree.InsertItem(num);
    cout << "Number inserted:" << num;
    }

Recommended Answers

All 2 Replies

The code ? maybe ? something like this:

template< class T >
void tqkeInAndInsertItem( TreeClass< T >& tree )
{
    cout << "Enter item: ";
    T tmp;
    cin >> tmp; // MUST HAVE defined >> for type T //
    tree.insert( tmp );
    cout << "Item inserted:" << tmp; // MUST HAVE defined << for type T //
}

how would i need to change this code to accept strings insted integers in avl tree?

Agreed with David W. Though the function seems entirely unnecessary. It's generally better practice to avoid having functions that do multiple things (ie. gather input and insert into a data structure) in favor of just one (ie. only gather input and return it or only insert into a data structure with provided values). Since your class already has an insert method, this function doesn't really do anything to justify a separate function.

It's also inflexible in that it only takes input from cin and also unconditionally writes to cout, so you can't automate this process such as accepting input from a file silently.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.