Reputation: 3
I'm relatively new to C language programming. I'm trying to program a School Record Management System for a school project. I understand your policy with regards to the "I'm not going to do your homework for you" so I will not ask how to do everything though I will provide the entire code I'm working on. The problem I'm facing at the moment is that I'm using strcpy and strcmp to prevent the user from assigning an already existing ID to a new student, but the compiler shows a warning/problem that I don't quite understand.
Here is the compiler log:
Compiler: TDM-GCC 4.7.1 64-bit Release
Executing gcc.exe...
gcc.exe "C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_StudentManagement System.c"
-o "C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.exe"
-I"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include"
-L"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib"
-static-libgcc C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_StudentManagement System.c:
In function 'add_student':
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management
**System.c:222:4: warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
**c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:51:18: note: expected 'char * __restrict__' but argument is of type 'char'**
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management
**System.c:222:4: warning: passing argument 2 of 'strcpy' from incompatible pointer type [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-**mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:51:18: note: expected 'const char * __restrict__' but argument is of type 'char (*)[50]'**
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management
**System.c:227:5: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
**c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:53:15: note: expected 'const char *' but argument is of type 'char'
C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:227:5: warning: passing argument 2 of 'strcmp' from incompatible pointer type [enabled by default]**
In file included from C:\Users\Sheng\Desktop\Restarting Project\ToStudent\Updated_Student Management System.c:8:0:
**c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.1/../../../../x86_64-w64-mingw32/include/string.h:53:15: note: expected 'const char *' but argument is of type 'char (*)[50]'**
Execution terminated
Compilation successful
Code:
struct Student
{
int num_student; // number of students
char student_name[50][50]; // name of students
char student_id[50][50]; // Student ID
int student_course_num[10]; // Number of course each student enroll
int student_course[20][20]; // The course code of the student
};
void add_student()
{
FILE *filePointer;
int i;
int user_input;
int j;
int replay = FALSE;
char testID[50];
struct Student profile[20];
printf("---------------------------------------------------\n");
printf("Option 3: Adding a Student\n");
printf("---------------------------------------------------\n");
printf("\n");
filePointer = fopen("Student.txt","a+");
if(filePointer == NULL)
{
printf("\nSystem Error...");
printf("\nPress any key to exit");
getch();
}
else
{
printf("Enter the number of students to be added: ");
scanf("%d",&user_input);
for(i=0;i<user_input;i++)
{
if(TRUE && replay == FALSE)
{
printf("Enter Student Name: ");
clearBuffer();
scanf("%[^\n]",profile[i].student_name);
}
if(TRUE || replay == TRUE)
{
printf("Enter Student ID (8 digits): ");
clearBuffer();
scanf("%[^\n]",profile[i].student_id);
}
strcpy(testID[i],profile[i].student_id);
for(j=0;j<i;j++)
{
if(strcmp(testID[j],profile[i].student_id) == 0)
{
printf("The ID already exists");
i--;
replay = TRUE;
}
}
if(replay == FALSE)
{
fprintf(filePointer,"%s\n%s\n",profile[i].student_name,profile[i].student_id);
}
}
}
fclose(filePointer);
return_menu();
}
Upvotes: 0
Views: 958
Reputation: 122
The warnings are because Second argument to strcmp() or strcpy()
must be a null terminated char array
(string) or a char pointer.
You are passing it a char **
i.e a double array as student_id is an array of arrays.
check these out :
http://www.cplusplus.com/reference/cstring/strcpy/
http://www.cplusplus.com/reference/cstring/strcmp/
Upvotes: 0
Reputation: 6080
Your Problem: testID is a char[50]
so testID[i]
is a single char (which is a small integer). strcpy()
wants as first parameter a pointer. So you make an Integer from an Pointer (without cast).
if you want to have many testIDs with a fixed maximum-size, your declaration should be something like char testID[50][100];
and please try to #define
some constants for these values!
Upvotes: 2