using namespace std;
The class Student {
Public:
int num;
String name;
Char sex;
int age;
};
Bool CMP (Student a, Student b) {
Return a.n um> B.n um.
}
Int main () {
Student s [4];
String ss;
char c;
S [0]. Num=10, s [0]. Name="wes," s [0]. Sex='f' s [0]. Age=23;
S [1]. The num=20, s [1]. The name="ert, s [1]. Sex='f' s [1]. The age=45;
S [2]. Num=30 s [2]. Name="STR" s [2]. Sex='t', s [2]. The age=89;
Cout<& lt;" Please enter the command: "& lt;
C=ss [0];
If (c=='I') {
String name;
Char sex;
Int num, age;
Sscanf (ss, "[^,] I % d, %, % [^,], % d", & amp; Num, name, sex, & amp; The age);
S [3]. Num=num;
S [3]. Name=name;
S [3]. Sex=sex;
S [3]. The age=age;
Sort (s, s + 4, CMP);
for(int j=0; j<4. J++)
Cout<& lt;" (" & lt;
If (c=='D') {
int m;
Sscanf (ss, "D % D", & amp; m);
for(int i=0; i<4. I++) {
If (s [I] num==m) {
S [I]. Age=s [3]. The age;
S [I] name=s [3]. The name;
S [I]. [3] sex=s. Sex;
S [I] num=s [3]. Num.
}
}
Sort (s, s + 3, CMP);
for(int j=0; j<3; J++)
Cout<& lt;" (" & lt;
return 0;
}
Are two sscanf errors, the reason for the error is the error C2664: 'sscanf: always convert parameter 1 from' class STD: : basic_string & lt; Char, struct STD: : char_traits & lt; char> Class, STD: : allocator
No user - defined - conversion operator available that can perform this conversion, or the operator always be called
CodePudding user response:
Sscanf (ss, "[^,] I % d, %, % [^,], % d", & amp; Num, name. C_str (); , sex, & amp; The age);
CodePudding user response:
Sscanf (ss, "[^,] I % d, %, % [^,], % d", & amp; Num, name, sex, & amp; The age);The name is a string, to get the address, need to use the name. The c_str ()
CodePudding user response:
With the name directly. The c_str () more troublesome, still had better use of intermediate variable, then assigned to the nameDirectly change the c_str () return address, and the size of the string matching on
Char \ [20].
Sscanf preach and temp inside
And then the name=temp; It is safe
CodePudding user response:
Two methods of eldest brother tried or useless,, and I the following sscanf (ss, "D % D", & amp; m); The same mistake