0

I am working on a movie database that lets users input movies with Title, Director, and Release Year. The database then saves all 3 of the inputs as 2 strings(Title and Director) and 1 int value(RY) for release year and stores all of them together into 1 text file. However, I want to sort the movies from newest year to oldest year but when we want to read in the integer from a text file into an array the sort method doesn't do anything besides print out the movies in the same order the user inputs them. Is there any way of putting integers from the text file into the array together? Below is the sort method. The amount of movies we want in the database is 7.

void sort()
{
    string Title;
    string fileName;
    string Director;
    int RY;

    int array [7] = { };
    int temp;
    int p;

    fstream file("directory.txt");

    for(int i = 0; i < 7; ++i)
    {
        file >> array[i];
    }

    for (int i = 1; i < sizeof(array); i++) {
    temp = array[i]; //temp = 3
    p = i -1; // p = 1
    while( p >=0 && array[p] > temp){
        array [p + 1] = array [p];
        p = p-1; //p = 0
    }
    array [p + 1] = temp;
}


        for (int i = 7 - 1; i >= 0; i--)
            cout << array[i];

        cout << "Entire Movie database" << endl;
        cout << "------------------------" << endl;
        ifstream dir("directory.txt");
        while (dir >> Title >> Director >> RY){
            cout << Title << ' ' << Director << ' ' << RY << endl;
        }

        main();
    }
2
  • 2
    I think you would be better loading the movie data into a vector of structs and then sorting the vector of structs with help from this example Commented Apr 23, 2020 at 0:25
  • for (int i = 1; i < sizeof(array); i++) -- sizeof does not do what you think it does. Print out the value of sizeof(array), and get ready for a surprise. Commented Apr 23, 2020 at 0:56

1 Answer 1

0

Your custom sorting procedure (probably) does whatever it should do: it sorts integers in your array. The following code just reads the contents of your file and outputs in the order it reads:

    ifstream dir("directory.txt");
    while (dir >> Title >> Director >> RY){
        cout << Title << ' ' << Director << ' ' << RY << endl;
    }

I wonder how you expect to read 7 integers from the file for the first time and read all the four fields for the second. Maybe you think that the string values would be skipped? They wouldn't

There are other problems in your code. For example, sizeof(array) doesn't return the number of elements but the size in bytes. One more thing: why are you calling main from your sort function?

Why are you reinventing the wheel? Read the values into a struct, store them in a vector, then use the std::sort procedure.

Sign up to request clarification or add additional context in comments.

2 Comments

I feel like these should be comments instead of an answer
@NathanWride, I guess that the main question was why doesn't this procedure print sorted data, and my answer really explains that. However I wouldn't protest if the whole question would be closed as the one that doesn't add any value to site and of a very low quality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.