Sorting algorithm

Im doing this program where you enter a movie collection and the it sorts it by the length of the movies. i get and error by at line 41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <stdlib.h>

using namespace std;


struct collection {
    string namn;
    string skapare;
    int length;
    };        

void filmsortering(collection info[], int filmer);

int main()
{
    int filmer;

    cout << "Number of movies in your collection: ";
    cin >> filmer;
    cin.ignore(1000, '\n');
    

    collection filmsamling[filmer];
    

    for (int i = 0; i < filmer; i++) {
        cout << "Title of your movie: ";
        getline(cin, filmsamling[i].namn);
        cout << "Director of that movie: ";
        getline(cin, filmsamling[i].skapare);
        cout << "The length of that movie: ";
        cin >> filmsamling[i].length;
        cin.ignore(1000, '\n');
        }
        
  for (int m = filmer-1; m > 0; m--)
  {
         for (int n = 0; n < m; n++)
         {
             if (info[n].length > info[n+1].length)
             {
                collection temp = info[n];
                info[n] = info[n+1];
                info[n+1] = temp;
                }
             }
                 
    
    cout << "This is your movies of choise: " << endl;
    for (int q = 0; q < filmer; q++)
    {
        cout << left << "\n" << "Film #" << q << "\n" << "Title: " << filmsamling[q].namn << "\n" << "Director: " << filmsamling[q].skapare << "\n" << "Length: " << filmsamling[q].length << endl;
    }
    
    system("PAUSE");
    return 0;

     }
}




Please help me fix this error as soon as possible.. Thanks!
Last edited on
info does not appear to be declared anywhere.
You must be using a non-std compiler if this code compiles because you can't write arrays like you have with a std c++ compiler.

This can be done by defining the operator< for your struct and using the std::sort algorithm. std::sort can't be any less efficient than what you wrote. In fact it is probably a lot faster and less error prone. The following article contains an example on how to do this. Although it is more complex, the concept works with simple structs as well.
http://cplusplus.com/forum/articles/10879/
Topic archived. No new replies allowed.