just wanted some feedback from some seasoned programmer i have only been at this for about a week i am teaching myself i think im getting a good understanding of the basics please let me know if there is any improvement that can be made
//Declare your secondary functions before main
//and define them after main.
int roll();
int bet(int b, int w);
int winbet(int wag);
int main()
{
int dice1 = 1;
int dice2 = 1;
int point;
int newRoll = 0;
char keepRolling;
int bank = 100;
int wager = 0;
srand(time(0));
int Wins = 0;
int losses = 0;
begin:
//Splitting this up makes it easier to read
cout << "Wins: " << Wins << endl;
cout << "Losses: " << losses << endl;
if (bank <= 0)
{
cout << "Game over.";
exit(0);
}
//Style point that some follow is always having an else for every if
// even if it's an empty statement
else
;
cout << "bank= (" << bank << ")" << endl;
cout << "Please place a bet between 1 and " << bank << endl;
cin >> wager;
if (wager > bank)
{
cout << "You dont have enough money to do this. please enter a different amount." << endl;
goto begin;
}
//Same as above
else
;
bank = bet(bank, wager);
cout << bank << endl;
point = roll();
if (point == 7 || point == 11)
{
cout << "Winner! with : " << point << endl;
bank = bank + (wager * 2);
Wins++;
goto begin;
}
//You can use if elseif here. That way once something is true
//The rest of the if's get skipped
elseif (point == 2 || point == 3 || point == 12)
{
cout << "you crapped out with " << point << " better luck next time," << endl;
losses++;
goto begin;
}
else
{
if (point != 2 || point != 3 || point != 12)
cout << "Your point is " << point << "\n Press enter to keep rolling" << endl;
else
;
cin.ignore();
cin.get();
goto Roll;
}
rolling:
cout << "would you like to keep playint? \n y/n" << endl;
cin >> keepRolling;
/*if (keepRolling == 'y')
{
}
else
{
//if(keepRolling=='n')
exit(0);
}*/
//Try this instead != is 'not equivalent to' also notice that for a single statement
// {} are not needed. Cleans things up a lot.
if (keepRolling != 'y')
exit(0);
else
;
Roll:
roll();
newRoll = roll();
cout << "The new roll was " << newRoll << endl;
if (newRoll == point)
{
cout << "We have a winner! ";
Wins++;
bank = bank + (wager * 2);
goto begin;
}
//else-if again
elseif (newRoll == 7)
{
cout << "Pass the dice please." << endl;
losses++;
goto begin;
}
else //single statement no {} needed
goto rolling;
system("PAUSE");
return 0;
}
int roll()
{
//Naming convention. Start with Lower-Case "dice1, dice2"
int Dice1 = 1;
int Dice2 = 1;
Dice1 = (rand() % 6) + 1;
Dice2 = (rand() % 6) + 1;
//Return sum, no need for a var
return Dice1 + Dice2;
}
int bet(int b, int w)
{
// this works, no need for a var
return b - w;
}
int winbet(int wag)
{
//Same here
return wag * 2;
}
What to fix:
1) gotos. There should be exact number of goto in your program: 0.
2) Formatting. thomaselder84 fixed it for you.
3) Overly long functions. If function does not fit on screen, it is too long.
4) Declare variables at the point of first use.
5) Use standard C++ facilities.