View Full Version : [C++]Guess your number
This is a program I helped a new C++ programmer with... I figured someone might want it here
#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;
}
Chruser
2004-01-30, 05:35 PM
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. :)
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
Chruser
2004-01-30, 06:12 PM
I poked around with a different, lazier version of the number guessing game which does a lot of checking.
#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;
}
I am assuming you used
char choice[50];
to prevent an error when someone stupid types in more than one character responce?
Chruser
2004-01-30, 06:54 PM
Yeah, like if someone would type in "higher" instead of 'h'.
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
BlueCube
2004-01-31, 12:50 PM
Bah, you call that lazy? Here's lazy.. (in QBASIC).
PRINT "Pick a number from 1 to 20000."
INPUT Num
PRINT "Yeah, your number is "; Num; "."
END
Demosthenes
2004-01-31, 01:02 PM
Bah, you call that lazy? Here's lazy.. (in QBASIC).
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.
Tyrannicide
2004-01-31, 01:27 PM
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*
Randuin
2004-01-31, 02:40 PM
Can we refrain from posting such obvious stupidity next time? mmm kthnxbye
vBulletin® v3.8.2, Copyright ©2000-2025, Jelsoft Enterprises Ltd.