Hello,
I'm just begining the process of learning C++ via self study. I have no previous programming experience of any kind.
I downloaded a compiler on Saturday and have been working my way through a few tutorials (including the ones here) and the book "Teach Yourself C++ in one hour/day" (I have no illusions about the timeline, don't worry). I have ordered Accelerated C++ and Thinking in C++ and should have them in a day or so.
I have just read up on Pointers and References and am starting to read up on Classes.
What I wanted to do was write a short program that had a couple different "chunks" without using any of the newer concepts I'm currently learning. I next plan to rewrite it using Pointers and Classes.
This is the first program I have written without referring to any documentation. If anyone feels like taking a look at it and offering criticism I would welcome the effort. I am somewhat concerned about my commenting (making them useful but not pointless). I am majorly concerned about my use of variables - specifically the number of them. I think I have probably used 2x as many variables as I really need. Also, I think most of the function gobroke() should be in main().
Again, my goal was to write a program that works (this seems to) that I can rewrite using classes and pointers once I have somewhat of a handle on them.
If this is too basic for such a forum I understand and will hopefully be able to offer something more advanced in the coming days. Any help is appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
// short game allowing user to gamble on the value of a six-sided die
#include <cstdlib>
#include <iostream>
using namespace std;
int gobroke(int); // function that will "play" the game
int wager(int); // function to accept a valid wager
int guess(); // function to accept guess of dice value
int dice(); // function to simulate rolling one die
int main()
{srand(time(0)); // needed for later rand() call
int lifesavings = 500; // starting pool of money to be gambled
while (lifesavings > 0) // allows user to continue playing while he still...
{(lifesavings = gobroke(lifesavings));} // ...has money left
cout << "You're busted!" << endl; // when the money runs out
char response; // there are probably better ways than...
cin >> response; //... this to keep my window open
return 0;}
int gobroke( int pocket) //playing the game
{int bet; // amount to be gambled, returned from wager()
int pick; // from guess(), to be compared with die roll
int roll; // value of single die, returned by dice()
(bet = wager(pocket));
cout << endl << "You have wagered " << bet << "$." << endl;
cout << endl;
(pick = guess());
cout << endl << "Throwing dice..." << endl;
(roll = dice());
cout << "The dice landed on a " << roll << "!!!" << endl;
if (roll == pick) // compares user guess with die value
{cout << "You win! You just won " << bet << "$" << endl;
pocket += bet;} // winnings added to user's money
else
{cout << "You lose! You just lost " << bet << "$" << endl << endl << endl;
pocket -= bet;} // losses subtracted from user's money
return pocket;}
int wager(int fatchance)
{int gamble = 0;
cout << endl << "You currently have " << fatchance << " dollars." << endl;
cout << "How many dollars would you like to wager?" << endl;
cin >> gamble;
while (gamble > fatchance) // ensures user has the money to wager
{cout << "Sorry, you cannot bet more than you have!" << endl;
cout << "Make a valid bet:" << endl;
cin >> gamble;}
return gamble;}
int guess()
{ int prayformojo;
cout << "Pick a number between 1 and 6:" << endl << endl;
cin >> prayformojo;
if (prayformojo>6 || prayformojo < 1)
{cout << endl << "I said between 1 and 6." << endl << endl;
prayformojo=guess();}
else
return prayformojo;}
int dice() // emulate a six-sided die
{int x;
x= rand();
x%=6; // random number between 0-5 (is this the best way to do this?)
x+=1; // changes range to 1-6 to represent die
return x;}
|