Question: Why is it telling me to be careful when using gets() in my C program? Please modify the program, so it doesn't show a warning.

Why is it telling me to be careful when using gets() in my C program?

Please modify the program, so it doesn't show a warning. (Please don't change the structure of the code, I need to follow certain rules for the assignment.) Spend time on this.... Do not just copy and paste my code as the answer, I will downvote.

Program(Below):

#include

#include

#include

// function declarations

void getStudents(char* names[], int noOfStuds);

void getGrades(int** grade, int noOfStuds, int noOfTests);

void calcGrades(char gradeletter[], int** grade, int noOfStuds, int noOfTests);

void printStudents(char* names[], int noOfStuds);

void printGrades(int** grade, int noOfStuds, int noOfTests);

void printFinalGrades(char gradeletter[], int noOfStuds);

int main()

{

char buff[BUFSIZ];

// declaring variables

int noOfStuds, noOfTests, i;

// getting the inputs entered by the user

printf("How many students :");

scanf("%d", &noOfStuds);

printf("How many assignments :");

scanf("%d", &noOfTests);

// creating two dimensional array which stores grades

int* grade[noOfStuds];

for (i = 0; i < noOfStuds; i++)

grade[i] = (int*)malloc(noOfTests * sizeof(int));

// creating array which stores student names

char* names[noOfStuds];

// creating array which stores student grade letters

char gradeletter[noOfStuds];

// calling the functions

getStudents(names, noOfStuds);

getGrades(grade, noOfStuds, noOfTests);

calcGrades(gradeletter, grade, noOfStuds, noOfTests);

printStudents(names, noOfStuds);

printGrades(grade, noOfStuds, noOfTests);

printFinalGrades(gradeletter, noOfStuds);

return 0;

}

// This function will get the student names entered by the user

void getStudents(char* names[], int noOfStuds)

{

int i;

char ch;

char str[40];

while ((ch = getchar()) != ' ' && ch != EOF)

;

for (i = 0; i < noOfStuds; i++)

{

printf("Enter name of student#%d :", (i + 1));

gets(str);

names[i] = (char*)malloc(sizeof str);

strcpy(names[i], str);

}

}

// This function will get the student grades entered by the user

void getGrades(int** grade, int noOfStuds, int noOfTests)

{

int i, j;

char buff[BUFSIZ];

for (i = 0; i < noOfStuds; i++)

{

printf("Student #%d : ", (i + 1));

for (j = 0; j < noOfTests; j++)

{

printf("Enter score of student%d :", (j + 1));

scanf("%d", &grade[i][j]);

}

}

}

// This function will calculate the grade letter

void calcGrades(char gradeletter[], int** grade, int noOfStuds, int noOfTests)

{

int i, j;

double tot = 0.0, avg = 0.0;

for (i = 0; i < noOfStuds; i++)

{

for (j = 0; j < noOfTests; j++)

{

tot += grade[i][j];

}

avg = tot / noOfTests;

if (avg >= 90)

{

gradeletter[i] = 'A';

}

else if (avg >= 80 && avg < 90)

{

gradeletter[i] = 'B';

}

else if (avg >= 70 && avg < 80)

{

gradeletter[i] = 'C';

}

else if (avg >= 60 && avg < 70)

{

gradeletter[i] = 'D';

}

else if (avg >= 0 && avg < 60)

{

gradeletter[i] = 'F';

}

tot = 0.0;

}

}

// This function will display student names

void printStudents(char** names, int noOfStuds)

{

int i;

printf(" ");

for (i = 0; i < noOfStuds; i++)

{

printf("%s", names[i]);

printf("\t");

}

printf(" ");

}

// This function will display student grades

void printGrades(int** grade, int noOfStuds, int noOfTests)

{

int i, j;

for (i = 0; i < noOfTests; i++)

{

for (j = 0; j < noOfStuds; j++)

{

printf("%d\t", grade[j][i]);

}

printf(" ");

}

}

// This function will display student grade letters

void printFinalGrades(char gradeletter[], int noOfStuds)

{

int i;

for (i = 0; i < noOfStuds; i++)

{

printf("%c\t", gradeletter[i]);

}

printf(" ");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!