从用户那里读取一个数字

时间:2015-05-03 22:01:16

标签: c integer character user-input negative-number

我想从用户那里读取一个正整数。如果用户输入其他内容,我想再问一次。当用户输入负数时我就这样做了。但如果用户输入角色或其他任何内容,我该怎么办?

int main  (){   
    int takennumber;
    int number,multiplication,divisor,result,total=0;

    printf("Please,enter a integer number: ");
    scanf("%d",&takennumber);

    for(;takennumber<=0;)
    {
        printf("Wrong value! Please reenter: ");
        scanf("%d",&takennumber);
    }

2 个答案:

答案 0 :(得分:0)

.appear:hover span { display: block; position: absolute; margin: -272px 0px 0px -153px; padding: 0px 0px 0px 0px; z-index: 1000; top: 50%; left: 50%; width: 544px; height: 306px; border: 1px solid #000000; text-decoration: none; overflow: hidden; } 返回成功扫描的值的数量。如果您尝试扫描scanf但用户输入int,则char将返回0.您应将调用scanf移至循环条件并检查其返回值:

scanf

答案 1 :(得分:0)

如果你想完全控制输入处理(假设你只想要基数10),为什么不强行呢:

security:
encoders:
    FOS\UserBundle\Model\UserInterface: 
        algorithm:            pbkdf2
        hash_algorithm:       sha512
        encode_as_base64:     true
        iterations:           1000

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username_email

firewalls:
    login:
        pattern:  ^/auth/login
        stateless: true
        anonymous: true
        form_login:
            provider: fos_userbundle
            check_path:               /auth/login
            success_handler:          lexik_jwt_authentication.handler.authentication_success
            failure_handler:          lexik_jwt_authentication.handler.authentication_failure
            require_previous_session: false
            username_parameter: username
            password_parameter: password
            post_only:      true

    api_open:
        pattern: ^/api/open
        anonymous:    true
        stateless: true
        lexik_jwt: ~

    api_secured:
        pattern: ^/api
        anonymous:    false
        stateless: true
        lexik_jwt: ~

access_control: