Number Guessing Game | C Programming Example

  Рет қаралды 10,439

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 21
@Baraa_ali62
@Baraa_ali62 Жыл бұрын
That's great , Thanks for the helpfull turorial , I just have a question, I want after 20 for.ex attempts the program stops and print the correct number how do I do that 😅
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome! :-) To solve that problem I would create a counter variable above the loop and initialize it to zero. Then each time at the end of the loop body m, the counter variable can be incremented to recognize another guess has occurred. Then at the beginning of the loop body you can have an if statement that checks if the counter variable is equal to 20 (or whatever), and if it is, print a message and use “break;” to stop the loop. :-)
@ORELKY
@ORELKY 2 жыл бұрын
Thank you so much. What about if you wanted to implement a point system based on how many times you got it right? would that be hard to make?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Ryan! 🙂And that shouldn't be too bad to implement. You could create a variable for points. And then you could wrap the game loop in another loop that allows the user to keep playing the game again and again (maybe ask the user if they want to keep playing each time in the outer loop, and have them answer Y/N). And every time the user gets it right, you could modify the points variable to reflect this and print out the new points value.
@isaack7979
@isaack7979 2 жыл бұрын
how would I continue if I wanted to ask the user if they wanted to play again select char 'y' or no select char 'n'?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question Isaac! We could wrap the main "game loop" with another loop that will continue so long as a char variable is set to 'Y'. And then we could ask the user to enter in Y or N after each game, and re-calculate the random number if they want to play again so the number is different for the next game. Here is the code below: #include #include #include int main() { srand(time(NULL)); int number = (rand() % 100) + 1; int guess = 0; char c = 'Y'; while (c == 'Y') { do { printf("Enter a Guess: "); scanf("%d", &guess); if (guess == number) printf("You got it! "); else if (guess < number) printf("Guess higher! "); else printf("Guess lower! "); } while (guess != number); fflush(stdin); printf("Keep playing? (Y/N): "); scanf("%c", &c); if (c == 'Y') number = (rand() % 100) + 1; } return 0; }
@player_1372
@player_1372 2 жыл бұрын
I just tried this out and it ended the program before I could type in y or n?, could you reformat it to not end the program that quickly? Thanks!
@harneyyerclores4382
@harneyyerclores4382 2 жыл бұрын
how can I put guess limit there?
@shang_psycho7414
@shang_psycho7414 Жыл бұрын
Thanks for the helpful tutorial
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@maryamkmr4383
@maryamkmr4383 2 жыл бұрын
Thanks for the video it helped me a lot. Would you do a prime code example too?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Maryam! :-) Do you mean prime numbers? I have this video on prime numbers in C here: kzbin.info/www/bejne/onLUeWWaqZyaa6s.
@maryamkmr4383
@maryamkmr4383 2 жыл бұрын
Yes, thank you so so much ❤️❤️ Your explanations are complete and perfect and easy to understand ♥️ Thanks again 💖
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@maryamkmr4383 You're welcome Maryam! :-D
@Tyda777
@Tyda777 Жыл бұрын
What if 2 ppl want to play?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
It would depend on if they are sharing a keyboard or not. :-) If two players are playing on the same machine, sharing the keyboard, we could alternate between prompting each user to guess the number. We would also want to decide whether they are guessing the same number or different numbers. We might want to say that the "winner" of the game is the player that guesses the number first too. We may also want to make the game play over a network or some other way such that the players cannot see each other's guesses... this would be important if they were guessing the same number in particular. So I guess the answer is that it would depend on a lot of things. :-)
@PrinceKumar-hh6yn
@PrinceKumar-hh6yn Жыл бұрын
This is interesting 😊
@phytoplancton4038
@phytoplancton4038 Жыл бұрын
if I enter a character it goes into an infinite loop edit: okay I put a getchar(); under scanf it works now!!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Awesome! :-)
@PhamousBoi
@PhamousBoi 2 жыл бұрын
Thanks Man
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
Word Scramble Game | C Programming Example
19:41
Portfolio Courses
Рет қаралды 6 М.
Building a Guessing Game | Tutorial 23
12:43
Giraffe Academy
Рет қаралды 10 М.
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Vaccination ERP FMPS SYSTÈME vidio 2
8:41
All FMPS
Рет қаралды 123
Lottery Number Generator | C Programming Example
8:41
Portfolio Courses
Рет қаралды 10 М.
C number guessing game 🔢
6:22
Bro Code
Рет қаралды 28 М.
Number Guessing Game in C - C Programming Tutorial 55
18:20
ChidresTechTutorials
Рет қаралды 27 М.
Advanced Integer Input Validation | C Programming Example
22:59
Portfolio Courses
Рет қаралды 12 М.
C++ Programming Tutorial 44 - Creating a Guessing Game
7:26
Caleb Curry
Рет қаралды 32 М.
Binary Search Algorithm | C Programming Example
11:41
Portfolio Courses
Рет қаралды 61 М.
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 957 М.
C++ Random Number Generator AKA STOP USING Rand()
7:20
The Builder
Рет қаралды 60 М.
This open problem taught me what topology is
27:26
3Blue1Brown
Рет қаралды 638 М.
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19