(C++) Funny little number game

Results 1 to 5 of 5
  1. #1
    Member 9000234 is offline
    MemberRank
    May 2008 Join Date
    85Posts

    (C++) Funny little number game

    Started c++ a week ago, and this is so far the biggest thing I've accomplished with it so far.

    Ideas for helping me to improve are always accepted :)

    Code:
    #include <iostream>  
    #include <ctime> 
    #include <windows.h>
    
    
    using namespace std;
    
    int main () 
    {
        keybd_event(VK_MENU,0x36,0,0);
        keybd_event(VK_RETURN,0x1c,0,0);
        keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
        keybd_event(VK_MENU,0x36,KEYEVENTF_KEYUP,0);
    
    int age;
    int rlage;
    string p; 
    
    cout << "Please enter your age: "; 
    cin  >> age; 
    
    if ( age < 40 ) { 
    cout << "You are pretty young!\n\n"; 
    }
    else if ( age > 41 ) { 
    cout << "You are getting old\n\n"; 
    }
    cin.get();
    
    
    cout << "Wanna play guess the number!?!?! (y/n): ";
    getline (cin, p);
    if (p == "n") {
         return 0; }
         else {
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading.\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading..\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading...\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading....\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading.....\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading.\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading..\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading...\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading....\n";
    Sleep(500);
    system("CLS");
    cout << "Ok!!!\n";
    cout << "Loading.....\n";
    Sleep(500);
    }
    system("CLS");
        srand((unsigned)time(0)); 
        int random_integer; 
        int lowest=1, highest=1000; 
        int range=(highest-lowest)+1; 
            random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
            rlage = random_integer;
    
    cout << "Guess the number between 1 and 1000: ";
    do
    {
    
    cin >> age;
    
    if ( age <= rlage ) {
    cout<<"Your cold, keep guessing\n";
    }
    else if ( age > rlage ) {
    cout<<"Your going to high, keep guessing\n";
    }
    else if ( age == rlage ) {
    cout<<"Well done! You guessed correctly\n";
    }
    
    } while (age != rlage);
    
    return 0;
    
    }


  2. #2
    Account Upgraded | Title Enabled! Intelext is offline
    MemberRank
    Mar 2007 Join Date
    EuropeLocation
    1,228Posts

    Re: (C++) Funny little number game

    I bet those loadings would be annoying. =)

    Otherwise, very good. C++ rocks.

  3. #3
    Super Mexican Joser is offline
    MemberRank
    Jun 2008 Join Date
    Your mom's roomLocation
    1,656Posts

    Re: (C++) Funny little number game

    Very nice. But maybe try not to use a system call for CLS.
    I hear that its bad practice.

    Otherewise very awesome!! ^^
    Indeed C++ is teh best!

    EDIT: When I guess correctly thew screen just dissapears.
    You need to put a pause before the end of the game so you can see the end screen.
    Hope I helped.

  4. #4
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: (C++) Funny little number game

    I've got a few points for you to improve on:

    1. Try to make programs as fast as possible, that is, remove the loading part, lol, it annoys and is not needed.

    Also, it seems system calls (system()), is rather slow...

    2. Try, if you can, to make the code cross-platform (except if you're an MS freak :O). System() and Sleep() are not cross-platform (though for system() it depends...).

    3. Use indentation, it helps a lot!

    Quote Originally Posted by Intelext View Post
    Otherwise, very good. C++ rocks.
    I second that :D

  5. #5
    Member 9000234 is offline
    MemberRank
    May 2008 Join Date
    85Posts

    Re: (C++) Funny little number game

    aye, I know system is not cross platform, all it does is call the string + .exe from within the system32 folder. If you guys know an alternative to CLS, sleep, and pause (apart from cin.get) please let me know! ^_^

    P.S. The loading screens were just to test something lol



Advertisement