C语言 使用 if 和 else if 为简单 ATM 机编写的 c 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19442991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c program for a simple ATM machine using if and else if
提问by anansharm
I am not able to figure out where is the mistake. When I am trying to run the program, Its not going pass the choice and its going directly to the end of the program after asking for the choice. And, its also giving some warning in "Show output from build" in visual c++ compiler. Can anyone help me with this, please?
我无法弄清楚错误在哪里。当我尝试运行该程序时,它不会通过选择,而是在要求选择后直接进入程序末尾。而且,它还在 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;
}
采纳答案by AJ.
before reading character type this fflush(stdin);
在阅读字符之前输入这个 fflush(stdin);
or do
或做
scanf(" %c", &password);// see the extra space between '"' and '%'
回答by Dwayne Towell
Presumably you want the password to be more than one character long, so you need a "string" not a single char. Unfortunately, C does not have a string type, but it does have arrays of charwhich is good enough. Instead of requesting a character via scanf("%c",&password);, you should request a "string" via scanf("%s",&password);. However, passwordmustbe defined as an array of charlong enough to hold the password.[1]
大概您希望密码长度超过一个字符,因此您需要一个“字符串”而不是单个char. 不幸的是,C 没有字符串类型,但它有char足够好的数组。scanf("%c",&password);您应该通过请求“字符串”,而不是通过 请求字符scanf("%s",&password);。但是,password必须定义为char足够长的数组以保存密码。[1]
The reason this is causing your program to "skip to the end" is because the %conly reads one character of input. You probably typed several characters. Later in the program you attempted to read an intvia %dwhich could not consume the non-digits of your password and therefore the scanf()calls failed, but you did not check the return value from these calls so your code did not know about the failures.
这导致您的程序“跳到最后”的原因是因为%c它只读取输入的一个字符。您可能输入了几个字符。后来在节目中你试图读取int通过%d这可能不会消耗你的密码的非数字字符,因此scanf()失败的呼叫,但没有检查这些调用的返回值,以便您的代码不知道失败。
[1] Actually passwordmust be long enough to hold whatever the user decides to enter. Really you should investigate the use of format widths such as %20sto prevent array overruns, which are a serious security hole. But since this is a homework assignment, we'll assume those sorts of details are not really important at the moment.
[1] 实际上password必须足够长以容纳用户决定输入的任何内容。实际上,您应该调查格式宽度的使用,%20s以防止数组溢出,这是一个严重的安全漏洞。但由于这是一项家庭作业,我们假设这些细节目前并不重要。
回答by Vinay Sharma
The following code should solve your problem:
以下代码应该可以解决您的问题:
#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;
}
回答by Jhon Paul Maprangala
#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;
}//

