Connecting Tech Pros Worldwide Help | Site Map

need help setting up a loop?

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: Oct 13 '09
i have to write a program that is a guessing the number game. the user has to choose the number of games they want to play. i need help setting up the loop that will run the number of times the user wants it to here is my code so far
Expand|Select|Wrap|Line Numbers
  1. //Assignment 2 Play
  2. //In this guessing game the user will be asked to guess a number from between 1-100
  3. //The user has ten tries if they donot guess the right number its game over
  4. //The user also gets to choose how many times they want to play between 1-10
  5. #define _CRT_SECURE_NO_DEPRECATE
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. int 
  12. main(void)
  13.  
  14. {
  15. FILE *inptr;  
  16. char firsti;        //Input users first initial
  17. char secondi;        //Input users second initial
  18. int games;            //Input, number of games user wants to play
  19. int valid=0;        //For the while loop, how many games the user wants to play
  20.  
  21. int tries;            //number of tries the user has to get the right number which is 10
  22. int guess1=0;        //Input the users guess at the number
  23. int guess2;
  24. int number;            //The ramdom number the computer picks so the user can guess
  25. int i=0;
  26.  
  27. void Instructions();
  28. printf("Welcome to Number Guess\n");
  29. printf("To begin, you will need to enter your initials\n");
  30. printf("You will then enter the number of games you want to play\n");
  31. printf("You have 10 chances to guess the number\n");
  32. printf("Let’s begin:\n\n");
  33.  
  34. //Asks the user to input his/her initials
  35. printf("Enter your two initials: ");
  36. scanf("%c%c", &firsti, &secondi);
  37. //opening file
  38. inptr=fopen("answers.txt","r");
  39.  
  40. //Loop for the number of times the user wants to play
  41. while(valid==0)
  42. {
  43.     printf("\nHow many games would you like to play %c%c?(1-10)\n",firsti, secondi);
  44.     scanf("%d", &games);
  45. if(games<1)
  46. {
  47.     printf("Thats less than 1\n");
  48.     valid=0;
  49. }
  50. else if(games>10)
  51. {
  52.     printf("That's too many!\n");
  53.     valid=0;
  54.  
  55. }
  56. else 
  57.  
  58.     valid=1;
  59.  
  60.  
  61. }
  62.  
  63. //User has to guess the number
  64. //Prompts the user the game number they are playing
  65. printf("Lets play game %c%c\n", firsti, secondi);
  66.  
  67.  
  68. printf("You have 10 guesses to get the correct answer\n");
  69.  
  70. srand(time(NULL));
  71. number = 1 + rand() % 100;
  72. //Loop for game
  73. for(tries=1; tries<11&&guess1!=number;tries++)
  74.  
  75. {
  76. //Asks user to input their guess
  77.     printf("please enter a number to guess between 0 and 100: ");
  78. scanf("%d", &guess1);
  79.  
  80. if(guess1>number)
  81.  
  82.     printf("Incorrect, you are to high\n");
  83.  
  84.  
  85. if(guess1<number)
  86.  
  87.     printf("Incorrect, you are to low\n");
  88.  
  89. if(guess1==number);
  90.  
  91. printf("Good job! You guessed it!\n");
  92.  
  93.  
  94. system("PAUSE");
  95.  
  96. printf("Game over\n");
  97.  
  98.  
  99. return(0);
  100. }
Member
 
Join Date: Jan 2008
Posts: 38
#2: Oct 13 '09

re: need help setting up a loop?


Change the condition of your while loop to reflect your two input criteria (so say while greater than 1 and less than 10). Take in your value for the number of games and use a second loop outside of the while loop but surrounding your game loop. You'll have a set number of games, so treat it like when you had a set number of guesses.

Also, please use the code tags (highlight your code text and hit the # button on the upper right hand side of the input box). Otherwise looks good.
Reply