【问题标题】:Programming in C - count occurences of a char element from structure with function [closed]用 C 语言编程 - 使用函数计算结构中 char 元素的出现次数[关闭]
【发布时间】: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 按钮编辑问题,以进行您希望进行的更改。目前有一个待处理的编辑,因此您必须在进行进一步更改之前接受或拒绝待处理的编辑。谢谢。

标签: c counter


【解决方案1】:

首先你应该知道VS会默认为你创建一个.cpp空项目,如果你不创建.c项目你不会收到很多重要的错误。(我认为你正在使用.cpp )

如果您想使用student 而不是struct student,您应该使用typedef(如果您在.cpp 项目中编码,您将不会因为此错误而收到任何错误。

 typedef struct student {
    char name[31];
    char ID[11];//personal code
    long FN;//student number

}student;

您还需要向gets 发送两个参数,看看char*_Bufferrsize_t _Size。您只发送一个,但无论如何使用gets 是不安全的,使用fgetsscanf 如下所示:

scanf("%31s",st->name)//this will prevent buffer overflow 

至于你调用它时的CountStudName函数注释,你已经遍历数组MasStud这里for (i=0; i&lt;BrS;i++)。因此,当您发送 CountStudName(&amp;MasStud[i]); 到函数时,该元素中不会存储任何特殊内容。

我重写了这个函数来获取一个名字并搜索它并打印有多少学生有这个名字:

void CountStudName(student* st) {
    int count, j, n, i;
    char name1[31];
    printf("Search name occurrence: "); 
    scanf("%31s", name1);
    count = 0;
    for (j = 0; j < BrS; j++)
    {
        if (strcmp(st[j].name, name1) == 0)
        {
            count++;
        }
    }
    printf("The searched name is %d times present\n", count);

}

你应该在main这样称呼它:CountStudName(MasStud);

【讨论】:

    猜你喜欢
    • 2021-12-14
    • 2018-06-27
    • 1970-01-01
    • 2017-05-13
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2019-04-06
    相关资源
    最近更新 更多