This is the famous counterfeit coin game in C++.

# include <iostream>
# include <cstring>
# include <ctime>
# include <cstdlib>

# define coinVal 2;

using namespace std;

void gameInfo();
void generateGame();
void playGame();

int coins, counterfeitCoin;
char typeOfCounterfeit;

int main()
{
    gameInfo();
    generateGame();
}

void gameInfo()
{
    cout<<" Enter number coins(max 12) : ";
    cin>>coins;

    cout<<" Press (H) for heavy counterfeit and (L) for light : ";
    cin>>typeOfCounterfeit;
}

void playGame(int coinTab[])
{
    cout<<"\n You will get 3 chance to identify the 'bad' coin ";

    int pan1, pan2, sum1=0, sum2=0, answer;

    for(int i=0 ; i<3 ; i++)
    {
        cout<<"\n Enter number of coins in Pan 1 : ";
        cin>>pan1;

        cout<<"\n Pan 1 = ";
        int input1[pan1];

        int j=0;

        while(j<pan1)
        {
            cin>>input1[j];
            j++;
        }

        cout<<"\n Enter number of coins in Pan 2 : ";
        cin>>pan2;

        cout<<"\n Pan 2 = ";
        int input2[pan2];

        j=0;

        while(j<pan2)
        {
            cin>>input2[j];
            j++;
        }

        for(int k=0; k<pan1; k++)
        {
            sum1 = sum1 + coinTab[input1[k]];
        }

        for(int k=0; k<pan2; k++)
        {
            sum2 = sum2 + coinTab[input2[k]];
        }

        if(sum1>sum2)
        {
            cout<<"\n Pan 1 > Pan 2";
        }

        else if( sum1<sum2)
        {
            cout<<"\n Pan 1 < Pan 2";
        }
        else
        {
            cout<<"\n Pan 1 = Pan 2";
        }
    }

    cout<<"\n Enter answer : ";
    cin>>answer;

    if((answer-1)==counterfeitCoin)
    {
        cout<<"\n Correct Answer";
    }
    else
    {
        cout<<"\n Wrong Answer";
        cout<<"\n Answer is coin number "<<counterfeitCoin+1;
    }
}

void generateGame()
{
    int *coinTable = new int[coins];

    for(int i=0; i<coins; i++)
    {
        coinTable[i] = coinVal;
    }

    srand ( time(NULL) );

    counterfeitCoin = rand() % coins;

    if(typeOfCounterfeit=='H')
    {
        coinTable[counterfeitCoin] = 3;

    }

    else
    {
        coinTable[counterfeitCoin] = 1;
    }

    playGame(coinTable);

}

There is some glitch in the program. Some times is runs well, but sometimes it gives wrong answer. Maybe a logical error. Also if there are any other error please point it out. Any help will be appreciated.

Recommended Answers

All 3 Replies

Sorry, I didn't comment the code. Hope the identifiers are self explanatory to all.

I don't get it -- how do you play that game? What do the numbers you put in pan1 and pan2 represent? Here is one of my inputs but I don't know whether its right or wrong because I don't understand the game.

Enter number coins(max 12) : 12
Press (H) for heavy counterfeit and (L) for light : H

You will get 3 chance to identify the 'bad' coin
Enter number of coins in Pan 1 : 6

Pan 1 = 1 0 0 0 0 0

Enter number of coins in Pan 2 : 6

Pan 2 = 2 1 0 0 0 0

Pan 1 > Pan 2
Enter number of coins in Pan 1 :

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.