结构体排序求助!!!
在结构体进行排序前可以正常输出,排序之后点运行,输入数据之后会直接退出,有没有大佬可以帮我看看哪里出了问题,怎么解决?#include<iostream>
#include<algorithm>
#include<string>
using std::string;
using namespace std;
struct Student{
string id;
int M;
int m;
int score=0;
}s;
bool cmp(Student a,Student b){
if(a.score == b.score)
return a.id < b.id;
return a.score > b.score;
}
int main() {
int N, M, G;
int m;
while (scanf("%d %d %d", &N, &M, &G) != EOF) {
int num = 0;
if(N == 0)
break;
for (int i = 0; i < M; i++)
scanf("%d", &m);
for (int i = 0; i < N; i++){
scanf("%s %d",&s.id,&s.M);
for(int j = 0; j < s.M; j++)
scanf("%d",&s.m);
}
for (int i = 0; i < N; i++){
for(int j = 0; j < s.M; j++){
s.score += m.m - 1];
}
if(s.score >= G){
num += 1;
}
}
printf("%d\n",num);
sort(s, s+N, cmp);
for(int i=0;i<num;i++){
printf("%s %d\n",&s.id,s.score);
}
}
return 0;
}
看看这些有没有帮助
test.cpp:32:39: warning: format specifies type 'char *' but the argument has type 'std::string *' (aka 'basic_string<char> *') [-Wformat]
scanf("%s %d",&s.id,&s.M);
~~ ^~~~~~~~
test.cpp:47:42: warning: format specifies type 'char *' but the argument has type 'std::string *' (aka 'basic_string<char> *') [-Wformat]
printf("%s %d\n",&s.id,s.score);
~~ ^~~~~~~~
2 warnings generated. dolly_yos2 发表于 2022-11-14 16:27
看看这些有没有帮助
将 结构体中 id 的类型改为 char 解决了 谢谢!
页:
[1]