传递此函数的值

时间:2017-10-14 05:35:11

标签: c visual-studio math parameter-passing pass-by-reference

#define _CRT_SECURE_NO_WARNINGS
/*

目的:该程序允许用户下注马匹 在争夺上述赌注的竞赛中。我试图运行configureBalance函数,然后向余额添加资金。我收到了异常读取访问冲突 * /

#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH myFlush()

//Prototyping
void getChoice(char *userChoice);       // main menu choice
void displayMenu();             // visual menu
void myFlush();                     // flush
void configureBalance(int *balance, int *wallet, int *withdraw, int *deposit);  // this function is for editing account credentials
void currentBalance(int *balance);          // displays the account balance
void coolRaceVisual();               // cool looking visual
                                 //Structs


main() {
int balance = 0, wallet = 0, withdraw = 0, deposit = 0;
char choice = ' ';


do {
    getChoice(&choice);

    switch (choice) {

    case 'A':
        configureBalance(balance, wallet, withdraw, deposit);
        PAUSE;
        break;

    case 'B':
        coolRaceVisual();
        PAUSE;
        break;
    case 'Q':
        CLS;
        printf("[][][][][][][][][][][]\n");
        printf("[]     Goodbye !    []\n");
        printf("[][][][][][][][][][][]\n");

        break;

    default:
        printf("[][][][][][][][][][][][][][][][][][][][][][][]\n");//
        printf("[]   Invalid Selection! Please try again    []\n");//   This 
prompt shows up when the user
        printf("[][][][][][][][][][][][][][][][][][][][][][][]\n");//     
inputs something incorrectly
        PAUSE;
        CLS;
        break;
        return;
    }

} while (choice != 'Q');
PAUSE;
}//end main

void getChoice(char *userChoice) {
displayMenu();
scanf("%c", userChoice); FLUSH;
*userChoice = toupper(*userChoice);
}//end getChoice

void displayMenu() {
CLS;

printf("                   Horse Derby Ticket Office               \n");
printf("                                                           \n");
printf("      A) Configure Balances.                               \n");
printf("                                                           \n");
printf("      B) Watch the Race.                                   \n");
printf("                                                           \n");
printf("      C) View Race Records.                                \n");
printf("                                                           \n");
printf("      D) Save and Quit.                                    \n");
printf("                                                           \n");
printf("      Q) Quit.                                             \n");
printf("                                                           \n");
}// end displayMenu

void myFlush() {
while (getchar() != '\n');
}//end myFlush

void configureBalance(int *balance, int *wallet, int *withdraw, int *deposit) {
CLS;
char configureMenuChoice = ' ';

printf("What service would you like? (Not FDIC Insured)\n\n");

printf("A) Add funds to your account balance.\n");
printf("B) Withdraw funds to your wallet.\n");
printf("C) Check Account Balance.\n");
printf("\n\n");
scanf("%c", &configureMenuChoice);
configureMenuChoice = toupper(configureMenuChoice);

选择配置余额的选项

if (configureMenuChoice == 'A') {
    CLS;
    printf("How much would you like to add to your account balance? \n");   

这直接增加了余额

    scanf("%i", &deposit);
    *balance = *balance + *deposit;
}
if (configureMenuChoice == 'C') {
    CLS;
    currentBalance(*balance);                       // displays current balance, made a functino so it can be used at will
}
}//end conFigureBalance

void currentBalance(int *balance) {

printf("Your current balance is: %i\n", &balance);

}//end checkBalance

1 个答案:

答案 0 :(得分:1)

改变这个:

scanf("%i", &deposit);

到此:

scanf("%i", deposit);

因为deposit在该上下文中属于int*类型(函数configureBalance的主体)。

它的逻辑与此相同:scanf("%c", userChoice);,所以我想知道你是如何错过它的。