简体   繁体   English

我将如何在&& if语句中使用2个数组

[英]How would i go about using 2 arrays in an && if statement

The first part of my program i creating 2 different arrays, the login id and password. 我程序的第一部分是创建2个不同的数组,分别是登录ID和密码。 After that has all been entered I would like the program to have 5 different IF statements that checks the 2 arrays to see if the ID and Password combinations are correct. 输入完所有内容后,我希望程序具有5个不同的IF语句,该语句检查2个数组以查看ID和密码组合是否正确。

I am currently trying it like this for the first one with no success 我目前正在第一个尝试这样的尝试,但没有成功

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

}
}

First problem 第一个问题

In C language you declare strings (arrays of characters) with double-quotes, not simple quotes. 在C语言中,您用双引号而不是简单的引号声明字符串(字符数组)。

Ex: char string[20] = "my string" 例如: char string[20] = "my string"

Using simple-quotes declares a single character. 使用简单的引号声明了一个字符。

Ex: char ch = 'a' 例如: char ch = 'a'

Second problem 第二个问题

You can't compare strings natively in low-level languages such as C language. 您不能本机比较低级语言(例如C语言)中的字符串。 You must use strcmp . 您必须使用strcmp

Solution

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.

相关问题 我将如何使用pthreads使此udpclient异步? - How would I go about making this udpclient asynchronous using pthreads? 我将如何阅读 go 并将此文本文件的信息分离到 arrays 中? - How would I go about reading and separating this text file's information into arrays? 我 go 如何在每次循环迭代后打印语句? C - How Would I go about Printing a Statement after each iteration of the loop? C 我 go 如何使用 I/O 重定向扫描带有空白字符的文本文件中的浮点值? - How would I go about scanning float values in a text file with whitespace characters using I/O Redirection? 我将如何使用Ncurses在屏幕周围制作盒子 - How Would I go about making a box around the screen using Ncurses 我 go 如何释放这个 malloc 以防止在使用 valgrind 时出现任何 memory 泄漏 - How would I go about freeing this malloc in order to prevent any memory leaks when using valgrind 我将如何编写Linux TTY嗅探器? - How would I go about writing a Linux TTY sniffer? 我将如何创建自己的VM? - How would I go about creating my own VM? 我将如何为我的语言添加定义? - How would I go about adding definitions to my language? 我 go 如何在 C 中反转二维数组? - How would I go about reversing a 2D array in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM