I am entering valid name Steve xyz @ //1
then also it is showing invalid name @ //2
. I am unable to compare the name in structure and name inputted by user.
fgets()
reads also the newline ('\n'
) character at the end of the buffer (when possible). a newline is added when you press Enter at the console and it will make the comparison fails. from man fgets
:
Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
so the content of empName
array for input "Steve xyz" will likely to be:
['S', 't', 'e', 'v', 'e', ' ', 'x', 'y', 'z', '\n', '\0', ...garbage]
here a rewrite of the code, basically you have to remove '\n'
if it's there. there was also a problem in the initialization of the struct array:
#include <stdio.h>
#include <string.h>
struct employee {
int cardNo;
char name[20];
long salary;
int age;
} emp[20] = {
{0, "John Doe", 6500, 25},
{1, "Steve xyz", 7000, 30},
// initialize here other 18 structs if needed
};
int main(void)
{
char empName[30];
printf("Enter your name\n");
char *ret = fgets(empName, 30, stdin);
if (ret == NULL) {
printf("Error while reading name\n");
return 1;
}
// remove eventual trailing newline replacing it with '\0'
empName[strcspn(empName, "\n")] = '\0';
if (!(strcmp(empName,emp[1].name))) { // 1
// I don't know why you do this...
strcpy(empName, "Steve xyz");
} else {
printf("Invalid name\n"); // 2
}
}
edit: changed newline removal approach using strcspn()
(thanks to @AndrewHenle in the comments) which seems to be a perfect fit for this task. from man strcspn
:
size_t strcspn(const char *s, const char *reject);
calculates the length of the initial segment of s which consists entirely of bytes not in reject. returns the number of bytes in the initial segment of s which are not in the string reject.
fgets reads the characters from the input stream and also a newline character. So you need to remove the newline character from the empName and this can be done by:
empName[strlen(empName) - 1] = '\0';
#include <stdio.h>
#include <string.h>
typedef struct employee
{
int cardNo;
char name[20];
long salary;
int age;
} Employee;
int main()
{
char empName[30];
Employee emp[20];
emp[1].age = 30;
emp[1].salary = 7000;
emp[1].cardNo = 1;
strcpy(emp[1].name, "Steve xyz");
int len = strlen(emp[1].name);
emp[1].name[len] = '\n';
printf("Enter your name\n");
fgets(empName, 30, stdin);
if (!(strcmp(empName, emp[1].name))) //1
{
strcpy(empName, "Steve xyz");
}
else
{
printf("Invalid name\n"); //2
}
}
Tha fact is fgets()
is reading also the \n
char, so you have to do this after using fgets()
size_t len = strlen(empName);
if (len > 0 && empName[len-1] == '\n') {
empName[--len] = '\0';
}
With this we delete the \n
, and now the comparison will determinate that are equals