2016-03-28 9 views
-2

Ich habe eine kurze Frage zu diesem Code, den ich schreibe. Wie würde ich unter Bezug auf void RunBankMenu(int *choice) und void TransactionDecision(...) den Wert aus RunBankMenu(Choice) verwenden, um eine if/else oder switch-Anweisung für TransactionDecision zu setzen? Wenn ein Benutzer beispielsweise 1 auswählt, verwendet die TransactionsDecision-Funktion den Batch-Code, den ich auch für den Switch festgelegt habe. Wie würde ich den Zeiger auf eine andere Funktion übergeben, damit ich den Wert lesen kann?Verwenden eines Zeigers in einer anderen Funktion - Bank Programm

Danke!

#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h> 
#define MAXCREDIT -4500 

void RunBankMenu(int *choice); 
void Greeting(); 
void AccountBalance(double account, char letter); 
void TransactionDecision(int num, double *cPtr, double *sPtr, double *xPtr); 
void DepositMoney(double *accountPtr); 
void WithdrawMoney(double *accountPtr, char letter); 

int main() 
{ 
    double checkings = 430.00; 
    double savings = 812.00; 
    double credit = -2254.00; 

    int NumberChoice = 0; 
    Greeting(); 
    AccountBalance(checkings, savings, credit); 
    RunBankMenu(&NumberChoice); 

    printf("%d", NumberChoice); 
} 


void Greeting() 
{ 
    printf("Welcome to the Bank of COP 2220\n\nIt is a pleasure to manage" 
       " your checking, savings, and credit accounts\n"); 
} 

void AccountBalance(double account, char letter) 
{ 
    double checkings = 430.00; 
    double savings = 812.00; 
    double credit = -2254.00; 

    printf("-- You currently have $%.2f in your checking account\n",checkings); 
    printf("-- You currently have $%.2f in your savings account\n",savings); 
    printf("-- You currently have $%.2f credit balance\n", credit); 
} 



void RunBankMenu(int *choice) 
{ 
    do{ 
     printf("-----------------------------\n"); 
     printf("(1) to DEPOSIT to CHECKING\n"); 

     printf("(2) to WITHDRAW from CHECKING\n"); 

     printf("(3) to DEPOSIT to SAVINGS\n"); 

     printf("(4) to WITHDRAW from SAVINGS\n"); 

     printf("(5) to DEPOSIT to CREDIT\n"); 

     printf("(6) to TAKE an ADVANCE from CREDIT\n"); 

     printf("(7) to TRANSFER MONEY BETWEEN ACCOUNTS\n"); 

     printf("(8) for all ACCOUNT BALANCES\n"); 

     printf("\n(9) QUIT\n\n"); 

     printf("Select an option: "); 
     scanf("%d", &*choice); 
    } while (*choice <= 8); 


} 

void TransactionDecision(int num, double *cPtr, double *sPtr, double *xPtr) 
{ 
    int num1; 
} 

void DepositMoney(double *accountPtr) 
{ 

} 

void WithdrawMoney(double *accountPtr, char letter) 
{ 

} 
+1

Mit Punkt Floating ist eine schlechte Idee. Und format & Einzug dieses Chaos. Wir sind (wahrscheinlich) Menschen keine Compiler. – Olaf

+1

"Wie würde ich den Zeiger an eine andere Funktion übergeben, damit ich den Wert lesen kann". So wie Sie jede andere Variable übergeben würden - als Argument für die Funktion. Natürlich brauchen Sie den Zeiger nicht, sondern nur den Variablenwert selbst. Also in main: 'RunBankMenu (& NumberChoice); TransactionDecision (NumberChoice, ...); ' – kaylum

+0

... besser, kein Fließkomma für die Kontonummer zu verwenden. –

Antwort

0

Vielleicht Dieser Code wird Ihnen den Einstieg ... für Währungen oder andere genaue Berechnungen

#include <stdio.h> 

typedef struct accounts_S 
    { 
    double checkings; 
    double savings; 
    double credit; 
    } accounts_T; 

void DepositMoney(double *accountPtr) 
    { 

    } 

void WithdrawMoney(double *accountPtr) 
    { 

    } 

void TransferBetweenAccounts(accounts_T *accounts) 
    { 

    } 

void AccountBalance(
     accounts_T *I__accounts 
    ) 
    { 
    printf("-- You currently have $%.2f in your checking account\n", I__accounts->checkings); 
    printf("-- You currently have $%.2f in your savings account\n", I__accounts->savings); 
    printf("-- You currently have $%.2f credit balance\n", I__accounts->credit); 

    return; 
    } 

void TransactionDecision(int NumberChoice, accounts_T *accounts) 
    { 
    switch(NumberChoice) 
     { 
     case 1: 
     DepositMoney(&accounts->checkings); 
     break; 

     case 2: 
     WithdrawMoney(&accounts->checkings); 
     break; 

     case 3: 
     DepositMoney(&accounts->savings); 
     break; 

     case 4: 
     WithdrawMoney(&accounts->savings); 
     break; 

     case 5: 
     DepositMoney(&accounts->credit); 
     break; 

     case 6: 
     WithdrawMoney(&accounts->credit); 
     break; 

     case 7: 
     TransferBetweenAccounts(accounts); 
     break; 

     case 8: 
     AccountBalance(accounts); 
     break; 
     } 

    return; 
    } 

void Greeting() 
    { 
    printf("Welcome to the Bank of COP 2220\n" 
      "\n" 
      "It is a pleasure to manage your checking, savings, and credit accounts\n" 
     ); 

    return; 
    } 

void RunBankMenu(
     int *choice 
    ) 
    { 
    printf("-----------------------------\n"); 
    printf("(1) to DEPOSIT to CHECKING\n"); 
    printf("(2) to WITHDRAW from CHECKING\n"); 
    printf("(3) to DEPOSIT to SAVINGS\n"); 
    printf("(4) to WITHDRAW from SAVINGS\n"); 
    printf("(5) to DEPOSIT to CREDIT\n"); 
    printf("(6) to TAKE an ADVANCE from CREDIT\n"); 
    printf("(7) to TRANSFER MONEY BETWEEN ACCOUNTS\n"); 
    printf("(8) for all ACCOUNT BALANCES\n"); 
    printf("\n(9) QUIT\n\n"); 

    do { 
     printf("Select an option: "); 
     scanf("%d", choice); 
     } while((0 == *choice) && (*choice > 9)); 

    return; 
    } 

int main() 
    { 
    int  rCode   = 0; 
    int  NumberChoice; 
    accounts_T accounts  = 
     { 
     .checkings = 430.00, 
     .savings = 812.00, 
     .credit = -2254.00 
     }; 

    Greeting(); 
    AccountBalance(&accounts); 

    do { 
     RunBankMenu(&NumberChoice); 
     TransactionDecision(NumberChoice, &accounts); 
     } while(9 != NumberChoice); 

    return(rCode); 
    } 
Verwandte Themen