0

我无法弄清楚错误在哪里。当我尝试运行该程序时,它不会通过选择,而是在询问选择后直接进入程序末尾。而且,它还在 Visual c++ 编译器的“显示构建的输出”中给出了一些警告。有人可以帮我解决这个问题吗?

     #include <stdio.h>
     int main ()
     {
     int card_number, choice, withdraw, deposit;
     float amount = 3000.00, new_amount = 0;
     char password;
     printf("Enter the card number: ");
     scanf("%d", &card_number);
     printf("Enter the Password: ");
     scanf(" %c", &password);
     printf("\n\n");
     printf("\n\t***********************************");
     printf("\n\t*           MENU                  *");
     printf("\n\t*     1. Check Balance            *");
     printf("\n\t*     2. Withdraw                 *");
     printf("\n\t*     3. Deposit                  *");
     printf("\n\t*     4. Exit                     *");
     printf("\n\t*                                 *");
     printf("\n\t***********************************");
     printf("\n\n");
     printf("Enter your choice: ");
     scanf("%d", &choice);

     if (choice == 1)
       {
      printf("Current balance on your account: %f\n", amount);
       }
     else if (choice == 2)
      {
    printf("Enter the amount you want to withdraw: ");
    scanf("%d", &withdraw);

    if (withdraw > amount)
      {
        printf("You don't have sufficient balance");
      }
    else
      {
        new_amount = amount - withdraw;
        printf("Current balance on your account: %f\n", new_amount);
      }
}

else if (choice == 3)
{
    printf("Enter the amount you want to deposit: ");
    scanf("%d", &deposit);
    amount = amount + deposit;
    printf("Current balance on your account: %d\n", amount);
}
else if (choice == 4)
{
    printf("Thank you for using our service\n\n");
}

return 0;
    }
4

4 回答 4

2

大概您希望密码长度超过一个字符,因此您需要一个“字符串”而不是单个char. 不幸的是,C 没有字符串类型,但它确实有char足够好的数组。scanf("%c",&password);您应该通过 请求“字符串”,而不是通过 请求字符scanf("%s",&password);但是password 必须定义为一个足够长的数组char来保存密码。[1]

这导致您的程序“跳到最后”的原因是因为%c它只读取一个输入字符。您可能输入了几个字符。稍后在程序中,您尝试读取无法使用密码的非数字的intvia ,因此调用失败,但您没有检查这些调用的返回值,因此您的代码不知道失败。%dscanf()


[1] 实际上password必须足够长以容纳用户决定输入的任何内容。确实,您应该调查格式宽度的使用,例如%20s防止数组溢出,这是一个严重的安全漏洞。但由于这是一项家庭作业,我们假设这些细节目前并不重要。

于 2013-10-18T06:50:22.713 回答
1

以下代码应该可以解决您的问题:

#include <stdio.h>
#include <conio.h>
int main ()
{
  int card_number, choice;
  float amount = 3000.00, withdraw=0.0, deposit, new_amount=0;
  char password;
  clrscr();
  printf(" INSERT YOUR ATM CARD : ");
  printf("\n\n");
  printf(" Enter the Password: ");
  scanf("%s", &password);
  clrscr();
  printf("\n\t***********************************");
  printf("\n\t*           MENU                  *");
  printf("\n\t*     1. Check Balance            *");
  printf("\n\t*     2. Withdraw                 *");
  printf("\n\t*     3. Deposit                  *");
  printf("\n\t*     4. Exit                     *");
  printf("\n\t*                                 *");
  printf("\n\t***********************************");
  printf("\n\n");
S:
  printf("\n Enter your choice: ");
  scanf("%d", &choice);
  if (choice == 1)
  {
    printf(" Current balance on your account: %f\n", amount);
    goto S;
  }
  else if (choice == 2)
  {
    printf(" Enter the amount you want to withdraw: ");
    scanf("%f",&withdraw);
    if (withdraw>amount)
    {
      printf(" \n You don't have sufficient balance\n ");
      goto S;
    }
    else
    {
      amount = amount - withdraw;
      printf(" \n Current balance on your account: %f\n",amount);
      goto S;
    }
  }
  else if (choice == 3)
  {
    printf(" \n Enter the amount you want to deposit: ");
    scanf("%f", &deposit);
    amount = amount + deposit;
    printf(" \n Current balance on your account: %f\n", amount);
    goto S;
  }
  else if (choice == 4)
  {
    printf(" \n Thank you for using our service\n\n");
    getch();
  }
  else
  {
    printf(" \n Enter correct Choice and Try Again \n\n");
    goto S;
  }

  getch();
  return 0;
}
于 2013-12-13T06:47:24.227 回答
0

在阅读字符之前输入这个fflush(stdin);

或者做

 scanf(" %c", &password);// see the extra space between '"' and '%'
于 2013-10-18T06:34:28.460 回答
0
#include<iostream.h>


int main()

{

       int password;

   for (int i=0;i<3;i++)

   {cout <<"enter password:\n";
        cin>>password;

        if (password==123456)
        {cout<<"correct!!!\n";

        double balance = 10000;
        double withdraw, deposit;
        int option;
cout<<"\n";
cout<<"            ***Western Ace***\n";
        cout<<"*** Automated Teller Machine***"<<endl;
        cout<<"Choose a Transaction:\n";
        cout<<"\n";
        cout    <<"[1] Inquire Balance \n"
                <<"[2] Withdraw \n"
                <<"[3] Deposit \n"
                <<"[4] Quit \n"
                <<"\n"
                <<"Enter Option:";
        cin>>option;

    switch(option)
    {
    case 1:
            cout<<"\n[[[BALANCE INQUIRY]]]\n";
            cout.setf(ios::fixed);
            cout.setf(ios::showpoint);
            cout.precision(2);
            cout<<"\n Your current balance is $"<<balance<<endl;
            break;
    case 2:
            cout<<"\n[[[WITHDRAW]]]\n";
            cout<<"Enter amount: $";
            cin>>withdraw;

            balance = balance - withdraw;

            cout.setf(ios::fixed);
            cout.setf(ios::showpoint);
            cout.precision(2);

            cout<<"You withdrew $"<<withdraw<<endl;
            cout<<"Your remaining balance is $"<<balance<<endl;
    continue;
    case 3:
            cout<<"\n[[[DEPOSIT]]]\n";
            cout<<"Enter amount: $";
            cin>>deposit;

            balance = balance + deposit;

            cout.setf(ios::fixed);
            cout.setf(ios::showpoint);
            cout.precision(2);

            cout<<"You deposited $"<<deposit<<endl;
            cout<<"Your new balance is $"<<balance<<endl;
            continue;
            case 4:
            cout<<"\n***[[[EXIT MODE]]]***\n";

    break;


    default:
            cout<<"\n That is an invalid option \n";
    }






            break;
    }
    else


            cout<<"Pls try again!!!\n";}

return 0;
}//
于 2014-10-15T07:46:59.670 回答