55 lines
1.1 KiB
C
55 lines
1.1 KiB
C
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <time.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define RANGE 100
|
||
|
#define BUF_LENGTH 255
|
||
|
|
||
|
int main(int argc, char** argv) {
|
||
|
|
||
|
char buffer[BUF_LENGTH];
|
||
|
int attempt = 0;
|
||
|
|
||
|
/* seed random generator */
|
||
|
srandom(time(NULL));
|
||
|
|
||
|
int number = (random() % RANGE) + 1;
|
||
|
|
||
|
for (;;) {
|
||
|
attempt++;
|
||
|
printf("Bitte geben Sie eine Zahl im Bereich 1-%d ein: ", RANGE);
|
||
|
int guess = 0;
|
||
|
|
||
|
memset(buffer, 0, BUF_LENGTH);
|
||
|
|
||
|
if (fgets(buffer, BUF_LENGTH - 1, stdin) != 0) {
|
||
|
sscanf(buffer, "%d", &guess);
|
||
|
/* Alternative zu sscanf
|
||
|
char* newline = strchr(buffer, '\n');
|
||
|
if (newline) {
|
||
|
*newline = '\0';
|
||
|
}
|
||
|
guess = atoi(buffer);
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
if (guess < 1 || guess > RANGE) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (guess < number) {
|
||
|
puts("Zu klein");
|
||
|
}
|
||
|
else if (guess > number) {
|
||
|
puts("Zu groß");
|
||
|
}
|
||
|
else {
|
||
|
printf("Richtig nach %d Versuchen.\n", attempt);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|