#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include <string>
using namespace std;
#define size 50
int main() {
struct student {
int nume_student;
char name_student[20];
char sex_student;
int phone_number;
float score_yuwen;
float score_math;
float score_english;
float score_pinde;
float total_score;
int rank;
char attach[50];
}stu[size];
struct num {
char iuy[100];
char tyu[100];
}nty[size];
char uio[100];
int a;
char b;
char c;
int d;
float e;
float f;
float g;
float h;
float j;
int k;
char* l;
int i;
int lo;
int op;
printf("请问您要输入多少个信息?");
scanf("%d", &lo);
printf("确认输入%d个信息\n开始输入您的信息", lo);
for (i = 0;i<lo;i++)
{
scanf("%d %s %c %d %f %f %f %f %s",&a, stu[i].name_student, &c,&d, &e, &f, &g, &h,stu[i].attach);
stu[i].sex_student = c;
stu[i].score_yuwen = e;
stu[i].score_math = f;
stu[i].score_english = g;
stu[i].score_pinde = h;
stu[i].total_score = e + f + g + h;
itoa(a, nty[i].iuy, 10); _ //这句是我为了数字转化字符串后加的_
itoa(d, nty[i].tyu, 10); ** //这句是我为了数字转化字符串后加的**
}
printf("请问您要查询哪个学号?\n"); _//在这之前没什么大问题_
scanf("%d", &op);
sprintf(uio, "%ld",op)
printf("%s", uio); **_//这一步输出uio的字符串,永远都不是我12位的学号永远都是8位数左右的随机数字_**
int r = strcmp(uio, nty[i].iuy);
i = 0;
while (r!=0)
{
i++;
r = strcmp(uio, nty[i].iuy);
}
printf("学号为%s 姓名为%s 性别为%c 手机号为%s 语文成绩为%f 数学成绩为%f 英语成绩为%f 品德分为%f 综合评测分为%f 排名为 备注为%s", nty[i].iuy,stu[i].name_student,stu[i].sex_student, nty[i].tyu, stu[i].score_yuwen, stu[i].score_math, stu[i].score_english, stu[i].score_pinde, stu[i].total_score, stu[i].attach);
我的目的只有一个,最后我输入我的学号通过while判断找到结构体数组里面我自己信息的那一组然后自己的学号和手机号完美输出不是随机数字也不是乱码,其他的名称成绩统统已经没问题了就是学号和手机号永远不能完好输出。然后我尝试数字改字符串,然后通过strcmp字符串while比较来找到自己的那一组信息,但没等断点测试到while那一行前面数字就不对劲了,5个小时,真顶不住了,希望有可以告诉我怎么在student那块设立结构体数组的时候在学号和手机号也就是nume_student;和phone_number;前面依旧定义int而不是char我知道char最容易但我们课题就是int说最好别改,所以在int前提下成功最后输出我的学号和手机号,悬赏问题,感谢你们了。
你学号和手机号是要int类型还是字符串char []类型
输入,判断和输出都应该统一,不要一会用int类型一会用字符串char []类型
int的范围是:-2147483648 to 2147483647
你手机号有11位数超出int能承载的最大范围
需要把学号与手机号改成long long int 类型
struct student {
long long int nume_student;
long long int phone_number;
与之相关的a,d,op也改成long long int 类型
long long int a;
long long int d;
long long int op;
学号与手机号输入和输出用%lld,不需要转成字符串了, 也就不需要nty了
你题目的解答代码如下:
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
using namespace std;
#define size 50
int main() {
struct student {
long long int nume_student;
char name_student[20];
char sex_student;
long long int phone_number;
float score_yuwen;
float score_math;
float score_english;
float score_pinde;
float total_score;
int rank;
char attach[50];
}stu[size];
char uio[100];
long long int a;
char b;
char c;
long long int d;
float e;
float f;
float g;
float h;
float j;
int k;
char* l;
int i;
int lo;
long long int op;
printf("请问您要输入多少个信息?");
scanf("%d", &lo);
printf("确认输入%d个信息\n开始输入您的信息:\n", lo);
for (i = 0;i<lo;i++)
{
scanf("%lld %s %c %lld %f %f %f %f %s",&a, stu[i].name_student, &c,&d, &e, &f, &g, &h,stu[i].attach);
stu[i].nume_student = a;
stu[i].sex_student = c;
stu[i].phone_number = d;
stu[i].score_yuwen = e;
stu[i].score_math = f;
stu[i].score_english = g;
stu[i].score_pinde = h;
stu[i].total_score = e + f + g + h;
}
printf("请问您要查询哪个学号?\n"); //在这之前没什么大问题_
scanf("%lld", &op);
for (i = 0; i < lo; i++)
if (op==stu[i].nume_student)
break;
if (i<lo)
printf("学号为%lld 姓名为%s 性别为%c 手机号为%lld 语文成绩为%f 数学成绩为%f 英语成绩为%f 品德分为%f 综合评测分为%f 排名为 备注为%s", stu[i].nume_student ,stu[i].name_student,stu[i].sex_student, stu[i].phone_number , stu[i].score_yuwen, stu[i].score_math, stu[i].score_english, stu[i].score_pinde, stu[i].total_score, stu[i].attach);
else
printf("没有找到学号%d",op);
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
问题很多,学号,手机号都要用字符串存储,还有需要初始化为空,越界和未初始化都可能正常乱码情况