c语言字符串程序挑错

来源:百度知道 编辑:UC知道 时间:2024/05/22 07:30:49
编写4个函数实现下列功能:
(1)函数input() 输入20个学生姓名和高考总分;
(2)函数sort() 按高考总分从高到低的顺序排序,姓名顺序也随之调整;
(3)函数display()显示学生姓名及其高考总分;
(4)在主函数中输入一个姓名,编写函数search() 用顺序查找方法找出该学生。若查找成功,在主函数中显示该生姓名及其高考总分;否则显示查找失败。
#include<stdio.h>
#include<string.h>
#define N 20
void main()
{
void input(int[],char name[][8]);
void sort(int[],char name[][8]);
void display(int[],char name[][8]);
void search(int,int[],char name[][8]);
int sco[N],score,name,flag=1,c;
char name[N][8];
input(score,name);
sort(score,name);
while(flag==1)
{
printf("\ninput name to look for:");
scanf("%d",&name);
printf("continue or not(Y/N)?");
getchar();
c=getchar();
if(c=='N'||c=='n')
{
flag=0;
}
}
}
void input(int[],char name[][8])
{
int i;
for(i=0;i<N;i++)
{
printf("\

有两个比较明显的错误吧:
1、字符串的比较不能用“==”(像一楼说的,略过)
2、多次使用temp[N],也就是temp[20],这里其实只是一个字符,而且还是一个不存在的位置,因为最大只有temp[19],非法的地址,危险哦!应该像下面这样写:
printf("can not find %10s\n",temp);
其他的再看看咯,这两个比较明显!

if(temp[N]==name[i])
字符串比较不能这样比,用#include<string.h> 调用strcmp(字符串1,字符串2)
改过来再试试吧

if(temp[N]==name[i])

是字符比较,不是这样写的吧 是 strcpy