Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Community Calendar Today's Posts Search
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
[C++]Guess you number
Reply
Posted 2004-01-30, 04:44 PM
This is a program I helped a new C++ programmer with... I figured someone might want it here


Code:
#include <windows.h>
#include <iostream.h>

int main(int argc, char* argv[])
{
	char z = (int)65;
	cout<<z;
	int number;
	cout<<"Pick a number 1-100 ";
	cin>>number;

	int guess = 50,last_guess = guess, input = 0;
    int high = 100, low = 1;

	while(guess != number){
        cout<<"Is your number "<<guess<<"? [1]yes [2]no ";
		cin>>input;
		if(input == 1)
			break;
		else {
            cout<<"Is it [1]higher or [2]lower? ";
			cin>>(int)input;
			if(input == 1){
               guess = (int)((last_guess + high) /2);
			   low = last_guess;
			}
			else if(input == 2) {
               guess = (int)((last_guess + low) /2);
			   high = last_guess;
			}

		}
		last_guess = guess;
	}
	cout<<"So you number is "<<guess<<endl;
	return 0;
}
Old
Profile PM WWW Search
Acer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzAcer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Acer
 



 
Reply
Posted 2004-01-30, 05:35 PM in reply to Acer's post "[C++]Guess you number"
What's the relevance in asking "Is your number 50?" if you chose, say, 20? Besides, if you lie to the program and say your number is out of the specified range, the program will get stuck somewhere and ask something like "Is your number 27?" over and over again. Otherwise, nice program.
"Stephen Wolfram is the creator of Mathematica and is widely regarded as the most important innovator in scientific and technical computing today." - Stephen Wolfram
Old
Profile PM WWW Search
Chruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed it
 
 
Chruser
 



 
Reply
Posted 2004-01-30, 05:44 PM in reply to Chruser's post starting "What's the relevance in asking "Is your..."
Chruser said:
What's the relevance in asking "Is your number 50?" if you chose, say, 20? Besides, if you lie to the program and say your number is out of the specified range, the program will get stuck somewhere and ask something like "Is your number 27?" over and over again. Otherwise, nice program.
Yeah lol, I told the person this, but he said it was good enough :P
Old
Profile PM WWW Search
Acer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzAcer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Acer
 



 
Reply
Posted 2004-01-30, 06:12 PM in reply to Acer's post "[C++]Guess you number"
I poked around with a different, lazier version of the number guessing game which does a lot of checking.

Code:
#include <iostream.h>
#include <string.h>
#include <math.h>

int main(){

	int counter=1, number=1, lasttry=1, looplimit=0;
	double increment=50, step=25;
	char choice[50];

	cout << "Input a number (1-100): ";
	cin >> number;

	while(increment!=number){
		lasttry=increment;
		if(increment>100||increment<1||looplimit>4){
			cout << "You cheated, you gotta tell me the truth son!\n";
			return 0;
		}
		cout << "You say the number isn't " << increment << ", so is it [H]igher or [L]ower than it? ";
		cin >> choice;
		if(choice[0]=='h' || choice[0]=='H'){
			increment+=step;
			if(lasttry==increment){
				increment++;
				looplimit++;
			}
			step/=2;
			step=floor(step);
			counter++;
		}
		else{
			increment-=step;
			if(lasttry==increment){
				increment--;
				looplimit++;
			}
			step/=2;
			step=floor(step);
			counter++;
		}
	}
	
	if(number==50){
		cout << "The computer instantly guessed that your number was 50.\n";
		return 0;
	}

	cout << "I guess the number you thought about is " << increment << ", and I WIN!\n";
	cout << "It took the computer a total of " << counter << " tries to guess your number.\n";

	return 0;
}
"Stephen Wolfram is the creator of Mathematica and is widely regarded as the most important innovator in scientific and technical computing today." - Stephen Wolfram

Last edited by Chruser; 2004-01-30 at 06:14 PM.
Old
Profile PM WWW Search
Chruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed it
 
 
Chruser
 



 
Reply
Posted 2004-01-30, 06:20 PM in reply to Chruser's post starting "I poked around with a different, lazier..."
I am assuming you used
char choice[50];
to prevent an error when someone stupid types in more than one character responce?
Old
Profile PM WWW Search
Acer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzAcer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Acer
 



 
Reply
Posted 2004-01-30, 06:54 PM in reply to Acer's post starting "I am assuming you used char..."
Yeah, like if someone would type in "higher" instead of 'h'.
"Stephen Wolfram is the creator of Mathematica and is widely regarded as the most important innovator in scientific and technical computing today." - Stephen Wolfram
Old
Profile PM WWW Search
Chruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed it
 
 
Chruser
 



 
Reply
Posted 2004-01-30, 07:03 PM in reply to Chruser's post starting "Yeah, like if someone would type in..."
I just relized I left that stupid char z = (int)65; in the code lol. It was an example to show how (int) (char) and such work
Old
Profile PM WWW Search
Acer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzAcer enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Acer
 



 
Even Lazier..
Reply
Posted 2004-01-31, 12:50 PM in reply to Chruser's post starting "I poked around with a different, lazier..."
Bah, you call that lazy? Here's lazy.. (in QBASIC).



Code:
PRINT "Pick a number from 1 to 20000."
INPUT Num
PRINT "Yeah, your number is "; Num; "."
END
Old
Profile PM WWW Search
BlueCube enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzBlueCube enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
BlueCube
 



 
Reply
Posted 2004-01-31, 01:02 PM in reply to BlueCube's post "Even Lazier.."
BlueCube said:
Bah, you call that lazy? Here's lazy.. (in QBASIC).



Code:
PRINT "Pick a number from 1 to 20000."
INPUT Num
PRINT "Yeah, your number is "; Num; "."
END
Omg...that might be the greatest thing i've ever seen coded. Sir...u r a genius.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2004-01-31, 01:27 PM in reply to Demosthenes's post starting "Omg...that might be the greatest thing..."
No i no even lazyier.

Input/show "; Num; "."

...yea, u type in ur number and thats it. lets c anyone get lazier then that.hahaha

umm or *is* "; Num; "." * input/show number*


~ KAMAHAME---Oh shit it's happening again.... ~
Old
Profile PM WWW Search
Tyrannicide enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzTyrannicide enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Tyrannicide
 



 
Reply
Posted 2004-01-31, 02:40 PM in reply to Tyrannicide's post starting "No i no even lazyier. Input/show ";..."
Can we refrain from posting such obvious stupidity next time? mmm kthnxbye
Old
Profile PM WWW Search
Randuin is neither ape nor machine; has so far settled for the in-between
 
 
Randuin
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules [Forum 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
Forum Jump


All times are GMT -6. The time now is 04:50 PM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.