Error in strcmp () when I use structure as parameter

My program needs these features:

NOTE. I did not include codes for numbers 1,2 and 4, since I already finished them. The third is my problem.

  • The program must constantly allow user input if the user still wants to. (Dynamically)
  • Get the final student class (average for frst_grade, scnd_grade, fnl_grade)
  • Get the number of college students.
  • Get the student name by entering s_id.

My problem is how to compare the search input with the user input in s_college to get the number of students. The only way I know is to use strcmp (), but it gives me this error: invalid conversion from 'char' to 'const char *' [-fpermissive]

So, how do I compare these two to get the number of college students?

#include<stdio.h> #include<string.h> #include<conio.h> int i,n,sum,search,num=0,ctr=0; char answer,choice,choice2,search2; struct record{ int s_id; char s_name[100]; char s_course; char s_college[5]; int s_scoress; }id[100],name[100],course,college[100],scores; struct s_scores{ int frst_grade; int scnd_grade; int fnl_grade; }first,second,final; void ADD(); void COLLEGE(); void ID(); void COLLEGE(){ printf("Enter college (abbreviation only)"); scanf("%s",&search2); for(i=0;i<num;i++){ if(strcmp(college[i].s_college,search2)==0); ctr++; } printf("The number of students in %s is %d",search2,ctr); 
+5
source share
6 answers

Let's look at these (partial) lines:

 char ..., search2; ... scanf("%s",&search2); ... ...strcmp(college[i].s_college,search2)... 

The variable search2 is a single character. When you try to insert a line into it, at least two characters will be written: the line you are reading, plus the line delimiter. This means that you go beyond.

Then you use a character variable as an argument to strcmp , which converts the contents of search2 to a pointer and uses that pointer as a pointer to a string.

Both of these problems will lead to undefined behavior.

Is search2 string? Then declare it as an array, e.g.

 char ..., search2[100]; 

If search2 should be one character, then first you need to read one character

 scanf("%c", &search2); // Note the changed format to read a single character 

And then you need to change your comparison so as not to use strcmp .

+7
source

You cannot use strcmp with what is not a terminating null string. You can write

 if(college[i].s_college[0] == search2 && college[i].s_college[1] == '\0') 

Remember to remove the garbage semicolon so that the if works.

+5
source

Your search2 is just a symbol. You will need a string

Perhaps declare search2 as follows:

 char search2[50]; 

Also read about scanf to prevent buffer overflows:

 scanf("%49s", search2); // Do not go past the end of search2[50] 
+5
source

Well, the compiler tells you about the error: the variable search2 is char, and s_college[5]; - an array of characters. The strcmp function requires two arrays / pointers to work.

If search2 is only one byte, you can create: char Search2[2]; which will contain one char and trailing null. But this will only work if search2 is one byte. If you need to compare two character arrays, where search2 greater than one byte, then you should probably consider dynamic allocation or create a static array char search2[some_length]; .

+1
source

this is not a complete "answer", however it fixes some of the main problems in the code:

Define your structure as follows:

 struct s_scores { int frst_grade; int scnd_grade; int fnl_grade; }; struct record { int s_id; char s_name[100]; char s_course; char s_college[5]; struct s_scores s_scoress; }; struct record records[100]; 

Then select individual fields similar to:

 if( 1 != scanf( "%4s", records[i].s_college ) ) { perror( "scanf for college abbreviation failed" ); exit( EXIT_FAILURE ) } // implied else, scanf successful // validate the college abbreviation for( size_t j=0; j< (sizeof(collegesAbbrevationsTable)/(sizeof( *collegeAbbreviationsTable ); i++ ) { if( strncmp( collegeAbbreviationsTable[j], records[i].s_college, 4) { // then found matching abbreviation break; // exit 'validation' loop } } 

Note: perror() found in stdio.h . exit() and EXIT_FAILURE found in stdlib.h .

Note. In C, when referring to an array, the result is a pointer to the first byte of this array, so when calling scanf() you should not use & when referring to the array s_college[] .

`

+1
source

declae search2 as char search2 [10]; or char * search2;

Reason: string2 is a character variable, and college is the null terminating char array.

Signature stncmp id int strcmp (const char * s1, const char * s2);

So, for proper operation you need to pass an array of char * or char (which again is char *).

-2
source

All Articles