Advertisement
TermSpar

Switch Case and Random Nums

May 18th, 2016
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int uInput;
  10.  
  11.     cout << "Enter 1 to generate a random number 1-10\n";
  12.     cout << "Enter 2 to generate a random number 1-100\n";
  13.     cout << "Enter 3 to generate a random number 1-1000\n";
  14.     cout << "Enter Here: ";
  15.     cin >> uInput;
  16.  
  17.     switch(uInput)
  18.     {
  19.         case 1:
  20.         {
  21.             srand(static_cast<unsigned int>(time(0))); //Seed random number with current time.
  22.             int rndNum1 = rand() % 10 + 1;
  23.             cout << "Randomly Generated Number: " << rndNum1 << endl;
  24.  
  25.             break;
  26.         }
  27.         case 2:
  28.         {
  29.             srand(static_cast<unsigned int>(time(0))); //Seed random number with current time.
  30.             int rndNum1 = rand() % 100 + 1;
  31.             cout << "Randomly Generated Number: " << rndNum1 << endl;
  32.  
  33.             break;
  34.         }
  35.         case 3:
  36.         {
  37.             srand(static_cast<unsigned int>(time(0))); //Seed random number with current time.
  38.             int rndNum1 = rand() % 1000 + 1;
  39.             cout << "Randomly Generated Number: " << rndNum1 << endl;
  40.  
  41.             break;
  42.         }
  43.         default:
  44.         {
  45.             cout << "Invalid\n";
  46.         }
  47.  
  48.     }
  49.     system("pause");
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement