Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   [C++]Guess your number (http://zelaron.com/forum/showthread.php?t=27094)

Acer 2004-01-30 04:44 PM

[C++]Guess you number
 
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;
}


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. :)

Acer 2004-01-30 05:44 PM

Quote:

Originally Posted by Chruser
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.

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;
}


Acer 2004-01-30 06:20 PM

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'.

Acer 2004-01-30 07:03 PM

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

Even 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


Demosthenes 2004-01-31 01:02 PM

Quote:

Originally Posted by BlueCube
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.

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


All times are GMT -6. The time now is 05:50 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.