Advertisement
Demonburger

bs2.0

Apr 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.69 KB | None | 0 0
  1. //chapter 1
  2. //Make it Fancy
  3.  
  4. //Included Library Files
  5. //needed for cin, cout, endl
  6. #include <iostream>
  7. //needed for system("pause");
  8. #include <stdlib.h>
  9. //for numeric_limits
  10. #include <limits>
  11. //needed for the random number generator
  12. #include <time.h>
  13. //needed for getchar
  14. #include <stdio.h>
  15.  
  16. //using Declarations
  17. using std::cout;
  18. using std::endl;
  19. using std::cin;
  20. using std::numeric_limits;
  21. using std::streamsize;
  22.  
  23. /*this is like declaring a variable, except we're declaring a void function called GameOver*/
  24. void GameOver();
  25. void ExpensiveCalculator();
  26. void GameStats();
  27. void NumberGame();
  28. char yncheck(char main);
  29.  
  30.  
  31.  
  32. //Starting the Main Program
  33. int main()
  34. {
  35.     char menu = 'y';
  36.     int choice;
  37.     bool inputfail;
  38.  
  39.     do
  40.     {
  41.         system("Color 0F");
  42.         cout << "Your Options are: ";
  43.         cout << "\n1 - Expensive Calculator ";
  44.         cout << "\n2 - Game Stats";
  45.         cout << "\n3 - Number Guessing Game";
  46.         cout << "\n4 - Game Over";
  47.         cout << "\nPlease select an option and press enter: ";
  48.         cin >> choice;
  49.         inputfail = cin.fail();
  50.  
  51.         cin.clear();
  52.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  53.  
  54.         //Checking to make sure input is a menu option
  55.         while (choice > 4 || choice <= 0)
  56.         {
  57.             cout << "Please Try Again .\nYour Options are: ";
  58.             cout << "\n1 - Expensive Calculator ";
  59.             cout << "\n2 - Game Stats";
  60.             cout << "\n3 - Number Guessing Game";
  61.             cout << "\n4 - Game Over";
  62.             cout << "\nPlease select an option and press enter: ";
  63.             cin >> choice;
  64.             //checking cin to make sure it is in fact and integer,loops till integer is input, clear out extra inputs
  65.             inputfail = cin.fail();
  66.             cin.clear();
  67.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  68.         }
  69.  
  70.         if (choice == 1)
  71.         {
  72.         /*This calls the void function ExpensiveCalculator(), the program jumps to void ExpensiveCalculator() and executes the entire function.*/
  73.         ExpensiveCalculator();
  74.         cout << endl << endl;
  75.         system("pause");
  76.         } //return a value to show successful run of main program
  77.  
  78.         else if (choice == 2)
  79.         {
  80.         /*This calls the void function GameStats, the program jumps to void GameStats() and executes the entire function.*/
  81.         GameStats();
  82.         system("pause");
  83.         cout << endl << endl;
  84.         }
  85.         else if (choice == 3)
  86.         {
  87.         NumberGame();
  88.         system("pause");
  89.         cout << endl << endl;
  90.         }
  91.         else if (choice == 4)
  92.         {
  93.         /*This calls the void function GameOver, the program jumps to void GameOver() and executes the entire function.*/
  94.         GameOver();
  95.         cout << endl << endl;
  96.         system("pause");
  97.         }
  98.         else
  99.         {
  100.            cout << "Invalid Input\n\n";
  101.         }
  102.  
  103.         cout << "Return to main Menu (y/n):";
  104.         menu = getchar();
  105.         menu = yncheck(menu);
  106.  
  107.         system("CLS");
  108.     } while (menu =='y');
  109.     //return a value to show successful run of main program
  110.     return 0;
  111. }
  112.  
  113. /*This is the void function itself, this is what runs when GameOver() is called */
  114. void GameOver()
  115. {
  116.     system("Color 1B");
  117.     system("CLS");
  118.     cout << "\t\t\tOk, Bye!\n";
  119.     cout << "#############################################################" << endl;
  120.     cout << "#                    _                                      #" << endl;
  121.     cout << "#                  -=\\`\\                                    #" << endl;
  122.     cout << "#              |\\ ____\\_\\__                                 #" << endl;
  123.     cout << "#            -=\\c`\"\"\"\"\"\"\" \"`)                               #" << endl;
  124.     cout << "#               `~~~~~/ /~~`\                                #" << endl;
  125.     cout << "#                 -==/ /                                    #" << endl;
  126.     cout << "#                   '-'                                     #" << endl;
  127.     cout << "#                  _  _                                     #" << endl;
  128.     cout << "#                 ( `   )_                                  #" << endl;
  129.     cout << "#                (    )    `)                               #" << endl;
  130.     cout << "#              (_   (_ .  _) _)                             #" << endl;
  131.     cout << "#                                             _             #" << endl;
  132.     cout << "#                                            (  )           #" << endl;
  133.     cout << "#             _ .                         ( `  ) . )        #" << endl;
  134.     cout << "#           (  _ )_                      (_, _(  ,_)_)      #" << endl;
  135.     cout << "#         (_  _(_ ,)                                        #" << endl;
  136.     cout << "#############################################################" << endl;
  137.     cout << "\t\t\tSee you later!!\n" << endl;
  138.     cout << "Please report Errors to Will. \n\n";
  139.     cout << "Game Over!" << endl;
  140. }
  141.  
  142. /*This is the void function itself, this is what runs when ExpensiveCalculator() is called */
  143. void ExpensiveCalculator()
  144. {
  145.     system("CLS");
  146.     int first, second;
  147.     bool inputfail;
  148.     cout << "Please Enter your First Number and press enter: ";
  149.     cin >> first;
  150.     inputfail = cin.fail();
  151.     cin.clear();
  152.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  153.  
  154.     //Checking to make sure input is a menu option
  155.     while (inputfail == true)
  156.     {
  157.             cout << "Please re-enter your First Number and press enter: ";
  158.             cin >> first;
  159.             inputfail = cin.fail();
  160.             cin.clear();
  161.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  162.     }
  163.     cout << "Please Enter your Second Number and press enter: ";
  164.     cin >> second;
  165.     inputfail = cin.fail();
  166.     cin.clear();
  167.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  168.  
  169.     //Checking to make sure input is a menu option
  170.     while (inputfail == true)
  171.     {
  172.             cout << "Please re-enter your second Number and press enter: ";
  173.             cin >> second;
  174.             inputfail = cin.fail();
  175.             cin.clear();
  176.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  177.     }
  178.     cout << first << " + " << second << " = " << first + second << endl;
  179.     cout << first << " - " << second << " = " << first - second << endl;
  180.     cout << first << " * " << second << " = " << first * second << endl;
  181.     cout << first << " / " << second << " = " << first / second << endl;
  182.     //not needed previous line does the same thing
  183.     //cout << "7.0 / 3.0 = " << 7.0 / 3.0 << endl;
  184.     cout << first << " % " << second << " = " << first % second << endl;
  185.     cout << first << " + " << second << " * 5 = " << first + second * 5 << endl;
  186.     cout << first << "(" << first << " + " << second << ") * 5 = " << (first + second) * 5 << endl;
  187. }
  188.  
  189. void GameStats()
  190. {
  191.     system("CLS");
  192.     int score;
  193.     double distance;
  194.     char playAgain;
  195.     bool shieldsUp, inputfail;
  196.  
  197.     short lives, aliensKilled;
  198.  
  199.     score = 0;
  200.     distance = 1200.76;
  201.     playAgain = 'y';
  202.     shieldsUp = true;
  203.     lives = 3;
  204.     aliensKilled=10;
  205.  
  206.     double engineTemp = 6572.89;
  207.  
  208.     cout << "\nScore: " << score << endl;
  209.     cout <<"Distance: " << distance <<endl;
  210.     cout <<"Play Again: " << playAgain << endl;
  211.     //Skipping shields up
  212.     cout << "Lives :" << lives << endl;
  213.     cout << "Aliens Killed: " << aliensKilled << endl;
  214.     cout << "Engine Temp: " << engineTemp << endl;
  215.  
  216.     int fuel;
  217.     cout << "\nHow Much Fuel?";
  218.     cin >> fuel;
  219.     inputfail = cin.fail();
  220.     cin.clear();
  221.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  222.  
  223.     //Checking to make sure input is a menu option
  224.     while (inputfail == true)
  225.     {
  226.             cout << "Please re-enter your fuel and press enter: ";
  227.             cin >> fuel;
  228.             inputfail = cin.fail();
  229.             cin.clear();
  230.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  231.     }
  232.  
  233.     cout << "Fuel: " << fuel << endl;
  234.  
  235.     typedef unsigned short int ushort;
  236.     ushort bonus = 10;
  237.     cout << "\nBonus: " << bonus <<
  238.      endl;
  239. }
  240. void NumberGame()
  241. {
  242.     //Initialize and assign values to variables, and get the number generator working.
  243.     srand(time(0));
  244.     int secretnumber;
  245.     int tries = 0;
  246.     int guess;
  247.     char again = 'y';
  248.     bool inputfail;
  249.  
  250.     system("CLS");
  251.  
  252.     //Change Color Again
  253.     system("Color 1F");
  254.  
  255.     //Welcome Screen for number game
  256.     cout << "\n\nWelcome to Guess The Number \n";
  257.     cout << "Possible answer range is from 1 to 100.\n\n\n";
  258.  
  259.     //Main game loop, will loop while (again == y) allowing for multiple plays
  260.     while (again == 'y')
  261.     {
  262.         //Set secretnumber to a random number between 1 and 100
  263.         secretnumber = rand() % 100 + 1;
  264.  
  265.         //Loop to allow for multiple guesses
  266.         do
  267.         {
  268.             //Get input, verify input is an integer.
  269.             do
  270.             {
  271.                 cout << "Enter your guess: ";
  272.                 cin >> guess;
  273.                 //checking cin to make sure it is in fact and integer,loops till integer is input
  274.                 cin.clear();
  275.                 cin.ignore(numeric_limits<streamsize>::max(), '\n');
  276.  
  277.                 //Checking to make sure input is between 1 and 100
  278.                 while (guess > 100 || guess <= 0)
  279.                 {
  280.                     cout << "Guess must be a number from 1 to 100. Please enter your guess: ";
  281.                     cin >> guess;
  282.                     //checking cin to make sure it is in fact and integer,loops till integer is input, clear out extra inputs
  283.                     inputfail = cin.fail();
  284.                     cin.clear();
  285.                     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  286.                 }
  287.  
  288.             } while (inputfail == true);
  289.  
  290.             //increment try counter
  291.             ++tries;
  292.  
  293.             //Too high notification
  294.             if (guess > secretnumber)
  295.             {
  296.                 cout << "Too High! \n\n";
  297.             }
  298.  
  299.             //Too low notification
  300.             else if (guess < secretnumber)
  301.             {
  302.                 cout << "Too Low! \n\n";
  303.             }
  304.  
  305.             //Correct answer notification as well as output for number of tries
  306.             else
  307.             {
  308.                 cout << "\nThat's It! You got it in " << tries << " guesses!\n";
  309.             }
  310.  
  311.         } while (guess != secretnumber);
  312.  
  313.         //Ask user if they would like to play again this needs improved in the same way guess input needs improved
  314.         cout << "Play Number Game again (y/n)?: ";
  315.         again = getchar();
  316.         again = yncheck(again);
  317.         //Reset Try counter for next game
  318.         tries = 0;
  319.     }
  320.     cout << "Thanks for Playing My Number Game.\n\n";
  321. }
  322.  
  323. char yncheck(char mainc)
  324. {
  325.     bool inputcheck;
  326.     inputcheck = cin.fail();
  327.     do
  328.     {
  329.         cin.clear();
  330.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  331.  
  332.         while (mainc != 'y' && mainc != 'n')
  333.         {
  334.             cout << "Please enter y or n:";
  335.             cin >> mainc;
  336.             //checking cin to make sure it is infact and interger,loops till interger is input, clear out extra inputs
  337.             inputcheck = cin.fail();
  338.             cin.clear();
  339.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  340.         }
  341.     }while (inputcheck == true);
  342.     return mainc;
  343.  
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement