-1
#ifndef MOVIETREE_H_INCLUDED
#define MOVIETREE_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <vector>
#include <math.h>
using namespace std;

class BinarySearchTree
{
    private:
        struct tree_node
        {
            tree_node* left;
            tree_node* right;
            int ranking;
            string title;
            int year;
            int quantity;
        };
        tree_node* root;

    public:
        BinarySearchTree()
        {
            root = NULL;
        }

        bool isEmpty() const { return root==NULL; }
        void insert(int ranking, string title, int year, int quantity);
        void remove(string title);
        void inorder(tree_node* p);
        double orderrating(string title);
        void print_inorder();
        void search(string d);
        void rent(string l);
        // void  split(const string& s, char c,vector<string>& v);
};

double BinarySearchTree::orderrating(string title)
{
    // string letters[52]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","o","p","q","r","s","t","u","v","w","x","y","z"};
    char letters[54]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
        'P','Q','R','S','T','U','V','W','X','Y','Z',':','a','b','c','d','e','f','g','h','i','j',
        'k','l','m','o','p','q','r','s','t','u','v','w','x','y','z',' '};
    double rating = 0;
    for(int i = 0; i<title.length();i++)
    {
        for(int j = 0;j<52;j++)
        {
            if(letters[j]==title.at(i))
            {
                rating = rating+pow(10,-i)*((j)%26);
            }
        }
    }
    //cout<<rating<<endl;
    return rating;
}

void split(const string& s, char c, vector<string>& v)
{
    string::size_type i = 0;
    string::size_type j = s.find(c);

    while (j != string::npos) {
        v.push_back(s.substr(i, j-i));
        i = ++j;
        j = s.find(c, j);

        if (j == string::npos)
            v.push_back(s.substr(i, s.length()));
    }
}

void BinarySearchTree::insert(int ranking, string title, int year, int quantity)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->quantity = quantity;
    t->ranking = ranking;
    t->title = title;
    t->year = year;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;
    // is this a new tree?
    if(isEmpty()) root = t;
    else
    {
        //Note: ALL insertions are as leaf nodes
        tree_node* curr;
        curr = root;
        // Find the Node's parent
        while(curr)
        {
            parent = curr;
            if(orderrating(t->title) > orderrating(curr->title)) curr = curr->right;
            else curr = curr->left;
        }

        if(orderrating(t->title) <= orderrating(parent->title))
            parent->left = t;
        else
            parent->right = t;
    }
}

void BinarySearchTree::search(string l)
{
    //Locate the element
    bool found = false;
    double d = orderrating(l);
    if(isEmpty())
    {
        cout<<" This Tree is empty! "<<endl;
        return;
    }
    tree_node* curr;
    tree_node* parent;
    curr = root;
    while(curr != NULL)
    {
        if(curr->title == l)
        {
            found = true;
            cout << "Movie Info:" << endl;
            cout << "===========" << endl;
            cout << "Ranking:" <<curr->ranking<<endl;
            cout << "Title:"<<curr->title<<endl;
            cout << "Year:" <<curr->year<<endl;
            cout << "Quantity:"<<curr->quantity<<endl;
            break;
        }
        else
        {
            parent = curr;
            if(d>orderrating(curr->title)) curr = curr->right;
            else curr = curr->left;
        }
    }
    if(!found)
    {
        cout<<" Movie not found."<<endl;
        return;
    }
}

void BinarySearchTree::rent(string l)
{
    //Locate the element
    bool found = false;
    double d = orderrating(l);
    if(isEmpty())
    {
        cout<<"This Tree is empty!"<<endl;
        return;
    }
    tree_node* curr;
    tree_node* parent;
    curr = root;
    while(curr != NULL)
    {
        if(curr->title == l)
        {
            found = true;
            if(curr->quantity!=0)
            {curr->quantity = curr->quantity-1;
                cout << "Movie has been rented." << endl;
                cout << "Movie Info:" << endl;
                cout << "===========" << endl;
                cout << "Ranking:" <<curr->ranking<<endl;
                cout << "Title:" <<curr->title<<endl;
                cout << "Year:" <<curr->year<<endl;
                cout << "Quantity:" << curr->quantity<<endl;
            }
            else{//If movie is in stock
                cout << "Movie out of stock." << endl;
            }
            break;
        }
        else
        {
            parent = curr;
            if(d>orderrating(curr->title)) curr = curr->right;
            else curr = curr->left;
        }
    }
    if(!found)
    {
        cout<<"Movie not found."<<endl;
        return;
    }
}

