Little error...
I recently got a book "Beggining Mac OS X Programming". Yay!
Now I'm learning C.
Unfortunately, there is not enough exercises inside the book, so one of my friend lend me a C++ book (he's a C++ programmer, and he's way better than me...). I looked at the beggining exercises and I found one without classes, so it would work in C. I coded and builded the program with Xcode 2.2. No errors during the build.
So, I run the program (it's a command-line tool) in Terminal. Some text appears, and I'm asked to input something. So far, everything works well. But when I enter something and press Return, it lags and then...
BANG! It says "Bus Error".
So, what is that "bus error", and how can I solve it?
Thanks!
Now I'm learning C.
Unfortunately, there is not enough exercises inside the book, so one of my friend lend me a C++ book (he's a C++ programmer, and he's way better than me...). I looked at the beggining exercises and I found one without classes, so it would work in C. I coded and builded the program with Xcode 2.2. No errors during the build.
So, I run the program (it's a command-line tool) in Terminal. Some text appears, and I'm asked to input something. So far, everything works well. But when I enter something and press Return, it lags and then...
BANG! It says "Bus Error".
So, what is that "bus error", and how can I solve it?
Thanks!
A bus error generally means you tried to access an invalid memory location.
I'm sure if you paste in your code within code tags (# icon in the message composer) someone would prompty identify what you did wrong. I suspect that you read input into an unallocated buffer.
Welcome to iDevGames
-Jon
I'm sure if you paste in your code within code tags (# icon in the message composer) someone would prompty identify what you did wrong. I suspect that you read input into an unallocated buffer.
Welcome to iDevGames
-Jon
Well, it's a bit long (about 180 lines of code)...
According to the debugger, the error happened at line 30 (which is a function call... the function itself starts at line 81).
Anyway, thanks a lot!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void ShowIntroScreen (void);
void ShowInformation (unsigned long Money);
short GetBet (void);
short DoDiceThrow (void);
unsigned short DoMoneyCalc (short Dice, short Bet, short BetMoney);
unsigned long GetAmount (void);
int main (int argc, const char * argv[]) {
unsigned long MoneyEarned;
unsigned long Money;
short DiceValue;
short Bet;
short BetMoney;
// Show intro and setup game
ShowIntroScreen();
Money = 1000;
// Play while player has money
// Keep 100 dollars for the cab home
do {
ShowInformation (Money);
// Get bet information
Bet = GetBet();
BetMoney = GetAmount();
DiceValue = DoDiceThrow();
MoneyEarned = DoMoneyCalc(DiceValue, Bet, BetMoney);
Money -= BetMoney;
// Show the number
if (MoneyEarned == 0) {
printf("You lost. Number was %d\n\n", DiceValue);
}
else {
printf("You won %d", MoneyEarned - BetMoney);
printf(" dollars. Number was: %d\n\n", DiceValue);
Money += MoneyEarned;
}
}
while (Money > 100);
printf("Game Over. Keep %d $ for the ride home\n", Money);
return 0;
}
void ShowIntroScreen(void) {
printf("\tWelcome to Craps 1.0\n\n");
printf("Here are the rules:\n\n");
printf("You have 1000 dollars to start gambling.\n\n");
printf("You can do three different bets. You can bet on ");
printf("numbers 2 and 12 which will give ");
printf("you a win ration of ");
printf("5 to 1 if you win. You can also bet on the numbers 4 ");
printf("and 10 ");
printf("which will give you a ratio of 2.5 to 1. ");
printf("The last kind of bet you can do is on the numbers 6 ");
printf("and 8 which will give you a win ratio of 1.5 to 1.\n\n");
printf("The minimum amount to bet is 10 dollars and the ");
printf("maximum is 100 dollars.\n\n");
printf("Have fun playing.\n\n\n");
}
void ShowInformation(unsigned long Money) {
printf("You have: %d dollars.\n", Money);
}
short GetBet(void) {
unsigned short BetType;
// Get bet
printf("Enter the type of bet (1 = '6/8' 2 = '4/10' 3 = '2/12'): ");
scanf("%d", BetType);
// If bet invalid bet on 6/8
if ((BetType == 1) || (BetType == 2) || (BetType == 3)) {
return BetType;
}
else {
return 1;
}
}
short DoDiceThrow(void) {
short DiceValue;
// Get dice value
srand(time(NULL));
DiceValue = (rand() % 11) + 2;
// If 4/10 get another number, this will make this
// event more improbable so pay ratio is bigger
if ((DiceValue == 4) || (DiceValue == 10)) {
srand(time(NULL));
DiceValue = (rand() % 12) + 1;
}
// If 2/12 get another number, this will make this
// event more improbable so pay ratio is bigger
if ((DiceValue == 2) || (DiceValue == 12)) {
srand(time(NULL));
DiceValue = (rand() % 12) + 1;
if ((DiceValue == 2) || (DiceValue == 12)) {
DiceValue = (rand() % 12) + 1;
}
}
return DiceValue;
}
unsigned short DoMoneyCalc(short Dice, short Bet, short BetMoney) {
unsigned long MoneyEarned = 0;
// See which type of the bet the player made
switch(Bet) {
// 6/8 - pay amount of 1.5:1
case 1:
if ((Dice == 6) || (Dice == 8)) {
MoneyEarned = BetMoney * 1.5;
}
break; break;
// 4/10 - pay amount of 2.5:1
case 2:
if ((Dice == 10) || (Dice == 4)) {
MoneyEarned = BetMoney * 2.5;
}
break; break;
// 2/12 - pay amount 5:1
case 3:
if ((Dice == 2) || (Dice == 12)) {
MoneyEarned = BetMoney * 5;
}
break;
default:
MoneyEarned = 0;
break;
}
return MoneyEarned;
}
unsigned long GetAmount(void) {
unsigned short BetAmount;
// Get bet amount
printf("Enter amount to bet (min 10 - max 100): ");
scanf("%d", BetAmount);
// If bet out of range fix it
if (BetAmount < 10) {
BetAmount = 10;
}
if(BetAmount > 100) {
BetAmount = 100;
}
return BetAmount;
}According to the debugger, the error happened at line 30 (which is a function call... the function itself starts at line 81).
Anyway, thanks a lot!
This is simple, the scanf statement on line 86 should be using a pointer of the variable "BetType". Just put a & infront so that it now says
and it works fine. Same with line 164.
Code:
scanf("%d", &BetType);
