Sort by grade AND by name

The problem is the following: While reading input from the keyboard, you will read a string and a number. The string is the name of the student, while the number (int) is his grade. I think I almost got it, but I have a problem when running the code. I've isolated the problem to where the string sort happens, but I can't seem to locate it exactly though. Can somebody help me?(I'd prefer if you'd point out some mistakes, or suggest another way, but I don't need code)
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
  #include<iostream>
#include<utility>
#include<vector>
#include<string>
using namespace std;
int smaller(string a, string b){  //determine which string has smaller size to set limits
int as = a.size();
int bs = b.size();
if (as>bs){
    return bs;
}
else if(as<bs){
    return as;
}
else{
    return as;
}
}
string alphabeta(string a, string b){ 
 /*here it checks which is alphabetically lower determined by the first 
character who isn't equal between the strings*/
int place = 0;
while (a[place]==b[place] && place<smaller(a, b)){  
    place++;
}
if (a[place]<b[place]){
    return a;
}
else if(a[place]==b[place]){
    return "equal";
}
else if(a[place]>b[place]){
    return b;
}
}
typedef pair<string, int> si;  
int main(){
vector < si > grades;
string id;
int grade;
while (cin>>id>>grade){         //read input
    si input;
    input = make_pair(id, grade);
    grades.push_back(input);
}
bool swapped = true;
while(swapped){       //sort using bubble sort, I think the integer part works
        swapped = false;
for (int i=0; i<grades.size(); i++){
    if (grades[i].second>grades[i+1].second){
        swap(grades[i], grades[i+1]);
         swapped = true;
    }

    else if(grades[i].second==grades[i+1].second){
        if(alphabeta(grades[i].first, grades[i+1].first)==grades[i+1].first){
            swap(grades[i], grades[i+1]);
            swapped = true;
        }
    }
}
}
for (int i=0; i<grades.size(); i++){
    cout<<grades[i].first<<" "<<grades[i].second<<"\n";
}
}  //output the final sorted vector 
I think I almost got it, but I have a problem when running the code. I've isolated the problem to where the string sort happens, but I can't seem to locate it exactly though.

And what exactly is the problem?
The program is terminated before it outputs anything, by not responding [http://i.imgur.com/I619Tkj.png]. I've been able to isolate it by commenting the lines from 55 to 62. I don't know why the program does this.
Probably because you are trying to access your vector out of bounds. Look at this snippet:
1
2
for (int i=0; i<grades.size(); i++){
    if (grades[i].second>grades[i+1].second){

When the loop is on it's final iteration when you add 1 to the index you are accessing the vector out of bounds.

@jib
.. I was looking for a more complicated error but the answer was so simple. Now I feel stupid I didn't see it :P. Thanks, anyways.
Topic archived. No new replies allowed.