2015-10-29 110 views
-3

任何人都知道这个问题?它只检测第一个字符。我不知道这个问题,请帮忙。我找不到答案。多个if语句C 446

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <string.h> 
#include <math.h> 

int main() 
{ 
    int password; 

    printf("Enter your password. \n"); 
    printf("Password must contain an uppercase letter, a lowercase letter, and a   number. \n"); 
    scanf("%c", &password); 

    if(isupper(password)){ 
     printf("Password meets requirement 1. \n"); 
    } 
    if(islower(password)){ 
     printf("Password meets requirement 2. \n"); 
    } 
    if(isdigit(password)){ 
     printf("Password meets requirement 3. \n"); 
    } 

    return 0; 
} 
+0

引擎收录在这里:http://pastebin.com/ZdDHgpx8 – pushcode

+0

'isupper','islower'和'isdigit'对单个字符,而不是字符串操作。 – keithmo

+0

您正在将单个字符读入一个应该是密码的int变量。这不是你如何使用scanf,整数变量或密码。 – Magisch

回答

0

您只读取单个字符,然后使用函数来测试此单个字符。读入字符缓冲区(例如char password[128]使用fgets(password, 128, stdin)),然后遍历您的密码并测试各个的字符。

1

可变password是一个单一的实体,它只能存储一个字符。你也读取单个字符。 This scanf (and family) reference可能会有所帮助。

如果要读取多个字符,则需要使用"%s"格式,而且还需要数组的字符。像

char password[32]; 
scanf("%31s", password); 

"%31s"的格式告诉scanf读取至多31个字符,并存储为一个零终止的字符串(因此仅至多31个字符读取32个字符的数组来存储)。


那么对于其他代码,你需要使用循环遍历字符串。在这里,你有两个选择,当谈到知道字符串的结尾:要么使用strlen得到字符串的长度,或依赖于一个事实,即在C字符串由零(字符'\0')终止。

0

您使用了一个int,并存储它进入的第一个字符的ASCII值。相反,您应该使用字符数组(char [])或字符指针(char *)并为其分配内存,然后在scanf中使用%s(而不是%c)捕获输入的密码。