簡體   English   中英

c 編程密碼正確后如何停止我的代碼循環

[英]How to stop the looping for my code after the password is correct for c programing

正確輸入密碼后循環不會停止 - 為什么會發生?

密碼是1234 ,但是輸入之后,循環就停不下來了,會循環。 5次后循環將停止,並且不能使用break或任何東西。

#include <stdio.h>  
#include <stdlib.h>
   
int check_pass(int pass);

int main()
{
    int x,pass;

    for(x=0;x<5;x++){ //the looping couldn't stop
    printf("Enter your password: ");
    scanf("%d",&pass);
    check_pass(pass);
    }
    return 0;
}

int check_pass(int pass)
{
    if(pass == 1234){
        printf("Password is correct\n");
        printf("==============  Hospital City  =============== ");
    }
    else{
        printf("Password is wrong, please try again\n");
    }
    return 0;
}

您需要更好地用帶有條件的while循環替換您的for循環:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
    
bool check_pass(int pass);
    
int main()
    
{
  int x,pass;
  bool Continue = true;

  x = 0;

  while (Continue && (x < 5))
  {
    printf("Enter your password: ");
    scanf("%d",&pass);
    if (check_pass(pass))
    {
      printf("Password is correct\n");
      printf("==============  Hospital City  =============== ");
      Continue = false;
    }
    else
    {
      printf("Password is wrong, please try again\n");
      x++;
    }    
  }
  
  return 0;
}
bool check_pass(int pass)
{
  bool Result;
  
  if(pass == 1234){
    Result = true;
  }
  else {
    Result = false;
  }

  return Result;
}

正如 Devolus 在評論中提到的那樣,更改最少的解決方案是:

#include <stdio.h>  
#include <stdlib.h>
        
int check_pass(int pass);
        
int main()   
{
    int x, pass;
        
    for(x=0;x<5;x++) { //the looping couldn't stop
        printf("Enter your password: ");
        scanf("%d",&pass);
        if (check_pass(pass)) {
            break;
        }
    }
        
    return 0;
}

int check_pass(int pass)
{
    if(pass == 1234) {
        printf("Password is correct\n");
        printf("==============  Hospital City  =============== ");
        return 1;
    } else {
        printf("Password is wrong, please try again\n");
        return 0;
    }
}

所需更改在以下代碼中注釋 -

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

int check_pass(int pass);

int main()
{
    int x,pass;

    for(x=0;x<5;x++){
        printf("Enter your password: ");
        scanf("%d",&pass);
        if( check_pass(pass) == 1){// change in code
            break;
        }
    }
    return 0;

}
int check_pass(int pass)
{
    if(pass == 1234){
        printf("Password is correct\n");
        printf("==============  Hospital City  =============== ");
        return 1;// change in code
    }
    else{
        printf("Password is wrong, please try again\n");
        return 0;// change in code
    }
    
}

首先,您必須始終控制scanf的返回值。 如果你不這樣做並且得到一個非數字值,那么后面的每個scanf都會阻塞該字符,並將其留在輸入緩沖區中以供下一個 => 無限循環使用。

接下來,如果您決定在 function 中測試有效性(這很好),則 function 必須為有效和無效測試返回不同的值,並且必須在調用者中檢查返回的值。

代碼可以變成:

#include <stdio.h>

int check_pass(int pass);

int main()

{
    int x, pass, cr;

    for (x=0; x<5; x++) { //allow 5 tries
        printf("Enter your password: ");
        cr = scanf("%d", &pass);       // ensure an integer was given
        if (cr != 1) {
            char buf[1024];           // "flush" input line in not
            if (NULL == fgets(buf, sizeof(buf), stdin)) {
                printf("Read error");  // give up on EOF or error
                break;
            }
            continue;                // do not test validity here
        }
        cr = check_pass(pass);       // test the integer value
        if (1 == cr) break;          // break loop if correct
    }
    return 1 - cr;                   // return 0 only on a correct password

}
int check_pass(int pass)
{
    if (pass == 1234) {
        printf("Password is correct\n");
        printf("==============  Hospital City  =============== ");
        return 1;
    }
    printf("Password is wrong, please try again\n");
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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