#include <iostream>
#include<algorithm>
#include<string>
using namespace std;
struct student {
string name;
int number;
double score;
};
bool compare(const student& a, const student& b) {
return a.score > b.score;
}
int main()
{
// int x = 10;
struct student stu[x];
int i = 0;
/** for(i = 0; i<x; i++) {
cout<<"Please enter the name.: ";
getline(cin, stu[i].name,'\n');
cout<<"Please enter the student number.: ";
cin>>stu[i].number;
cout<<"Please enter the score.: ";
cin>>stu[i].score;
cin.ignore();
} **/
do {
cout<<"Please enter the name.: ";
getline(cin, stu[i].name,'\n');
cout<<"Please enter the student number.: ";
cin>>stu[i].number;
cout<<"Please enter the score.: ";
cin>>stu[i].score;
i++;
cin.ignore();
} while (stu[i].name == "quit");
sort(stu, stu+x, compare);
cout<<"Rank"<<'\t'<<"Name"<<'\t'<<"Student Number"<<'\t'<<"Score"<<endl;
for(int j = 0; j<x; j++) {
cout<<j+1<<'\t'<<stu[j].name<<'\t'<<stu[j].number<<'\t\t'<<stu[j].score<<endl;
}
}
To use the for loop, I need to know the number of the students, but in case I don't know the number, I want to use do-while loop and it has to run until I type "quit". But now it occurs an error. I guess stu[i].name == "quit" is the problem, how can I fix it?