Sorting problems

The following information is in my header file
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef MYCLASSES_H_INCLUDED
#define MYCLASSES_H_INCLUDED


#include<iostream>
#include<string.h>
#include<fstream>

using namespace std;//we need this for class

class Quote
{
public:
    Quote();
    string getFirst()
    {
        return(first);
    }
    string getLast()
    {
        return(last);
    }
    string getQ()
    {
        return(q);
    }
    void set(string l,string f,string qu)
    {
        last=l;
        first=f;
        q=qu;
    }
    void operator=(Quote);
    int operator==(Quote);
    int operator>(Quote);
private:
    string last;
    string first;
    string q;
};

Quote::Quote()
{
    first="First";
    last="Last";
    q="Quote";
}
template <class type>
void sort (type song[], const int N)
{
    for(int e=0; e<N; e++)
    {
        for(int i=0; i<N-1; i++)
        {
            if (song[i]>song[i+1])
            {
                type temp;
                temp=song[i];
                song[i]=song[i+1];
                song[i+1]=temp; //I think something is wrong with this loop
            }
        }
    }
}
void Quote::operator=(Quote song)
{
    first=song.first;
    last=song.last;
    q=song.q;
}
int Quote::operator==(Quote song)
{
    first=song.first;
    last=song.last;
    q=song.q;
}

int Quote::operator>(Quote q2)
{
    if(last>q2.getLast())
    {
        return 1;
    }
    else if(last==q2.getLast())
    {
        if(first>q2.getFirst())
        {
            return 1;
        }
        else if(first==q2.getFirst())
        {
            if(q>q2.getQ())
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}
#endif // MYCLASSES_H_INCLUDED 

the following is in my c++
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
#include"myclasses.h"
const int N=5;
using namespace std;

int main()
{
    Quote song[N];
    string l;
    string f;
    string qu;
    //ifstream quotein("S://Public/Potter/Quotes.txt");
    ifstream quotein("F://Comp Sci/QuoteCompIn.txt");
    if(quotein.is_open())
    {
        while(quotein.good())
        {
            for(int i=0; i<N; i++)
            {
                getline(quotein,l,',');
                getline(quotein,f,',');
                getline(quotein,qu,'\n');
                song[i].set(l,f,qu);
                cout<<(song[i].getLast())<<','<<" ";
                cout<<(song[i].getFirst())<<','<<" ";
                cout<<(song[i].getQ())<<endl;
            }
            quotein.close();
        }
    }
    else
    {
        cout<<"No input."<<endl;
    }
    cout<<endl;
    sort(song,N);
    ofstream quoteout("F://Comp Sci/QuoteCompOut.txt");
    if(quoteout.is_open())
    {
        while(quoteout.good())
        {
            for(int i=0; i<N; i++)
            {
                quoteout<<(song[i].getLast())<<","<<" ";
                quoteout<<(song[i].getFirst())<<','<<" ";
                quoteout<<(song[i].getQ())<<endl;
                cout<<(song[i].getLast())<<","<<" ";
                cout<<(song[i].getFirst())<<','<<" ";
                cout<<(song[i].getQ())<<endl;
            }
            quoteout.close();
        }
    }
    else
    {
        cout<<"No ouput."<<endl;
    }

}


I am trying to get quotes and then organize them by their last names. If that is equal than by their first names and then by the quote. My problem is that when I output it outputs the correct answer twice. Another problem is that if I have 10 lines of "last, first, "quote"" and I say my N=9 it wont work. Please help. Thanks in a advance.
Last edited on
closed account (o3hC5Di1)
Hi there,

Could you please be a little more specific about what happens when you say it doesn't work? Are you getting runtime errors, if so which ones? What output do you expect and what output do you get instead?

That should allow us to help you better.

All the best,
NwN
When N=5

My input is:
tim, jones, "I love you"
joe, ephraim, "Baby come back!"
mary, motion, "OH no she did it again"
sam, sled, "No"
sam, bubble, "Help"

The problem is that if I have 6 lines in my file input and I N=5 this happens

My output is:
, bubble, "Help"
, bubble, "Help"
, bubble, "Help"
, bubble, "Help"
, bubble, "Help"

But if N= to the exact amount of lines in my file it works fine.

What I am trying to get is:
"All Day All Long", orange, apple
apple, orange, "All Day All Long"
joe, ephraim, "Baby come back!"
mary, motion, "OH no she did it again"
sam, bubble, "Help"

I guess you should try changing

while(quotein.good())

to

if (quotein.good())

or just skip it, you are already checking whether it is open.

The area I found a problem is in the header file...b.c. The data recieved is correct
Topic archived. No new replies allowed.