gpt4 book ai didi

c - 有没有可能在c语言中循环strcmp函数?

转载 作者:行者123 更新时间:2023-12-02 02:10:48 27 4
gpt4 key购买 nike

我是一名大学新生,目前正在学习C语言。昨晚我刚刚发现我的代码存在很大的问题,但我以我糟糕的技能无法解决任何问题...你能帮我解决这个问题吗?

(条件)

  • 您正在尝试使用 ID/密码登录,但只有 3 次机会。
  • 每个都有 5 个 ID 和密码。
  • 如果id不存在,则打印id不存在
  • 如果 id 存在但 pw 不匹配,则打印 pw 不匹配
  • 如果 id 和 pw 匹配,则打印登录成功并退出程序。

有没有办法循环标记的strcmp函数?

下面是代码。

#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;
}

感谢您的任何评论!祝你有美好的一天

最佳答案

您应该使用数组来收集要在循环中使用的值。

#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";

/* gather strings in arrays */
char* ids[] = { id0, id1, id2, id3, id4 };
char* pws[] = { pw0, pw1, pw2, pw3, pw4 };

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));

/* use the arrays for looping */
for (int j = 0; j < 5; j++) {
if (strcmp(idsol, ids[j]) == 0) {
idmat = 0;
if (strcmp(pwsol, pws[j]) == 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;
}

另一种方法是将字符串直接放入数组中:

#include <stdio.h>
#include <string.h>
int main(void) {
char ids[][6] = {
"user1",
"user2",
"user3",
"user4",
"user5"
};

char pws[][6] = {
"pass1",
"pass2",
"pass3",
"pass4",
"pass5"
};

char idsol[100] = { 0 };
char pwsol[100] = { 0 };

/* omit: same as the first code */
}

关于c - 有没有可能在c语言中循环strcmp函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67774930/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com