From 191705c1dcb239f80b78e0afcfad0f41633924f7 Mon Sep 17 00:00:00 2001 From: k0rrluna Date: Sat, 10 May 2025 03:04:01 +0300 Subject: [PATCH] chapter5 --- DeitelC/Chapter5/guessTheNum.c | 43 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/DeitelC/Chapter5/guessTheNum.c b/DeitelC/Chapter5/guessTheNum.c index c6695b8..d2fb903 100644 --- a/DeitelC/Chapter5/guessTheNum.c +++ b/DeitelC/Chapter5/guessTheNum.c @@ -2,18 +2,41 @@ #include #include + +void guessTheNum(); + int main(void) { srand(time(NULL)); - int num = 1 + rand() % 999; - printf("%s\n%s\n%s\n", "I have a number between 1 and 1000.", "Can you guess my number?", "Please type your first guess."); - int scanNum = 0; - scanf("%d", &scanNum); - if((scanNum > 1001) || (scanNum < 0)) { - puts("Invalid integer!"); - } + guessTheNum(); + return 0; - while(scanNum != num) { - if("") - } +} + + +void guessTheNum() +{ + int num = 1 + rand() % 1000; + printf("%s\n%s\n%s\n", "I have a number between 1 and 1000.", "Can you guess my number?", "Please type your first guess."); + int scanNum = 0; + scanf("%d", &scanNum); + char con; + + if((scanNum > 1001) || (scanNum < 0)) { + puts("Invalid integer!"); + } + + do { + while(scanNum != num) { + if(scanNum < num) { + puts("Too low!"); + scanf("%d", &scanNum); + } else { + puts("Too high"); + scanf("%d", &scanNum); + } + } + } + while(scanf(" %c", &con)); + puts("FOUND!"); }