2015-12-08 4 views
0

Его класс программирования в C, его простой банкомат, который делает 4 действия: Снятие, внесение, детали, проверка баланса пользователя и выход. Я сделал простую основу, но из-за изменения требований к проекту мне теперь нужно ввести в него пароль, я пробовал и придумал следующий код, но я хочу сделать так, чтобы он запрашивал пароль перед mainmenu, то есть: депозит , вывести и т. д. ... Я также попытался использовать случай переключения, но я чувствовал, что ничего не изменил. Любая помощь будет принята с благодарностью.Нужна помощь в отделке моего банкомата

код до сих пор, что было сделано мною, является:

#include<stdio.h> 
#include<conio.h> 
    int atmfun(); 
main() 
{ 
int option1; 
do 
option1=atmfun(); 
    while (option1==1); 

    getch(); 
    } 

    int atmfun() 
    { 


    char Name1[]="Fever"; 
    char Name2[]="JACK"; 
    char Name3[]="JOHN"; 
    char choice='y',choice1='n'; 
    char FullName1[]="Fever tyrell"; 
    char FullName2[]="Jack williams"; 
    char FullName3[]="John Mason"; 
    int i,a,AccBalance = 20000; 
    int acc_type,password,option,option1; 
    int passworda=31081; 
    int passwordb=12345; 
    int passwordc=12456; 
    int w_amount,d_amount; 
    printf("Welcome to generic ATM\n"); 
    printf("\n\nPlease choose an option..."); 
    printf("\n(1)View account details."); 
    printf("\n(2)Check balance."); 
    printf("\n(3)Withdraw amount."); 
    printf("\n(4)Deposit amount."); 
    printf("\n(5)Exit.\n"); 
    scanf("\n%d",&option); 



       //start of option 1 
       if(option==1) //if user wants to view their account details 
       { 
        printf("PLEASE ENTER YOUR PASSWORD: "); 
        scanf("%d",&password);   

       if 
      (password!=passworda&&password!=passwordb&&password!=passwordc) 

        printf("YOU HAVE ENTERED AN INVALID PASSWORD"); 
       else 
       if(password==passworda) 
       { 
        printf("WELCOME Mr.%s\n\n",Name1); 
        //rest of the account details 
       } 
       else 
       if(password==passwordb) 
       { 
        printf("WELCOME Mr.%s\n\n",Name2); 
        //rest of the acc details 
       } 
       else 
       if(password==passwordc) 
       { 
        printf("WELCOME Mr.%s\n\n",Name3); 
        //rest of the acc details 
       } 
       { 
        printf("\nDo you wish to go back to the main menu?\nEnter 1 for YES, and any other number for NO\n"); 
        scanf("%d",&option1); 
       if (option1==1) 
       { 
       return option1; 
       } 
       else 
        printf("Thank you for using Generic ATM"); 
       return 0; 
       } 
       } 
       // end of option1 

       // start of option 2 
       if(option==2) // if user wants to view his account balance 
        { 
        printf("Your account balance is $%d",AccBalance); 

        printf("\nDo you wish to go back to the main menu? \nEnter 1 for YES, and any other number for NO\n"); 
        scanf("%d",&option1); 
       if (option1==1) 
       { 
       return option1; 
       } 
       else 
        printf("Thank you for using Generic ATM"); 
       return 0; 
       } 

       //end of option 2 


       //start of option 3 
       if(option==3)// if user wants to withdraw money 
       { 
        printf("Enter amount to withdraw\n$"); 
        scanf("%d",&w_amount); 
       { 
       if(w_amount<=0) 
       { 
        printf("Amount cannot be a number under zero or zero"); 
        for(i=4;i>0;i--) 
       { 
        printf("\nEnter appropriate amount,you have %d tries left\n",i); 
       { 
        printf("Enter amount to withdraw\n$"); 
        scanf("%d",&w_amount); 
       if(w_amount<=0) 
       { 
        printf("You cannot withdraw this amount"); 
       } 
       } 
       if (i==1) 
        printf("\nYou have exceeded your tries\n"); 
       } 
       } 
       else 
       if(w_amount>AccBalance) 
       { 
        printf("You don't have sufficient funds, please enter the amount within your Account Balance"); 
        for(i=4;i>0;i--) 
       { 
        printf("\nEnter appropriate amount,you have %d tries left\n",i); 
       { 
        printf("Enter amount to withdraw\n$"); 
        scanf("%d",&w_amount); 
       if(w_amount<=0) 
       { 
        printf("You cannot withdraw this amount"); 
       } 
       } 
       if (i==1) 

        printf("\nYou have exceeded your tries\n"); 

       } 
       } 
       else 
       { 
        AccBalance=AccBalance-w_amount; 
        printf("Your new balance is $%d",AccBalance); 
       } 
       } 
       printf("\nDo you wish to go back to the main menu?\nEnter 1for YES, and any other number for NO\n"); 

       scanf("%d",&option1); 
       if (option1==1) 
       { 
       return option1; 
       } 
       else 
        printf("Thank you for using Generic ATM"); 
       return 0; 
       } 
       //end of option 3 

       //start of option 4 
       if (option==4) // if user wants to deposit dem moneys bbby 
       { 
        printf("Enter amount to deposit\n$"); 
        scanf("%d",&d_amount); 
       if(d_amount<=0) 
       { 
        printf("You cannot deposit this amount"); 
       for(i=4;i>0;i--) 
       { 
        printf("\nEnter appropriate amount,you have %d tries left\n",i); 
       { 
        printf("Enter amount to deposit\n$"); 
        scanf("%d",&d_amount); 
       if(d_amount<=0) 
       { 
        printf("You cannot deposit this amount"); 
       } 
       } 
       if (i==1) 
        printf("\nYou have exceeded your tries\n"); 
       } 
       } 
       else 
       { 
        AccBalance=AccBalance+d_amount; 
        printf("You have deposited $%d",d_amount); 
        printf("\nYour New balance is $%d",AccBalance); 
       } 
       printf("\nDo you wish to go back to the main menu?\nEnter 1 for YES, and any other number for NO\n"); 
        scanf("%d",&option1); 
       if (option1==1) 
       { 
       return option1; 
       } 
       else 
        printf("Thank you for using Generic ATM"); 
       return 0; 
       } 
       //end of option 4 

       //start of option 5 
       { 
       if (option == 5); 
        printf("\nThank you for using Generic ATM\n"); 
        return option; 
       } 
       //end of option 5 
return 0; 

getch(); 
} 
+0

