Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 12-02-2008, 05:02 PM   #1
In Runtime
a1sporty15's Avatar
Join Date: Dec 2007
Posts: 102
Default C++ help!

I have to write a program that makes the computer guess your number. Ok first you input your number then the computer guesses it such as "is your number greater than 25 then less than 37 until it reachers your number. I have this all done but it does not seem to work.. It compiles normally and everything but the out put does not work....


Also there has to be an easier way to write this program...

a1sporty15 is offline   Reply With Quote
Old 12-02-2008, 06:01 PM   #2
JogaBonito1502's Avatar
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: C++ help!

There definitely is an easier way to write this. I wrote a similar program fro a number guessing game where the computer would tell you how far off you were. What you have to do is make the computer ask (assuming input is from 0-100) if your number is greater or lower than the midpoint of the range:

"Is your number greater than or less than 50?"

Then whatever answer you get you divide the new range again. So if the user answers "lower" the computer will ask:

"Is your number greater than or less than 25?"

You keep this going until the range is 1. To do this you can create a while-loop that will test while some Boolean variable is false. Definite it correct, or guess, or something. When the person guesses it correctly, you change it to true and snap out of the loop. You can then create variables for the upper, lower, and midpoint of the range. Depending on the user's answer you change an old upper or lower to the old midpoint. Then to find the new midpoint you find the difference in the range, divide it by two, and add/subtract from the lower/upper (doesn't matter which you do so long add to the lower or subtract from the upper).

If you don't know loops look for a quick tutorial on google, or if you want I'll teach it real quick.
JogaBonito1502 is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 08:44 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0