CPP   65

strings 2

Guest on 20th April 2022 02:08:30 AM

  1. // For C++ libraries.
  2. #include <iostream>
  3.  
  4. // Library for strings.
  5. #include<string>
  6.  
  7. // The c standard library, math and time libraries are needed.
  8. #include <cstdlib>
  9. #include <cmath>
  10. #include <ctime>
  11.  
  12. using namespace std;
  13.  
  14. int randomItem(int max) {
  15.     // Set the randomization seed to be based on the current time in seconds in Jan 1, 1970.
  16.         srand((unsigned)time(0));
  17.  
  18.         // Pick a random number from 0 to max.
  19.         return rand()%max;
  20.  
  21. }
  22.  
  23. int main() {
  24.     string days[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday",
  25.                    "Saturday"};
  26.  
  27.     string verbs[]={"Went","Walked","Drove"};
  28.     string nouns[]={"to School","to Work","Home"};
  29.  
  30.  
  31.     int day,verb,noun;
  32.  
  33.     day=randomItem(7);
  34.     verb=randomItem(3);
  35.     noun=randomItem(3);
  36.  
  37.     cout << "On " << days[day] << " I " << verbs[verb] << " " << nouns[noun] << endl;
  38.     return 0;
  39. }

Raw Paste


Login or Register to edit or fork this paste. It's free.