-5

I have problem with a function. I have a structure with student information as name, id number, marks etc. I am able to read and print all needed, but the problem is when i want to count the input equal names. For example if I enter to count name Iva, and it is present 3 times, to count 3. It returns 0 for what i used till now. Here is the code:

#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();
}

Thank you in advance for you kind help.

IvaG
  • 7
  • 1
  • I don't know what compiler you're using, but this code won't compile at [online GDB](https://www.onlinegdb.com/online_c_compiler). First error is: `main.c:14:1: error: unknown type name ‘student’ student MasStud[35]; //array for number of students`. – Bob Jarvis - Слава Україні Apr 04 '20 at 18:26
  • Hi, thank you. I am using Visual c++ 2010 and is not giving this error – IvaG Apr 04 '20 at 18:40
  • 1
    Compiler should be telling you about the error in [`gets_s`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/gets-s-getws-s?view=vs-2019) – not enough arguments. Strangely you also use the obsolete function `gets`. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) There are numerous syntax errors in this code, please address what the compiler tells you. – Weather Vane Apr 04 '20 at 18:51
  • You are also mixing the use of `scanf_s()` with `gets_s()` and this will lead to trouble. Stick to one input method. This question applies: [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Apr 04 '20 at 18:57
  • Please do not post "answers" which are intended to update a question. Instead, please **edit the question** using the `edit` button just below the tags to make the changes you wish to make. At the moment there's a pending edit so you'll have to either accept or reject the pending edit before making further changes. Thanks. – Bob Jarvis - Слава Україні Apr 04 '20 at 19:34

1 Answers1

1

first you should know VS will make a .cpp empty project for you by default and if you don't make .c project you will not receive many errors which are important.(I think you are using .cpp)

if you want to use student instead of struct student you should use typedef(if you code in .cpp projects you won't receive any error for this mistake.

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

}student;

also you need to send two argument to gets look char*_Buffer and rsize_t _Size.you are only sending one ,but anyway using gets is not safe use fgets or scanf like below:

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

as for your CountStudName function note when you call it , you have already traverse array MasStud here for (i=0; i<BrS;i++) . so when you send CountStudName(&MasStud[i]); to function nothing special is stored in that element.

I have rewrote this function to take a name and search for it and print how many students have that name:

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);

}

and you should call it like this in main: CountStudName(MasStud);

hanie
  • 1,863
  • 3
  • 9
  • 19