void BinarySearchTree::print_inorder()
{
    inorder(root);
}

int counter =0;

void BinarySearchTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout<<"Movie: "<<p->title<<endl;
        //cout<<" "<<p->quantity<<endl;//cout<<counter<<endl;
        if(p->right) inorder(p->right);
    }
    else return;
}
#endif // MOVIETREE_H_INCLUDED

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include "MovieTree.h"

using namespace std;

struct MovieInfo{
    int ranking;
    string title;
    int year;
    int quantity;
};

int main(int argc, char * argv[])
{
    //creates struct to store movie info
    MovieInfo MovieInfo1[51];
    int counter1 = 0;
    std::string word1;
    //"Assignment5Movies.txt"
    ifstream myfile1("Assignment5Movies.txt");
    if (myfile1.is_open())
    {
        std::string line;
        while ( getline (myfile1,line) )
        {
            //need delimeter for file.
            vector<string> v;
            string s = line;
            split(s, ',', v);
            for (unsigned int i = 0; i < v.size(); ++i)
                //add individual atributes to moviearray
            {//cout<<v[i]<<endl;
                if(i%4==1){MovieInfo1[counter1].title = v[i];}
                if(i%4==2){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].year = value;}
                if(i%4==3){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].quantity = value;}
                if(i%4==0){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].ranking = value;}
            }//cout<<counter1<<endl;
            counter1++;
        }
        myfile1.close();
    }
    // construct Binary tree
    BinarySearchTree b;
    for(int i = 0; i<counter1;i++)
    {
        b.insert(MovieInfo1[i].ranking,MovieInfo1[i].title,MovieInfo1[i].year, MovieInfo1[i].quantity);
    }
    int option;
    //do while loop for user interface
    do{
        cout << "======Main Menu=====" << endl;
        cout << "1. Find a movie" << endl;
        cout << "2. Rent a movie" << endl;
        cout << "3. Print the inventory" << endl;
        cout << "4. Quit" << endl;
        cin>>option;
        cin.ignore(10000,'\n');
        if (option==1)//find movie
        {
            cout << "Enter title:" << endl;
            string temp;
            cin >> ws;
            getline(cin, temp);
            b.search(temp);
        }
        if (option==2)//rent movie
        {
            cout << "Enter title:" << endl;
            string temp;
            cin >> ws;
            getline(cin, temp);
            b.rent(temp);
        }
        if (option==3)//print inventory
        {
            b.print_inorder();
        }
    }while(option!=4);
    cout << "Goodbye!" << endl;
}

here is my main file and my header, basically my output is off for like two movies and I don't know why. any input would be appreciated. and yes I should have used a string.compare(string) also when given a txt file it prints

12 Angry Men  
Back to the Future  
Casablanaca  
... the rest are in order except  
Lord of the Rings: the two towers   
Lord of the Rings: the fellowship of the ring  
Lord of the Rings: the return of the king  

are in the right place, but ordered incorrectly.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • This is a great case for a debugger. What were the issues you discovered when using the debugger? – Thomas Matthews Feb 27 '15 at 21:39
  • 1
    When over half of the page space is unformatted output and the rest is a gigantic code block, it's a very good indication that the post is not a good fit for SO. We thrive on *minimal* examples, not a dump of the entire code being used. – chris Feb 27 '15 at 21:40
  • I highly recommend refactoring. The `BinarySearchTree` should only contain code related to creating and searching a tree. Stuff like menus should be placed in another translation unit, such as `main`. – Thomas Matthews Feb 27 '15 at 21:44
  • In your inorder, you are checking `if (p->left) inorder(p->left)`, same thing with `p->right`, however, this check is unnecessary since you are already checking `if (p != NULL)` at the beginning of your inorder function. –  Feb 27 '15 at 21:45
  • Creating an [MCVE](http://stackoverflow.com/help/mcve) would help you, and would also help us to help you. – Emil Laine Feb 27 '15 at 21:47

1 Answers1

0

The value of orderrating() for these three movies is identical. Therefore, they will be printed in the opposite order that you insert them into the tree.

  • Lord of the Rings: the two towers
  • Lord of the Rings: the fellowship of the ring
  • Lord of the Rings: the return of the king

There's only so much precision in a double.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173