C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-30-2008, 03:28 PM   #1
Registered User
 
Join Date: Sep 2008
Posts: 3
Number Guessing

First post here, hopefully more to come. Anyway, I'm in an intro to C programming course and about in my fourth week in. So far, we've gone over printf/scanf functions, for/while loops, if statements and stuff like that.

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?")
}
EDIT: so, to summarize: Is my first boolean statement fine with rand being set to the range between 1 and 2? If someone can point out how to make it within a while statement ended with a sentinel, I think I'm almost there. 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   Reply With Quote
Old 09-30-2008, 08:17 PM   #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   Reply With Quote
Old 10-01-2008, 01:48 AM   #3
Technical Lead
 
QuantumPete's Avatar
 
Join Date: Aug 2007
Location: London, UK
Posts: 723
Quote:
Originally Posted by blacknapalm View Post
I was working on getting the main skeleton down (probably a bad habit).
Actually that's a good way of going about it. Even better is to write it out in pseudo code first. Then you just have to translate pseudo code into your skeleton and then flesh (pardon the pun) the whole thing out.

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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22