ой мое слово .... извините, но его ошибка в форматировании, Printf, кажется, идет на новую строку , – Commongrate

+0

Измените свой вопрос и исправьте отступ. – Lundin

+1

Кроме того ... Не уверен, что это проблемы, но .... поскольку вы используете целые числа, никакие пароли не могут иметь в них 0. Пароль числового значения 00005 такой же, как 5. – jaybers

ответ

1
int main(){ 
    while true{ 
    if (checkPassword()) handleTransaction(); 
    }; 
}; 

Посмотрите, как легко, если вы не делаете все, что в основной?

+0

Я не совсем понимаю, я действительно новый, вы можете объяснить, что вы здесь сделали. Что такое checkpassword здесь. – Commongrate

+0

'if checkPassword() возвращает 1 then call handleTransaction()' where 'checkPassword' будет функцией, возвращающей 1, если пароль правильный, и' handleTransaction' - функция, которая будет делать все. – Kotshi

+0

Так что я просто делаю функцию, называемую check pass, тогда, если она возвращает 1 call handletrans, то есть остальная часть программы? ... – Commongrate

0

Чтобы задать пароль, прежде чем в главном меню вы должны спросить перед печатью (главное меню):

//rest of code 
printf("PLEASE ENTER YOUR PASSWORD: "); 

do{ 
     scanf("%d",&password); 
     if(password!=passworda&&password!=passwordb&&password!=passwordc) 
      printf("YOU HAVE ENTERED AN INVALID PASSWORD"); 
     else break; 
    }while(true); 

if(password==passworda) 
{ 
     printf("WELCOME Mr.%s\n\n",Name1); 
        //rest of the account details 
} 
else 
    if(password==passwordb) 
    { 
      printf("WELCOME Mr.%s\n\n",Name2); 
        //rest of the acc details 
    } 
    else 
      if(password==passwordc) 
      { 
       printf("WELCOME Mr.%s\n\n",Name3); 
        //rest of the acc details 
      } 

    printf("Welcome to generic ATM\n"); 
    printf("\n\nPlease choose an option..."); 
    printf("\n(1)View account details."); 
    printf("\n(2)Check balance."); 
    printf("\n(3)Withdraw amount."); 
    printf("\n(4)Deposit amount."); 
    printf("\n(5)Exit.\n"); 
    scanf("\n%d",&option); 

//rest of code 
Смежные вопросы