I'm a freshman in university and currently learning the C language. Last night I've just found really big problem with my code but there's nothing I could fix with my terrible skills... can you help me to solve this?
(condition)
- you are trying to login with id / pw and only have 3 chances.
- there are 5 id's and pw's on each.
- if id does not exist, print
id does not exist. - if id exists but pw does not match, print
pw does not match. - if id and pw match, print
login successfuland exit program.
is there any way to loop the strcmp functions which marked?
Here's code below.
#include <stdio.h>
#include <string.h>
int main(void) {
char id0[] = "user1";
char id1[] = "user2";
char id2[] = "user3";
char id3[] = "user4";
char id4[] = "user5";
char pw0[] = "pass1";
char pw1[] = "pass2";
char pw2[] = "pass3";
char pw3[] = "pass4";
char pw4[] = "pass5";
char idsol[100] = { 0 };
char pwsol[100] = { 0 };
int idmat = 1;
int pwmat = 1;
int logstack = 0;
for (int i = 0; i <= 2;) {
logstack = i;
rewind(stdin);
printf("ID: ");
scanf_s("%s", idsol, sizeof(idsol));
printf("PW: ");
scanf_s("%s", pwsol, sizeof(pwsol));
if (strcmp(idsol, id0) == 0) { //i want to make these 5 blocks in one command.
idmat = 0;
if (strcmp(pwsol, pw0) == 0) {
pwmat = 0;
}
}
if (strcmp(idsol, id1) == 0) {
idmat = 0;
if (strcmp(pwsol, pw1) == 0) {
pwmat = 0;
}
}
if (strcmp(idsol, id2) == 0) {
idmat = 0;
if (strcmp(pwsol, pw2) == 0) {
pwmat = 0;
}
}
if (strcmp(idsol, id3) == 0) {
idmat = 0;
if (strcmp(pwsol, pw3) == 0) {
pwmat = 0;
}
}
if (strcmp(idsol, id4) == 0) { //these 5 blocks!!!
idmat = 0;
if (strcmp(pwsol, pw4) == 0) {
pwmat = 0;
}
}
if (idmat != 0) {
printf("ID does not exist.\n");
i++;
idmat = 1;
}
else if (idmat == 0 && pwmat != 0) {
printf("PW does not match.\n");
i++;
idmat = 1;
} else {
logstack = 0;
break;
}
}
if (logstack >= 2) {
printf("login failed.\n");
} else {
printf("login successful!\n");
}
return 0;
}
Thanks for any comment! have a nice day