![](/img/trans.png)
[英]How would I go about making this udpclient asynchronous using pthreads?
[英]How would i go about using 2 arrays in an && if statement
我程序的第一部分是创建2个不同的数组,分别是登录ID和密码。 输入完所有内容后,我希望程序具有5个不同的IF语句,该语句检查2个数组以查看ID和密码组合是否正确。
我目前正在第一个尝试这样的尝试,但没有成功
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char id[5];
char password[8];
int i, j, k;
int access;
char c;
printf("Please enter your ID: ");
scanf("%5c", id);
printf("\nPlease enter your password: ");
for(k = 0; k < 8; k++)
{
do
{
c = getch();
if(c != '\n' || c != '\r')
{
password[k] = c;
putchar('*');
break;
}
}while(c != '\n' || c != '\r');
}
for(i = 0; i < 8; i++)
{
if(password[i] >= '0' && password[i] <= '9')
{
password[i] = (char)((password[i] - '0' + 4 )% 10 + '0');
}
if((password[i] >= 'a' && password[i] <= 'z') || (password[i] >= 'A' && password[i] <= 'Z') )
{
password[i] = (char)((password[i] - 'a' + 4) % 26 + 'a');
}
}
if((strcmp(password, "abcdefgh") == 0) && (strcmp(id,"ft234")==0))
{
access = 1;
printf("Logged in.\n\n");
}
else if((strcmp(password, "bcdefghi")==0) && (strcmp(id, "ty394")==0))
{
access = 1;
printf("Logged in.\n\n");
}
}
第一个问题
在C语言中,您用双引号而不是简单的引号声明字符串(字符数组)。
例如: char string[20] = "my string"
使用简单的引号声明了一个字符。
例如: char ch = 'a'
第二个问题
您不能本机比较低级语言(例如C语言)中的字符串。 您必须使用strcmp
。
解
if ((strcmp(password, "string_passwd") == 0) && (strcmp(login, "string_login") == 0)) {
<both password and login are ok>
} else {
<one (or both) of password or/and login are not ok>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.