请帮助我的代码。我想在我的atm代码中输入一个密码时终止所有字符以及字符和数字的组合。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
#define maxx 10
void menu(),quit(), login();
void withdrawinq(), depositinq(), balanceinq();
main(void)
{
int userID[ maxx ] = {1010, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
double balance;
float deposit, withdraw;
int pin = 0, x, y, ans;
char choice;
char str[9999];
for( pin = 1 ;pin <= maxx; pin++ ){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- \n\n");
printf(" *********************************************************************** \n");
printf("| | \n");
printf("* MINESKI BANK * \n");
printf("| | \n");
printf(" *********************************************************************** \n");
printf("\n ----------------------------------------------------------------------- \n");
printf("| LOG IN | \n");
printf(" ----------------------------------------------------------------------- \n");
printf("| | \n");
printf("| Please cover and secure your Account PIN while logging in | \n");
printf("| | \n");
printf(" ----------------------------------------------------------------------- \n");
printf("\n\n\n Enter PIN Number: ");
scanf("%d",&pin);
/* read string */
fgets(str, sizeof str, stdin);
/* remove trailing newline from string */
str[strlen(str) - 1] = '\0';
/* check string is OK */
if (strlen(str) != 4 ||
!isdigit((unsigned char)str[0]) ||
!isdigit((unsigned char)str[1]) ||
!isdigit((unsigned char)str[2]) ||
!isdigit((unsigned char)str[3]))
{
printf("Invalid pin, terminating\n");
system("pause");
/* convert string to integer */
pin = atoi(str);
printf("\n\n\n");
if(pin == 1010){
system("cls");
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Carlos Slim Helu");
printf("\n\t\t\t Account Number: 010030001");
balance = 7300000;
printf("\n\n");
}else if(pin == 1111){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t Name: William Henry Gates III");
printf("\n\t\t\t Account Number: 010030002");
balance = 6700000;
printf("\n\n");
}else if(pin == 2222){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Amancio Ortega");
printf("\n\t\t\t Account Number: 010030003");
balance = 5700000;
printf("\n\n");
}else if(pin == 3333){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Warren Buffett");
printf("\n\t\t\tAccount Number: 010030004");
balance = 5350000;
printf("\n\n");
}else if(pin == 4444){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Larry Ellison");
printf("\n\t\t\tAccount Number: 010030005");
balance = 4300000;
printf("\n\n");
}else if(pin == 5555){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Charles Koch");
printf("\n\t\t\tAccount Number: 010030006");
balance = 3400000;
printf("\n\n");
}else if(pin == 6666){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: David Koch");
printf("\n\t\t\tAccount Number: 010030007");
balance = 3400000;
printf("\n\n");
}else if(pin == 7777){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Li Ka-shing");
printf("\n\t\t\tAccount Number: 010030008");
balance = 3100000;
printf("\n\n");
}else if(pin == 8888){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Liliane Bettencourt");
printf("\n\t\t\t Account Number: 010030009");
balance = 3000000;
printf("\n\n");
}else if(pin == 9999){
printf("\n ----------------------------------------------------------------------- \n");
printf("| WELCOME | \n");
printf(" ----------------------------------------------------------------------- ");
printf("\n\n\t\t\t Name: Bernard Arnault");
printf("\n\t\t\t Account Number: 10030000");
balance = 2900000;
printf("\n\n");
}else{
printf("\n\n No such account registered.\n\n\n");
}
}
}
这是我的问题!我有10个帐户,您提供的代码先生将不接受代码中的帐户
答案 0 :(得分:0)
您应该读取一个字符串,然后检查字符串,然后转换为整数。这样的东西会检查PIN是否正好是四位数。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
char str[99];
int pin;
/* read string */
fgets(str, sizeof str, stdin);
/* remove trailing newline from string */
str[strlen(str) - 1] = '\0';
/* check string is OK */
if (strlen(str) != 4 ||
!isdigit((unsigned char)str[0]) ||
!isdigit((unsigned char)str[1]) ||
!isdigit((unsigned char)str[2]) ||
!isdigit((unsigned char)str[3]))
{
printf("Invalid pin, terminating\n");
exit(1);
}
/* convert string to integer */
pin = atoi(str);
}
答案 1 :(得分:0)
第一个问题是
printf("\n\n\n Enter PIN Number: ");
scanf("%d",&pin);
/* read string */
fgets(str, sizeof str, stdin);
为什么要尝试两次读取引脚?我向您展示了如何使用fgets
阅读它,但您仍在尝试使用scanf
阅读它。删除scanf。
第二个问题
if (strlen(str) != 4 ||
!isdigit((unsigned char)str[0]) ||
!isdigit((unsigned char)str[1]) ||
!isdigit((unsigned char)str[2]) ||
!isdigit((unsigned char)str[3]))
{
printf("Invalid pin, terminating\n");
system("pause");
/* convert string to integer */
pin = atoi(str);
您尚未终止if语句,应为
if (strlen(str) != 4 ||
!isdigit((unsigned char)str[0]) ||
!isdigit((unsigned char)str[1]) ||
!isdigit((unsigned char)str[2]) ||
!isdigit((unsigned char)str[3]))
{
printf("Invalid pin, terminating\n");
system("pause");
} /*** IF STATEMENT ENDS HERE ***/
else
{
/* convert string to integer */
pin = atoi(str);
等等。您应该尝试自己解决这些问题。它们非常简单,只要让别人为你编写代码就不会学到任何东西。