Advertisement
Lawnknome

randFun

Nov 2nd, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. /*******************************************************************************************
  2. * Author:                                   Jared Hughes
  3. * Date Created:                             10/30/2014
  4. * Last Modification Date:                   10/30/2014
  5. * Filename:                                 randFun.cpp
  6. *
  7. * Overview:
  8. *       This program will display a single random number to a user from a
  9. *       range.  The range will be decided by the user.  The data will be
  10. *       validated to be only integers as well as confirming the range is legal
  11. *
  12. * Input:
  13. *       Input will be two integers given by the user.  One low end and one high end.  
  14. *       These numbers will be validated as integers and validated to make sure the low
  15. *       end is less than the high end.
  16. *
  17. * Output:
  18. *       The final output of this program will be a single interger displayed to the user
  19. *       between the range of min and max.
  20. *
  21. *
  22. *********************************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <ctime>
  26. #include <cstdlib>
  27. using namespace std;
  28.  
  29.  
  30. void rand_int(const int &min, const int &max, int &val);        //Function to be called later - random number generator
  31.  
  32. int main ()
  33. {
  34.  
  35.     srand(time(0));
  36.  
  37.     int maximum;                //Max range of random number generator
  38.     int minimum;                //Minimum range of random number generator
  39.     int value;                  //value returned from rand_int()
  40.  
  41.     cout << "This is a random number generator\n";
  42.     cout << "Please input the first interger as your minimum: ";
  43.    
  44.     while (!(cin >> minimum))
  45.     {
  46.         cin.clear();
  47.         cin.ignore(1000, '\n');
  48.         cout << "Please make sure your minimum input is an integer.\n";
  49.         cout << "Please input the first interger as your minimum: ";
  50.     }
  51.  
  52.     cout << "Please input the second integer as your maximum: ";
  53.  
  54.     while(!(cin >> maximum))
  55.     {  
  56.         cin.clear();
  57.         cin.ignore(1000, '\n');
  58.         cout << "Please make sure your maximum input is an integer.\n";
  59.         cout << "Please input the second integer as your maximum: ";
  60.     }  
  61.  
  62.     while(minimum >= maximum)
  63.     {  
  64.         cout << "Please make sure your minimum is less than your maximum\n";
  65.         cout << "Please input the first interger as your minimum: ";
  66.    
  67.         while (!(cin >> minimum))
  68.         {
  69.             cin.clear();
  70.             cin.ignore(1000, '\n');
  71.             cout << "Please make sure your minimum input is an integer.\n";
  72.             cout << "Please input the first interger as your minimum: ";
  73.         }
  74.  
  75.         cout << "Please input the second integer as your maximum: ";
  76.  
  77.         while(!(cin >> maximum))
  78.         {  
  79.             cin.clear();
  80.             cin.ignore(1000, '\n');
  81.             cout << "Please make sure your maximum input is an integer.\n";
  82.             cout << "Please input the second integer as your maximum: ";
  83.         }  
  84.     }  
  85.  
  86.     rand_int(minimum, maximum, value);
  87.  
  88.     cout << "Your randomly generated integer between " << minimum << " and " << maximum << " is " << value << "." << endl;
  89.  
  90.     return 0;
  91. }
  92.  
  93.  
  94. /*******************************************************
  95. * Function to pull in two user input integers as range *
  96. * min and max, will output a single random number      *
  97. * within that range.  Will return that value to main   *
  98. *******************************************************/
  99.  
  100. void rand_int(const int &min, const int &max, int &val)
  101. {
  102.     int range;                  //Numbers in the calculated range;
  103.    
  104.     range = max - min + 1;
  105.     val = (rand() % range) + min;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement