cplusplus-training/Teil_1/4. Kontrollstrukturen/Lösungen/Loesung Uebung 3 - Zahlenraten.cpp
2021-10-15 08:36:02 +02:00

35 lines
648 B
C++

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
int zahl, eingabe, versuche=0;
srand (time(NULL));
zahl = rand()%100 + 1; //Zufallszahl zwischen 0 und 99 wird generiert, daher +1, damit die Zahl zwischen 1 und 100 ist
do
{
cout << "Zahl eingeben: ";
cin >> eingabe;
if(eingabe > zahl)
cout << "Zu gross!" << endl;
if(eingabe < zahl)
cout << "Zu klein!" << endl;
versuche ++;
}
while(eingabe != zahl && versuche < 6);
if (eingabe == zahl)
cout << "Erraten!" << endl;
else
cout << "Verloren! Die gesuchte Zahl war " << zahl << endl;
system("pause");
return 0;
}