Like Tree4Likes
  • 1 Post By Salem
  • 1 Post By brewbuck
  • 2 Post By Elysia

Generating a 4-digit number with distinct digits?

This is a discussion on Generating a 4-digit number with distinct digits? within the C++ Programming forums, part of the General Programming Boards category; I 've started programming a " Bulls and Cows " game.I got through the two player mode(which is easy). Now ...

  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 07:43 AM. Reason: Inserted the code

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    iMalc likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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.
    whiteflags likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    whiteflags and Elkvis like this.
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21