![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 3
| Number Guessing Anyway, my teacher's powerpoint slides don't match the ones he had in class yet so I'm having a hard time with our the homework assignment. It's a number guessing program. What we have to do is get two user selected numbers, a lower number and higher number of any range. Then the program has to check if the first number is actually lower than the second number. If not, it tells the user to start over. After that, the computer randomly chooses a number between the lower and higher number (range). The user than has to guess the number. Finally, it has to end when the number guessed is correct and then output all the numbers with the correct number showing "correct" next to it. The program should ask the user if he/she wants to play again. I am willing to learn, so here is the code I have now. I know there are syntax errors, but I was working on getting the main skeleton down (probably a bad habit). Can someone help push me in the right direction. I have a feeling I want to utilize a while loop and I know I need a sentinel in there somewhere for the last part. Keep in mind, it is pretty incomplete, but how far off am I? I wanted to check now so debugging will be easier, hope that made sense. Thanks in advance. Code: #include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
int seed;
int counter;
int number_1;
int number_2;
int my_random;
int guess;
seed=time(NULL);
srand(seed);
{
printf("I will think of a number between two numbers of your choice. \n");
printf("What is the lower number?");
scanf("%d", &number_1);
printf("What is the higher number?");
scanf("%d", &number_2);
while(number_1>=number_2)
{
printf("The first number must be lower than the second. Please try again. \n");
}
else(number_1<=number_2)
{
printf("I have figured out my number. Start guessing by entering a number.");
my_random==(rand()>number_1<number_2;
scanf("%d", &guess);
}
if(guess==my_random)
{
printf("You are correct!");
}
else(guess!=my_random)
{
printf("Wrong! Try again.");
}
printf("Would you like to play again?")
}
argh, I'm having more trouble on this assignment than my last calculus one. Last edited by blacknapalm; 09-30-2008 at 06:02 PM. |
| blacknapalm is offline | |
| | #2 |
| Registered User Join Date: Sep 2008 Location: Toronto, Canada
Posts: 507
| You need to have the entire thing enclosed within a while(), and break out of that when the user doesn't want to play any more. You have to put in a correct formula to make my_random be between number_1 and number_2. Look up what range of values rand() generates and scale that to be (number_2 - number_1)+1 or something like that. Depending on whether you expect the guesses to include the numbers themselves. Do "hand" calculations on the lowest number and highest number rand() will ever give you so make sure the calculation gives the proper range. |
| nonoob is offline | |
| | #3 | |
| Technical Lead Join Date: Aug 2007 Location: London, UK
Posts: 723
| Quote:
QuantumPete
__________________ "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support "Have you tried turning it off and on again?" - The IT Crowd | |
| QuantumPete is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| program that reads a number between 1 and 999 and spells it in english | Cyberman86 | C Programming | 6 | 02-19-2009 07:19 PM |
| xor linked list | adramalech | C Programming | 23 | 10-14-2008 10:13 AM |
| Issue w/ Guess My Number Program | mkylman | C++ Programming | 5 | 08-23-2007 01:31 AM |
| My number guessing game | The Gweech | C++ Programming | 7 | 06-22-2002 09:03 AM |
| Random Number problem in number guessing game... | -leech- | Windows Programming | 8 | 01-15-2002 05:00 PM |