1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
   |  
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cstdlib>
using namespace std;
# define   INFILE   "oneliners" /* fichier a ouvrir */
 
main ( )
{
  ifstream f;  /* pointeur */
  static long offset [ 100 ]; /* array ou stocker les jokes */
  int c = 0; /* compteur */
  char iline [ 81 ]; 
  char yesno; /* variable pour l'input (Y/N) */
 
  srand (time (NULL)); /* initialisation du generateur */
  int joke; A
 
  f.open ( INFILE );
  if ( ! f.is_open ( ) ) {
    cerr << "Unable to open " << INFILE << "!\n\n";
    exit ( 1 );
    }
 
  /*
   * Boucle pour lecture du fichier 
   */
  while ( f.getline ( iline, 81) ) {
    if ( iline [ 0 ] == '\0') {      /* a chaque espace chaque joke */
      offset [ c ] = f.tellg ( );   /* utiliser ftell pour y assigner une position */
      c ++; /* incremenation du compteur */
      }
    }
 
  for ( c = 0; offset [ c ] > 0; c ++ ) {
    f.clear ( );
    f.seekg ( offset [ c ] ); /* chercher la position ? */
    f.getline ( iline, 81 );   /* lire la ligne suivante */
    cout << setw ( 3 ) << c + 1 << ". " << iline << endl;
  }
 
  cout << "\n You have "<< c << " jokes. \n";
  cout << "\n Do you want hear a joke ? (Y/N) > ";
  cin >> yesno;
 
 while ( yesno == 'Y' || yesno == 'y' ) {
  joke = rand()  % 16 + 1;
  cout << "\n A random joke would be number " << joke << "\n";
  cout << offset[c] << endl;
  cout << "\n\n Do you want hear a joke ? (Y/N) > ";
  cin >> yesno;
 }
} | 
Partager