473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

need help setting up a loop?

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. }
Oct 13 '09 #1
1 2761
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.
Oct 13 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop...
3
by: Mike | last post by:
Hey guys I am pulling my hair out on this problem!!!!! Any help or ideas or comments on how to make this work I would be grateful! I have been working on this for the past 4 days and nothing I do...
7
by: berkay | last post by:
i have a txt file; berkay#white jack#black smith#jane writes in it. and after i run the program it only prints smith jane and crashes what is wrong?
6
by: Martijn van Oosterhout | last post by:
I've had some fun in the past where I've had to grant a lot of tables and other similar system commands. Unfortunatly, you can't use queries to fill in fields for you. Anyway, I've implemented a...
5
by: Charlie | last post by:
Hi: I'm working on an e-commerce site. Using the SMTP class, my site sends out confirmation messages. It works most of the time, but sometimes raises an error. I need a way of making sure it...
4
by: MS | last post by:
Hi. I'm new to developing and c#. I'm developing a tool that allows a user to select a directory with specially formatted files. The tool then iterates through the list of files in the directory...
1
by: simon | last post by:
I have an unbound RTF2 control in an Access 2003 form which I set to show either unformatted text or else text with words and sentences highlighted. I do this by setting the PlainText property,...
68
by: Gsec | last post by:
Hi, Can anybody let me know how to write a infinite loop, such that the program never crashes ? I, guess, buffer overflow method mite help!Not sure. Plz let me know. -thanks and regards,
0
by: Andy | last post by:
Hello all, I have a user getting an error 438, object doesn't support this prop. or method, when I run code to setup the treeview control. I'm using the MSCOMCTRL.OCX, file version: 6.01.9545....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.