Kaktusen

Random generator

May 27th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <ctime>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. string s1;
  10. string s2;
  11. string s3;
  12. string s5;
  13.  
  14. void write();
  15. void read();
  16. int random();
  17.  
  18. int main() {
  19.  
  20.     read();
  21.  
  22.     string s4;
  23.  
  24.     cout << "Write to the file (Y/N): ";
  25.     cin >> s4;
  26.  
  27.     if(s4 == "Y" || s4 == "y" || s4 == "Yes" || s4 == "yes") {
  28.         write();
  29.     } else if(s4 == "N" || s4 == "n" || s4 == "No" || s4 == "no") {
  30.         random();
  31.     } else {
  32.         cout << "Wrong input!" << endl;
  33.     }
  34. }
  35.  
  36. void write() {
  37.     ofstream file ("file.txt");
  38.  
  39.     cin >> s1 >> s2;
  40.  
  41.     s3 = s1 + " " + s2;
  42.  
  43.     file << s3;
  44. }
  45.  
  46. void read() {
  47.     ifstream file ("file.txt");
  48.  
  49.     if(file.is_open()) {
  50.         while(file.good()) {
  51.             getline(file, s5);
  52.             cout << s5 << endl;
  53.         }
  54.         file.close();
  55.     }
  56. }
  57.  
  58.  
  59. int random() {
  60.     srand(time(NULL));
  61.     ifstream file("file.txt");
  62.     int r1;
  63.  
  64.     if(file.is_open()) {
  65.         while(file >> s1 >> s2) {
  66.  
  67.         }
  68.         file.close();
  69.     }
  70.  
  71.  
  72.     for(int i = 0; i<3; i++) {
  73.         r1 = rand() % 2 + 1;
  74.  
  75.         if(r1 == 1) {
  76.             cout << s1 << endl;
  77.         } else if(r1 == 2) {
  78.             cout << s2 << endl;
  79.         }
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment