Guesing game

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 13
Reputation: heshangho is an unknown quantity at this point 
Solved Threads: 0
heshangho heshangho is offline Offline
Newbie Poster

Guesing game

 
0
  #1
Mar 10th, 2008
hey guys I have a question on this guessing game. I want to make this game to repeat. when the guess is correct, i wanted the user to input 'y' or 'n' and if yes it should repeat. How can i do that? Do I have to put the whole program in to another while loop?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int main() {
  5.  
  6. int g;
  7. int x;
  8. char a;
  9.  
  10.  
  11.  
  12. srand (time(NULL));
  13. g = rand() % 1000+1;
  14.  
  15. printf("I have a number between 1 and 1000\nCan you guess my number\?\nPlease type your first guess\n");
  16.  
  17. scanf("%d", &x);
  18.  
  19. while (g!=x){
  20.  
  21. if (g<x)
  22.  
  23. printf("Too high. Try again\n");
  24.  
  25. else if (g>x) printf("your guess is less\n");
  26.  
  27. scanf("%d", &x);
  28.  
  29. }
  30. printf("Excellent!you guess the number\nWould you like to play again (y or n)?\n");
  31.  
  32.  
  33. return 0;
  34.  
  35. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Guesing game

 
0
  #2
Mar 10th, 2008
Yes, a while loop would work, or better yet a do-while.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 13
Reputation: heshangho is an unknown quantity at this point 
Solved Threads: 0
heshangho heshangho is offline Offline
Newbie Poster

Re: Guesing game

 
0
  #3
Mar 10th, 2008
ok.thanks. however i need to continue the program just if the user wish to.(by typing yes).So can you help me with that?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Guesing game

 
0
  #4
Mar 10th, 2008
What I would suggest is that you make a function of everything starting from "int g;" to the end of the while loop. Then in main() call the function inside another loop as
  1. do
  2. {
  3. function_here();
  4. printf("Excellent!you guess the number\nWould you like to play again (y or n)?\n");
  5. scanf("%c",&a);
  6. }while(a=='y'||a=='Y');
Last edited by hammerhead; Mar 10th, 2008 at 3:53 am.
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 13
Reputation: heshangho is an unknown quantity at this point 
Solved Threads: 0
heshangho heshangho is offline Offline
Newbie Poster

Re: Guesing game

 
0
  #5
Mar 10th, 2008
ok.I got it now.thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 821 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC