有人可以向我解释一下scanf如何与C的多个输入会话一起工作?

时间:2015-11-17 04:32:03

标签: c while-loop scanf

在任何人咆哮之前,我知道编码是丑陋的,并且可能很难完成所有变量,但只有for循环所以不要让那些打扰你。问题是,我必须接受用户的输入即:帐号和帐号密码,并根据随机帐号和随机密码生成的内容检查是否输入正确的密码和帐号。我检查帐号和帐号密码的功能,没问题。我在那里有一些DEBUG语句试图找出问题发生的地方。我注意到在while循环中出现的问题,如果用户在第一次尝试时输入了帐号或密码错误,它会循环返回并再次询问,但是当它再次询问用户第二个信息时尝试,来自用户的新信息的数组不存储新信息,而是仍然传递原始尝试的旧信息。是否有一些我不知道的scanf使得它不能扫描旧的新信息,或者甚至不能像那样工作?

示例输入和输出:

Account Number: 67561

PIN: 2531

Please Enter account Number: 67562

Please Enter account PIN: 2531

Account Number DEBUG: 67562Account PIN DEBUG: 2531

ACCNT2

PIN1

Please Enter account Number: 67561

Please Enter account PIN: 2531

Account Number DEBUG2: 67562Account PIN DEBUG2: 2531

DEBUG2 2DEBUG3 1Please Enter account Number: 

正如您在Debug2语句中的第二次尝试中所看到的那样,它仍然采用原始输入而不是采用我指定的新输入。关于这里有什么问题的任何想法?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int check_login(int rand_account[], int account[]);
int check_login_pin(int rand_pin[], int pin[]);

int main(void) {

    char account_nmb[5],account_pin[4];
    int i,e,x,y,a,b,number=1,attempts=1,rand_pin[4], rand_account_number[5], account_nmb_int[5],account_pin_int[4];
    int d,f,account_number,account_pn,g,h,j,k;
    time_t t;

    srand((unsigned) time(&t));

    //generate array of rand account numbers and pin numbers
    for(x=0;x<5;x++){
        rand_account_number[x]=rand()%9;
    }
    for(y=0;y<5;y++){
        rand_pin[y]=rand()%9;
    }

    //print array of rand account numbers and pin numbers
    printf("Account Number: ");
    for(a=0;a<5;a++){
        printf("%d",rand_account_number[a]);
    }
    printf("\n");
    printf("PIN: ");
    for(b=0;b<4;b++){
        printf("%d",rand_pin[b]);
    }
    printf("\n");

    //Check User's input for match with random account number and pin
    while(attempts!=3){
        //Take user's input
        printf("Please Enter account Number: ");
        scanf("%s", account_nmb);
        for (i = 0; i < 5; i++ ) {
            account_nmb_int[i] = account_nmb[i] - '0';
        }
        printf("Please Enter account PIN: ");
        scanf("%s", account_pin);
        for (e = 0; e < 4; e++ ) {
            account_pin_int[e] = account_pin[e] - '0';
        }

        //DEBUG
        printf("Account Number DEBUG: ");
        for(d=0;d<5;d++){
            printf("%d",account_nmb_int[d]);
        }
        printf("Account PIN DEBUG: ");
        for(f=0;f<4;f++){
            printf("%d",account_pin_int[f]);
        }


        account_number=check_login(rand_account_number, account_nmb_int);
        account_pn=check_login_pin(rand_pin,account_pin_int);

        //DEBUG
        printf("\nACCNT%d",account_number);
        printf("\nPIN%d\n",account_pn);

        if(account_number==2||account_pn==2){
            printf("Please Enter account Number: ");
            scanf("%s", account_nmb);
            for (g = 0; g < 4; g++ ) {
                account_nmb_int[g] = account_nmb[g] - '0';
            }
            printf("Please Enter account PIN: ");
            scanf("%s", account_pin);
            for (h = 0; h < 4; h++ ) {
                account_pin_int[h] = account_pin[h] - '0';
            }

            //DEBUG
            printf("Account Number DEBUG2: ");
            for(d=0;d<5;d++){
                printf("%d",account_nmb_int[d]);
            }
            printf("Account PIN DEBUG2: ");
            for(f=0;f<4;f++){
                printf("%d",account_pin_int[f]);
            }
            printf("\n");

            account_number=check_login(rand_account_number, account_nmb_int);
            account_pn=check_login_pin(rand_pin,account_pin_int);
            //DEBUG
            printf("DEBUG2 %d",account_number);
            printf("DEBUG3 %d",account_pn);
            attempts++;
        }
        else{
            printf("\nCONGRATS\n");
            attempts=3;
        }
    }

    return 0;
}

int check_login(int rand_account[], int account[]){
    int index,number;

    //check user account number and pin number with random generation
    for(index=0;index<5;index++){
        if(account[index]==rand_account[index]){
            number=1;
        }
        else{
            number = 2;
            break;
        }
    }
    return number;
}

int check_login_pin(int rand_pin[], int pin[]){
    int index, number;
    for(index=0;index<4;index++){
        if(pin[index]==rand_pin[index]){
            number=1;
        }
        else{
            number = 2;
            break;
        }
    }
    return number;

}

1 个答案:

答案 0 :(得分:0)

问题的答案是首先修复帐号和密码的数组大小,以包含&#39; \ 0&#39; @Jonathan Leffler指出的null终止字符。其次另一个问题是让if语句运行第二个输入和打印阶段,而不应该只是将while循环推送到它的第二次迭代,允许新输入通过@kaylum指出。感谢大家对阵列和数据存储的帮助和建议!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int check_login(int rand_account[], int account[]);
int check_login_pin(int rand_pin[], int pin[]);

int main(void) {

    char account_nmb[6],account_pin[5];
    int i,e,x,y,a,b,attempts=0,rand_pin[5], rand_account_number[6], account_nmb_int[6],account_pin_int[5];
    int d,f,account_number,account_pn;
    time_t t;

    srand((unsigned) time(&t));

    //generate array of rand account numbers and pin numbers
    for(x=0;x<5;x++){
        rand_account_number[x]=rand()%9;
    }
    for(y=0;y<4;y++){
        rand_pin[y]=rand()%9;
    }

    //print array of rand account numbers and pin numbers
    printf("Account Number: ");
    for(a=0;a<5;a++){
        printf("%d",rand_account_number[a]);
    }
    printf("\n");
    printf("PIN: ");
    for(b=0;b<4;b++){
        printf("%d",rand_pin[b]);
    }
    printf("\n");


    //Check User's input for match with random account number and pin
    while(attempts!=3){
        //Take user's input
        printf("Please Enter account Number: ");
        scanf(" %s", account_nmb);
        for (i = 0; i < 5; i++ ) {
            account_nmb_int[i] = account_nmb[i] - '0';
        }
        printf("Please Enter account PIN: ");
        scanf(" %s", account_pin);
        for (e = 0; e < 4; e++ ) {
            account_pin_int[e] = account_pin[e] - '0';
        }

        //DEBUG
        printf("Account Number DEBUG: ");
        for(d=0;d<5;d++){
            printf("%d",account_nmb_int[d]);
        }
        printf("Account PIN DEBUG: ");
        for(f=0;f<4;f++){
            printf("%d",account_pin_int[f]);
        }


        account_number=check_login(rand_account_number, account_nmb_int);
        account_pn=check_login_pin(rand_pin,account_pin_int);

        //DEBUG
        printf("\nACCNT%d",account_number);
        printf("\nPIN%d\n",account_pn);

        if(account_number==2||account_pn==2){
            attempts++;
        }
        else{
            printf("\nCONGRATS\n");
            attempts=3;
        }
    }

    return 0;
}

int check_login(int rand_account[], int account[]){
    int index,number=1;

    //check user account number and pin number with random generation
    for(index=0;index<5;index++){
        if(account[index]==rand_account[index]){
            number=1;
        }
        else{
            number = 2;
            break;
        }
    }
    return number;
}


int check_login_pin(int rand_pin[], int pin[]){
    int index, number=1;
    for(index=0;index<4;index++){
        if(pin[index]==rand_pin[index]){
            number=1;
        }
        else{
            number = 2;
            break;
        }
    }
    return number;

}