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)
#include<iostream>
#include<utility>
#include<vector>
#include<string>
usingnamespace 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;
}
elseif(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;
}
elseif(a[place]==b[place]){
return"equal";
}
elseif(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;
}
elseif(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.
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.