Thread: Generating a 4-digit number with distinct digits?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Generating a 4-digit number with distinct digits?

    I've started programming a "Bulls and Cows" game.I got through the two player mode(which is easy). Now for the single player, I know how to generate a random number but I have a problem in generating a number with distinct digits(4-digit number)?Can anyone help me out?

    Example:
    The number can be 1234 not 1133 or 2448.

    [code]
    #include <stdlib.h>
    #include <iostream.h>
    #include <conio.h>

    void main()
    {
    char guess[10], word[10];
    int turns, a, b, bulls, cows;
    clrscr();
    cout<<"Welcome to Bulls & Cows \n\nPlayer 1, enter the letters(distinct) of your word: ";
    cin.getline(word,sizeof(word));
    clrscr();
    cout<<"\nPlayer2, please get ready.";

    for(turns=1;turns<=10;turns++)
    {
    bulls=0;cows=0;
    cout<<"\nEnter Guess : "<<turns;
    cin.getline(guess,sizeof(guess));

    for(a=0;a<4;a++)
    {
    if(guess[a]==word[a])
    {
    bulls++;
    }
    else
    {
    for(b=0;b<4;b++)
    {
    if(guess[a]==word[b])
    {
    cows++;
    }
    }
    }
    }

    if(bulls==4)
    {
    cout<<"\nYou are Victorious!\n";
    break;
    }else
    {
    cout<<"\nBulls ="<<bulls<< "Cows ="<<cows;
    }
    }

    getch();
    }
    [\code]
    Last edited by droidbreath; 02-19-2013 at 08:43 AM. Reason: Inserted the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Another approach is to generate a random four digit number, check if any digits are repeated, and if so throw the number away and generate another one.

    Sounds dumb, but is in fact a common way of generating samples of a specific distribution.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    I'm new to c++ and this is how they teach in our school.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Occasionally someone does get by the code filter though. This will help you follow board etiquette in the future. Code tags are supposed to be [code] [ / code] and you can also use the # button when making a new thread, or on the advanced reply form. To get from the Quick Reply box to the advanced reply form, click the button that says Go Advanced.

    In general, check your posts before you submit em. We have to see exactly what you see. People can post the nastiest junk code sometimes...

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by droidbreath View Post
    I'm new to c++ and this is how they teach in our school.
    This is an opportunity for you to learn yourself better C++ than the (seemingly) junk your school teaches you. It will also be of more use to you in real life.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion to find sum of digits of n digit number ?
    By justine in forum C Programming
    Replies: 7
    Last Post: 11-26-2012, 05:35 AM
  2. sum of digits of a number reducing to 1 digit.
    By amolkarale in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 05:55 PM
  3. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  4. five digit number with different digits
    By khdani in forum C Programming
    Replies: 11
    Last Post: 05-03-2009, 11:33 AM
  5. To get the sum of digits of a five digit number
    By hum_tum2005007 in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 04:39 PM