2017-10-14 142 views
1
#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 
+0

@Deonte Threatt应该有scanf(“%i”,存款);而不是scanf(“%i”,&deposit);在函数中。 –

回答

1

更改此:

scanf("%i", &deposit); 

这样:

scanf("%i", deposit); 

因为deposit是在这种背景下(函数configureBalance的主体)int*类型。

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

+0

我改变了这一点,但它的价值,当我添加存款的余额。 –