[ create a new paste ] login | about

Link: http://codepad.org/T5z5vTZH    [ raw code | output | fork ]

C++, pasted on Dec 6:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "wait.h"  /* custom header */

/********************************
* TODO:                         *
* Save wins and losses          *
* Load wins and losses          *
* Add more featers to wait.h    *
* Improve variable names        *
* Explain the for loop          *
* Add more comments             *
* ???                           *
* Profit!                       *
********************************/

using namespace std;

int main()
{
  srand(time(NULL)); /* only call this once! */

  game:

  clear;

  int score;
  string restart;

  ifstream money ("score.txt");
  if (!money.is_open()) {
    string create_score_file;
    cout<<"Score file could not be loaded.  Would you like me to create one for you? (yes/no): ";
    cin>> create_score_file;
    if (create_score_file == "no") {
      return 1;
    }
    else if (create_score_file == "yes") {
      score = 100;
      ofstream money_write("score.txt");
      money_write<< score;
      money_write.close();
      cout<<"Score file created.  Would you like to restart the game? (yes/no): ";
      cin>> restart;
      if (restart == "no") {
        return 1;
      }
      else if (restart == "yes") {
        goto game;
      }
      else {
        cout<<"Invalid Option "<< restart << endl;
        return 1;
      }
    }
    else {
      cout<<"Invalid Option "<< restart << endl;
      return 1;
    }
  }
  else {
    score = money;
    money.close()
  }

  money<< score;
  money.close();

  ofstream money ("score.txt");

  int landed = rand()%20+1; /* for use in if statements.  > 10 is red, < 10 is black */

  string rob; /* red or black */

  int amount_to_bet;  /* declaring start of variable without giving it a value */


  /* some output, the input */
  cout<<"What color would you like to bet on? (red/black): ";
  cin>> rob;
  if (rob != "red" && rob != "black") {  /* if user inputs anything invalid */ 
    cout<<"ERROR, Invalid Option\n";
    return 1;
  }

  string pocket_yn;

  /* more output and input */
  cout<<"Would you like to bet on a specific pocket of "<< rob <<"? (yes/no): ";
  cin>> pocket_yn;
  if (pocket_yn != "yes" && pocket_yn != "no") {
    cout<<"ERROR, Invalid Option\n";
    return 1;
  }

  int pocket_num;

  /* if user wants to bet on a pocket */
  if (pocket_yn == "yes") {
    cout<<"What pocket of "<< rob <<" would you like to bet on? (1-36): ";
    cin>> pocket_num;
    if (pocket_num > 36) {
      cout<<"There are only 36 pockets on a roulette wheel, not "<< pocket_num <<"!\n";
      return 0;
    }

    cout<<"How much would you like to bet on "<< pocket_num <<", "<< rob <<"? (1-"<< money <<"): ";
    cin>> amount_to_bet;
    if (amount_to_bet > money) {
      cout<<"You have bet more money then you have!\n";
      return 0;
    }

    cout<<"You have bet "<< amount_to_bet <<" on "<< pocket_num <<", "<< rob <<"\nRolling...\n";
  }
  else { /* if they dont */
    cout<<"How much would you like to bet on "<< rob <<"? (1-"<< money <<"): ";
    cin>> amount_to_bet;
    if (amount_to_bet > money) {
      cout<<"You have bet more money then you have!\n";
      return 0;
    }

    cout<<"You have bet "<< amount_to_bet <<" on "<< rob <<"\nRolling...\n";
  }


  /* this part was fun to write.  lots of if statements! */
  if (landed > 10) {                                                 // 10 and below is red
    int rednum = rand()%36+1;                                        // the red pocket (1-36)
    for (int foo = 0; foo <= rednum; foo++) {                        // creates foo, sets it value to 0.  while foo equal to or less then the red number (pocket)
      if (foo == rednum) {                                           // if foo is the red pocket
        int waittime = rand()%4+1;                                   // creates waittime variable as a rand number between 1 and 4
        wait_s(waittime);                                            // 
        cout<<"Ball landed on "<< rednum <<" red.\n";                //
        if (rob == "black") {                                        // if user input black, not red.
          cout<<"You have lost\n";                                   //
          break;                                                     // 
        }                                                            // 
        else if (rob == "red") {                                     //
          if (pocket_yn == "no") {                                   // if the user did not bet on a specific pocket
            cout<<"You have won $"<< amount_to_bet * 2 << endl;      // user wins twice what they bet
            break;                                                   // break out of the loop
          }                                                          //
          else if (pocket_yn == "yes") {                             // if they did want to bet on a specific pocket
            if (pocket_num == foo) {                                 // if the pocket number is the same as foo
              cout<<"You have won $"<< amount_to_bet * 5 << endl;    // user wins 5 times what they bet
            }                                                        //
            else {                                                   // if its not foo
              cout<<"You have lost\n";                               //
              break;                                                 // breaks out of loop
            }
          }
        }
      }
    }
  }
  else if (landed < 10) {
    int blacknum = rand()%36+1;
    for (int foo = 0; foo <= blacknum; foo++) {
      if (foo == blacknum) {
        int waittime = rand()%4+1;
        wait_s(waittime);
        cout<<"Ball landed on "<< blacknum <<" black.\n";
        if (rob != "black") {
          cout<<"You have lost.\n";
          break;
        }
        else if (rob == "black") {
          if (pocket_yn == "no") {
            cout<<"You have won $"<< amount_to_bet * 2 << endl;
            break;
          }
          else if (pocket_yn == "yes") {
            if (pocket_num == foo) {
              cout<<"You have won $"<< amount_to_bet * 5 << endl;
            }
            else {
              cout<<"You have lost\n";
              break;
            }
          }
        }
      }
    }
  }

  string play_again;
  cout<<"Would you like to play again? (yes/no): ";
  cin>> play_again;

  if (play_again == "no") {
    return 0;
  }
  else if (play_again == "yes") {
    goto game;
  }
  else {
    cout<<"ERROR, Invalid Option\n";
    return 1;
  }
}


Output:
1
2
3
In function 'int main()':
Line 27: error: 'clear' was not declared in this scope
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: