fork download
  1. #include <ctime>
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. //------------------------------------------------------------------------
  8. // Vielleicht den Teil in andere Datei auslagern...
  9. enum ResultType
  10. {
  11. ZU_KLEIN = -1,
  12. RICHTIG = 0,
  13. ZU_GROSS = 1
  14. };
  15.  
  16. int signum(int x)
  17. {
  18. if ( x < 0 )
  19. return -1;
  20. else if ( x > 0 )
  21. return 1;
  22. else
  23. return 0;
  24. }
  25.  
  26. ResultType evaluate(int kandidat, int ziel)
  27. {
  28. return static_cast<ResultType>(signum(kandidat - ziel));
  29. }
  30. // Bis hierhin...
  31. //------------------------------------------------------------------------
  32.  
  33. int main(void)
  34. {
  35. srand(static_cast<unsigned int>(time(NULL)));
  36. int rateZahl = rand() % 1000 + 1;
  37.  
  38. int eingegebeneZahl;
  39. int anzahlVersuche = 0;
  40.  
  41. do {
  42. anzahlVersuche++;
  43. cout << anzahlVersuche << ". Versuch" << endl;
  44. cout << "Zufallszahl zwischen 1 und 1000 eingeben: ";
  45. if ( !(cin >> eingegebeneZahl) )
  46. return 1;
  47.  
  48. ResultType result = evaluate(eingegebeneZahl, rateZahl);
  49.  
  50. if ( result == ZU_KLEIN )
  51. cout << "Zahl ist zu klein.\n" << endl;
  52. else if ( result == ZU_GROSS )
  53. cout << "Zahl ist zu gross.\n" << endl;
  54.  
  55. } while ( eingegebeneZahl != rateZahl );
  56.  
  57. cout << endl;
  58.  
  59. if ( anzahlVersuche < 10 )
  60. cout << "Weniger als 10 Versuche, Glueck gehabt" << endl;
  61. else if ( anzahlVersuche == 10 )
  62. cout << "Prima, 10 Versuche, Sie sind ueberlegt an das Problem herangegangen" << endl;
  63. else
  64. cout << "Mehr als 10 Versuche, sie haben mehr oder weniger im Nebel gestochert" << endl;
  65.  
  66.  
  67. cin.get();
  68. return 0;
  69. }
  70.  
Runtime error #stdin #stdout 0.02s 2728KB
stdin
Standard input is empty
stdout
1. Versuch
Zufallszahl zwischen 1 und 1000 eingeben: