3 digit guessing game help

This is a discussion on 3 digit guessing game help within the C++ Programming forums, part of the General Programming Boards category; Need help on solving/fixing the program... condition -Each digit of the number cannot be repeated -The first digit can start ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Unhappy 3 digit guessing game help

    Need help on solving/fixing the program...
    condition
    -Each digit of the number cannot be repeated
    -The first digit can start with 0
    -If the number entered by the user has repeating digits, the user has to be asked to re-enter a new number
    -Each attempt of guessing need to be numbered
    -The user will be told how many digits correctly positioned and how many digits wrongly positioned after each guess
    -The user will be congratulated if the user successfully guessed the number.
    -The user will be asked whether to replay or not at the end of the game

    the output of the program should be something like this~

    Please enter a non-repeating 3-digit number:
    Guess #1: 921
    You have 1 correct position, 1 wrong position.

    Guess #2: 991
    Invalid number! The digits cannot be repeated. Please re-enter a new number.
    Guess #2: 119
    Invalid number! The digits cannot be repeated. Please re-enter a new number.

    Guess #3: 917
    Congratulations! You have got the number!

    Play again? (Y/N):

    here my program~
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int random(int ran[]);
    int input(int num,int list[]);
    void repeatinput(int list[],int times);
    int check(int ran[], int list[], int* correct, int* wrong);
    void check2(int correct, int wrong);
    int reguess(int times,int list[]);
    void retrycount(int* times);
    char replay();
    void abort();
    
    
    int random(int ran[])
    {
        int x,y,z;
    
    
        srand(time (NULL));
        x= rand()%10 ;
        do
        {
            y=rand()%10;
        }while(y==x);
        do
        {
            z=rand()%10;
        }while(z==x || z==y);
    
    
        ran[0]=x;
        ran[1]=y;
        ran[2]=z;
        return ran[3];
    }
    
    
    int input(int num,int list[])
    {
        int x,y,z;
    
    
        list[0]=0;
        list[1]=0;
        list[2]=0;
        x=num/100;
        y=(num - x*100)/10;
        z=num - x*100 - y*10;
        list[0]=x;
        list[1]=y;
        list[2]=z;
        return list[3];
    }
    
    
    void repeatinput(int list[],int times)
    {
        int num;
    
    
        if(list[0]==list[1]||list[1]==list[2]||list[0]==list[2])
        {
            printf("Invalid number! The digits cannot be repeated. Please re-enter a number.\n");
            printf("Guess #%d:",times);
            scanf("%d", &num);
            input(num,list);
            repeatinput(list,times);
        }
    }
    
    
    int check(int ran[], int list[], int* correct, int* wrong)
    {
        int i,c=0,w=0;
    
    
        for(i=0;i<3;i++)
        {
            if(ran[i]==list[i])
                c++;
        }
        if(list[0]==ran[1])
            w++;
        if(list[0]==ran[2])
            w++;
        if(list[1]==ran[0])
            w++;
        if(list[1]==ran[2])
            w++;
        if(list[2]==ran[0])
            w++;
        if(list[2]==ran[1])
            w++;
        *correct = c;
        *wrong = w;
        return (*correct,*wrong);
    }
    
    
    void check2(int correct, int wrong)
    {
        if(correct!=3)
            printf("You have %d correct position, %d wrong position\n",correct,wrong);
    }
    
    
    void retrycount(int* times)
    {
        int total=1;
        total+= *times;
        *times = total;
    }
    
    
    
    
    int reguess(int times,int list[])
    {
        int num,ran[3],correct=0,wrong=0;
    
    
        printf("Guess #%d:",times);
        scanf("%d", &num);
        input(num,list);
        repeatinput(list,times);
        return(times,list[3]);
    }
    
    
    int main()
    {
        int num,list[3],ran[3],times,correct=0,wrong=0;
        char ans;
        
        random(ran);
        times=1;
        printf("This a 3-digit number guessing game\n");
        printf("Please enter a non-repeating 3-digit number:\n");
        printf("Guess #%d:",times);
        scanf("%d", &num);
        input(num,list);
        repeatinput(list,times);
        
        for(check(ran,list,&correct,&wrong) ; correct!=3 ; reguess(times,list))
        {
            retrycount(&times);
            check2(correct,wrong);
        }
        printf("Congratulations! You have got the number!\n\n");
        printf("Play again? (Y/N):");
        scanf("%c",&ans);
        while(ans!='n'||ans!='N')
            replay();
    
    
        return 0;
    }
    
    
    char replay()
    {
        int num,list[3],ran[3],times,correct=0,wrong=0;
        char ans;
        
        random(ran);
        times=1;
        printf("Please enter a non-repeating 3-digit number:\n");
        printf("Guess #%d:",times);
        scanf("%d", &num);
        input(num,list);
        repeatinput(list,times);
    
    
        for(check(ran,list,&correct,&wrong) ; correct!=3 ; reguess(times,list))
        {
            retrycount(&times);
            check2(correct,wrong);
        }
    
    
        printf("Congratulations! You have got the number!\n\n");
        printf("Play again? (Y/N):");
        scanf("%c",&ans);
    
    
        return ans;
    }
    Last edited by Styfer; 04-08-2012 at 10:01 AM.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    You might wanna narrow it down a bit, otherwise you're probably get a Happy Easter greeting at best. Do you have any clue of what's gnarly?

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    srand should only be called once, first thing in main; see the FAQ.

    Your scanf calls are leaving newline in the input buffer; see the FAQ.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Agreed on srand, take it ouf of the function and put it at the top of main. It should only be called once for the whole program.

    So, what seems to be the problem? I assume the program doesn't do everything it was meant to, or you woldn't be here. But you haven't asked a question, or told us what the problem is.

    Why is there a function prototype for abort?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 09:53 AM
  2. guessing game
    By Sal79 in forum C Programming
    Replies: 14
    Last Post: 05-09-2007, 02:22 PM
  3. guessing game
    By Inferno in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2004, 05:37 PM
  4. A guessing game
    By Lyanette in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 09:02 AM
  5. guessing game
    By wayko in forum C++ Programming
    Replies: 11
    Last Post: 09-19-2001, 06:10 PM

Tags for this Thread


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