【问题标题】:Passing the value from this function从这个函数传递值
【发布时间】:2018-03-26 06:13:44
【问题描述】:
#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

【问题讨论】:

  • @Deonte 威胁 应该有 scanf("%i", deposit);而不是 scanf("%i", &deposit);在函数中。

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


【解决方案1】:

改变这个:

scanf("%i", &deposit);

到这里:

scanf("%i", deposit);

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

和这里的逻辑相同:scanf("%c", userChoice);,所以我想知道你是怎么错过的。

【讨论】:

  • 我改变了它,但是当我将存款添加到余额中时它在值中。
猜你喜欢
  • 2016-10-06
  • 1970-01-01
  • 2020-09-26
  • 2021-08-14
  • 1970-01-01
  • 2018-07-19
  • 2021-02-16
  • 1970-01-01
相关资源
最近更新 更多