I've just kind of gotten bored reading over learncpp.com repeatedly, so i decided to attempt to make a couple small programs for my own enjoyment ( and to really test what I'm capable of ). I have been making this little game that I call Implicit Explicit. It's basically the same thing as Cows and Bulls.

I have a couple questions commented in the code below. Please help?

#include <iostream>


int Random ( int x )
{
    // this is completely random.. I don't know how else to do it.
    x = x * 97;
    x = x - 24;
    x = x + 175861;
    x = x / 7;
    x = x * 3;
    // trying to return a value under 10.. is this correct?
    x = x % 10;
    return x;
}


int main ( int argc, char** argv )
{
    // Is there a better way than using goto statements in this context? They don't look nice
    start:
    char cCommand = 'R';
    while ( cCommand != 'X' )
    {
        static int nOne = 3;
        nOne = Random ( nOne );
        int nTwo = Random ( nOne );
        int nThree = Random ( nTwo );
        int nFour = Random ( nThree );
        int nFive = Random ( nFour );


        std::cout << "Welcome to Implicit Explicit." << "\n";
        std::cout << "I have randomly generated a number." << "\n";
        std::cout << "The number is 5 digits long." << "\n";
        std::cout << "Try to guess what it is." << "\n";
        std::cout << "I will tell you how many digits you have guessed, and how many are in the correct spot." << "\n";
        std::cout << "Try to guess it exactly, in as few tries as possible!" << "\n";

        guess:

        int aGuess[ 5 ];

        std::cout << "Guess!" << "\n";
        std::cin >> aGuess;  
/*
Error here. D:\CodeBlocks\projects\Personal_Tests\Implicit_Explicit\implicit_explicit\main.cpp|42|error: no match for 'operator>>' in 'std::cin >> aGuess'|
*/

        int nImplicit = 0;
        int nExplicit = 0;

        if ( aGuess[ 0 ] == nOne )
        {
            nImplicit++;
        }
        else if ( aGuess[ 1 ] == nTwo )
        {
            nImplicit++;
        }
        else if ( aGuess[ 2 ] == nThree )
        {
            nImplicit++;
        }
        else if ( aGuess[ 3 ] == nFour )
        {
            nImplicit++;
        }
        else if ( aGuess[ 4 ] == nFive )
        {
            nImplicit++;
        }


        for ( int i = 0; i < 5; i++ )
        {
            // Is there a better way to right this following line? It's sloppy
            if ( aGuess[ i ] == nOne || aGuess[ i ] == nTwo || aGuess[ i ] == nThree
            || aGuess[ i ] == nFour || aGuess[ i ] == nFive )
            {
            nExplicit++;
            }
        }


        if ( nImplicit != 5 )
        {
            std::cout << "You guessed " << nExplicit << " of the 5 numbers. ";
            if ( nImplicit == 1 )
            {
                std::cout << nImplicit << " of them was in the right position, too!";
            }
            else if ( nImplicit == 0 )
            {
                std::cout << "None of them were in the correct position though!";
            }
            else
            {
                std::cout << nImplicit << " of them were in the right position, too!";
            }
            goto guess;
        }
        else
        {
            std::cout << "You Won!" << "\n";
            std::cout << "Enter 'R' to restart, or 'X' to Quit." << "\n\n";
            std::cin >> cCommand;
            goto start;
        }
    }
    return 0;
}

Thanks for any help!

Recommended Answers

All 4 Replies

for the first question google SRAND
line 42 you create an array of ints then on line 45 you attempt to read a value to this array without specifying which location in the array you want to store it. I think you want to take in the digits of the guess into the array so you can check them one by one but this is not what you are doing.

Oh I thought you could enter like 5 integers at once almost like you can with characters into an array. And thanks for the tip on SRAND

Instead of using goto, wrap the whole part you want to loop within a do {...} while statement.

Oh I thought you could enter like 5 integers at once almost like you can with characters into an array.

No if you want the individual digits in an array you will have to find a way to separate them. (there are a number of threads on this site with ideas about how to achieve this)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.