c使用if和else if的简单ATM机程序

时间:2013-10-18 06:26:44

标签: c visual-c++

我无法弄清楚错误在哪里。当我试图运行该程序时,它不会通过选择,并在请求选择后直接进入程序的末尾。并且,它还在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 个答案:

答案 0 :(得分:2)

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

这导致程序“跳到最后”的原因是因为%c只读取一个输入字符。你可能输入了几个字符。在程序的后半部分,您试图通过int通过%d读取无法使用密码的非数字,因此scanf()调用失败,但您没有检查返回值从这些调用开始,所以你的代码不知道失败。


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

答案 1 :(得分: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;
}

答案 2 :(得分:0)

在阅读字符类型之前fflush(stdin);

或做

 scanf(" %c", &password);// see the extra space between '"' and '%'

答案 3 :(得分: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;
}//