簡體   English   中英

我將如何在&& if語句中使用2個數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM