Wow, that code uses just about every evil construct that we violently attack on a daily basis.
>issit possible to compare(strcmp()) a string and a int ?
Not with strcmp directly. You could convert the int to a string or the string to an int and then do the comparison. But it looks like you want the user to be able to enter either an ID or a name. In that case it might be easier to try to convert and use that as your validation:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CORRECT_ID 12345
#define CORRECT_NAME "Robin Cole"
int main(void)
{
char buffer[BUFSIZ];
printf("Enter your User ID or User Name: ");
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin)) {
char *endp;
char *newline;
unsigned long id;
newline = strchr(buffer, '\n');
if (newline) {
*newline = '\0';
}
id = strtoul(buffer, &endp, 0);
if (endp == buffer) { /* User Name was entered */
if (strcmp(buffer, CORRECT_NAME) == 0) {
printf("Logging in...\n");
}
else {
printf("Sorry, incorrect User Name\n");
return EXIT_FAILURE;
}
}
else { /* User ID was entered */
if (id == CORRECT_ID) {
printf("Logging in...\n");
}
else {
printf("Sorry, incorrect User ID\n");
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}