【发布时间】:2020-04-04 18:14:15
【问题描述】:
我的功能有问题。我有一个包含学生信息的结构,如姓名、身份证号、标记等。我能够阅读和打印所有需要的信息,但问题是当我想计算输入的同名时。例如,如果我输入以计算名称 Iva,并且它出现 3 次,则计算 3。它返回 0 代表我到目前为止使用的内容。代码如下:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char name [31];
char ID[11];//personal code
long FN;//student number
};
int BrS;
student MasStud[35]; //array for number of students
char bf[5];
void readStud(student *st){
int i;
printf("-------------------\n");
printf("Enter name of a student:\n"); gets_s(st->name);
printf("Enter ID");gets_s(st->ID);
printf("Enter FN:"); scanf_s("%ld",&st->FN);
gets_s(bf);
}
void CountStudName(student*st){
int count, j, n, i;
char name1[31];
int FreqArr[31];
strcpy(st->name,name1);
printf("Search name occurrence: "); gets(name1);
for (i=0; i<BrS; i++)
FreqArr[i]=-1;
count=1;
for (j=i+1;j<BrS;j++)
{
if (strcmp(st->name,name1)==0)j ++;
count++;
FreqArr[j]=count;
}
printf("The searched name is %d times present\n", count);
}
void main() {
int i;
printf("Enter number of students:"); scanf_s("%d",&BrS);
gets_s(bf);
printf("-------------------\n");
for (i=0; i<BrS;i++) readStud(&MasStud[i]);
printf("-------------------\n");
CountStudName(&MasStud[i]);
_getch();
}
提前感谢您的热心帮助。
【问题讨论】:
-
我不知道您使用的是什么编译器,但这段代码在online GDB 无法编译。第一个错误是:
main.c:14:1: error: unknown type name ‘student’ student MasStud[35]; //array for number of students. -
您好,谢谢。我正在使用 Visual c++ 2010 并没有给出这个错误
-
编译器应该会告诉你
gets_s中的错误——没有足够的参数。奇怪的是,您还使用了过时的函数gets。请阅读Why is the gets function so dangerous that it should not be used?这段代码中有很多语法错误,请解决编译器告诉你的问题。 -
您还将
scanf_s()与gets_s()混合使用,这会导致麻烦。坚持一种输入法。此问题适用:scanf() leaves the newline char in the buffer。 -
请不要发布旨在更新问题的“答案”。相反,请使用标签下方的
edit按钮编辑问题,以进行您希望进行的更改。目前有一个待处理的编辑,因此您必须在进行进一步更改之前接受或拒绝待处理的编辑。谢谢。