Programming update

So here is the finished guessing game. I should have posted it way earlier because it already feels like I could do it better with the knowledge from the first lectures of this week. But I figure this is too basic a game to put any more time into, because regardless how perfect, it will always be a boring game to play.


#include <iostream>
#include <time.h>
#include <stdlib.h>

int random(int min, int max)
{
    return min + (rand() % (max - min + 1));
}

int main(int argc, char* argv[])
{
    int tries = 0;
    srand((unsigned int)time(0));
    int randomnumber = random(1, 100);
    bool victory = false;
    std::cout << "Guess the number between 1 and 100." << std::endl << std::endl;
    
    //std::cout << "random(): " << randomnumber << std::endl;

    while (true)
    {
        int guess = 0;    
        std::cin >> guess;

        if (guess >= 1 && guess <= 100)
        {
            if (guess == randomnumber)
            {
                std::cout << "Correct! You win! ";
                tries++;
                break;
            }

            else if (guess < randomnumber)
            {
                std::cout << "Too low. Try again.\n" << std::endl;
                tries++;
            }

            else if (guess > randomnumber)
            {
                std::cout << "Too high. Try again.\n" << std::endl;
                tries++;
            }
        }

        else
        {
            std::cout << guess << " is not between 1 and 100, stupid! Counts as a guess anyway.\n" << std::endl;
            tries++;
        }
    }

    if (tries == 1)
    {
        std::cout << "And on the first guess, too! You're a natural!\nPress Enter to exit.\n";
    }
    
    else
    {
        std::cout << "You needed " << tries << " guesses. Press Enter to exit.\n" << std::endl;
    }

    std::cin.ignore(1024, '\n');
    std::cin.get();
    return 0;
}

Featured image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s