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 Жыл бұрын
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. :-)
@ORELKY2 жыл бұрын
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?
@PortfolioCourses2 жыл бұрын
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.
@isaack79792 жыл бұрын
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'?
@PortfolioCourses2 жыл бұрын
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_13722 жыл бұрын
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!
@harneyyerclores43822 жыл бұрын
how can I put guess limit there?
@shang_psycho7414 Жыл бұрын
Thanks for the helpful tutorial
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@maryamkmr43832 жыл бұрын
Thanks for the video it helped me a lot. Would you do a prime code example too?
@PortfolioCourses2 жыл бұрын
You're welcome Maryam! :-) Do you mean prime numbers? I have this video on prime numbers in C here: kzbin.info/www/bejne/onLUeWWaqZyaa6s.
@maryamkmr43832 жыл бұрын
Yes, thank you so so much ❤️❤️ Your explanations are complete and perfect and easy to understand ♥️ Thanks again 💖
@PortfolioCourses2 жыл бұрын
@@maryamkmr4383 You're welcome Maryam! :-D
@Tyda777 Жыл бұрын
What if 2 ppl want to play?
@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 Жыл бұрын
This is interesting 😊
@phytoplancton4038 Жыл бұрын
if I enter a character it goes into an infinite loop edit: okay I put a getchar(); under scanf it works now!!