簡體   English   中英

在 C 的開關盒內檢測 Enter?

[英]Detecting Enter within a switch case in C?

在這方面遇到的麻煩比我應該的要多。 我正在構建一個非常簡單的銀行菜單界面,用戶可以在其中查看余額、存款、取款或退出菜單。 case 1case2case3似乎工作正常,但case 4不正確 function ,無論我嘗試什么谷歌。

在這個當前版本中,出現第case 4種情況下的printf()語句,然后似乎什么都沒有發生,直到我輸入其他選項之一 (1 - 3) 兩次,此時循環將循環並自動使用第二個選項輸入是。 我嘗試了getchar()但這似乎不起作用(或者我沒有正確實現它)。

為什么會發生這種行為,我該如何解決?

這是代碼:

#include <stdio.h>
#include <cs50.h>

double deposit(double a, double b);
double withdraw(double a, double b);

int main(void)
{
    int resume = 1;
    int user_input = 0;
    double user_balance = 10.00;
    printf("Welcome to UBank!\n");

    while (resume)
    {
        printf("\n====================\n");
        printf("Select an operation:\n\n1. Show Balance\n2. Make a Deposit\n3. Make a Withdrawal\n4. Quit\n"
               "====================\n\n");
        scanf("%d", &user_input);

        int quit_character = 0x00;
        double deposit_amount = 0.00;
        double withdraw_amount = 0.00;

        switch (user_input)
        {
            case 1:
                printf("Balance: $%.2lf\n", user_balance);
                break;
            case 2:
                printf("How much would you like to deposit?\n");
                scanf("%lf", &deposit_amount);
                user_balance = deposit(user_balance, deposit_amount);
                break;
            case 3:
                printf("How much would you like to withdraw?\n");
                scanf("%lf", &withdraw_amount);
                user_balance = withdraw(user_balance, withdraw_amount);
                break;
            case 4:
                printf("Press Enter to finish banking or any other key to continue.\n");
                scanf("%d\n", &quit_character);
                if (quit_character == 0x0A)
                {
                    resume = 0;
                }
                break;
        }
    }
}

double deposit(double a, double b)
{
    if (b > 0 && b < 10000)
    {
        return a + b;
    }
    else
    {
        printf("Please enter a valid amount. (0.01 - 9999.99)\n");
        return a;
    }
}

double withdraw(double a, double b)
{
    if (b > 0 && a - b >= 10)
    {
        return a - b;
    }
    else if (b <= 0)
    {
        printf("Withdrawal amount must be greater than $0.00.\n");
    }
    else if (a - b < 10)
    {
        printf("Withdrawal amount invalid.  Remaining balance must be $10.00 or more.\n");
    }
    return a;
}

假設我想畫我家的外部。 我買了油漆,然后回到我家(從最近的商店開車 5 小時)。 當我回到家時,我意識到我忘了買一個畫筆。 我不想在接下來的 10 個小時里開車去拿油漆刷,但我應該怎么塗油漆呢?

我可以使用掃帚、拖把、抹布、我的手、我兒子的手等等,沒有一個比刷子更可取。 但后來我意識到……我有一匹馬,我剪下馬的主干並制作自己的畫筆。 我畫了一天。 下周當我需要另一把刷子時,再給我的馬夾一下。 這最終比 10 小時的車程便宜得多。

好吧……那是個老生常談的虛構故事。 關鍵是使用scanf()進行用戶輸入就像用豬鼻子畫畫。 這只是工作的錯誤工具。 最好按照您期望的方式編寫您自己的用戶輸入 function。

考慮下面的GetInput() function。 我必須自己做,但它肯定優於使用scanf()進行用戶輸入:

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

  double deposit(double a, double b);
  double withdraw(double a, double b);

  /*-----------------------------------------------------------------------------------
  ** Get input from user, and return it as specific types.  
  **
  ** Caller may specify NULL for any (or all) input types that are not needed.
  */
  void GetInput(int *intOut, char *charOut, double *doubleOut)
    {
    // Pointer to a temporary (allocated memory) buffer to hold user input.
    char    *line_A   = NULL;

    // Number of bytes allocated to the temporary (allocated) buffer.
    size_t   lineSize = 0;

    // Holds the number of bytes entered by the user, or (-1) for error.
    ssize_t  lineLength;

    lineLength=getline(&line_A, &lineSize, stdin);
    if(-1 == lineLength)
       {
       fprintf(stderr, "getline() failed.\n"
       goto CLEANUP;
       }

    // If the caller did not specify NULL for intOut, ...
    if(intOut)
      // Convert the string to an int and pass it back to the caller.
      *intOut = atoi(line_A); 

    if(charOut)
      *charOut = *line_A;

    if(doubleOut)
      *doubleOut = atof(line_A);

  CLEANUP:
    if(line_A)
      free(line_A);

    return;
    }

  int main(void)
  {
      int resume = 1;
      int user_input = 0;
      double user_balance = 10.00;
      printf("Welcome to UBank!\n");

      while (resume)
      {
          printf("\n====================\n");
          printf("Select an operation:\n\n1. Show Balance\n2. Make a Deposit\n3. Make a Withdrawal\n4. Quit\n"
                 "====================\n\n");
          GetInput(&user_input, NULL, NULL);     // Get an integer from the user.

          char quit_character = 0x00;
          double deposit_amount = 0.00;
          double withdraw_amount = 0.00;

          switch (user_input)
          {
              case 1:
                  printf("Balance: $%.2lf\n", user_balance);
                  break;
              case 2:
                  printf("How much would you like to deposit?\n");
                  GetInput(NULL, NULL, &deposit_amount);  // Get a double from the user.
                  user_balance = deposit(user_balance, deposit_amount);
                  break;
              case 3:
                  printf("How much would you like to withdraw?\n");
                  GetInput(NULL, NULL, &withdraw_amount);  // Get a double from the user.
                  user_balance = withdraw(user_balance, withdraw_amount);
                  break;
              case 4:
                  printf("Press Enter to finish banking or any other key to continue.\n");
                  GetInput(NULL, &quit_character, NULL);  //Get a character from the user.
                  if (quit_character == 0x0A)
                  {
                      resume = 0;
                  }
                  break;
          }
      }
  }

  double deposit(double a, double b)
  {
      if (b > 0 && b < 10000)
      {
          return a + b;
      }
      else
      {
          printf("Please enter a valid amount. (0.01 - 9999.99)\n");
          return a;
      }
  }

  double withdraw(double a, double b)
  {
      if (b > 0 && a - b >= 10)
      {
          return a - b;
      }
      else if (b <= 0)
      {
          printf("Withdrawal amount must be greater than $0.00.\n");
      }
      else if (a - b < 10)
      {
          printf("Withdrawal amount invalid.  Remaining balance must be $10.00 or more.\n");
      }
      return a;
  }

暫無
暫無

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

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