# 3 digit guessing game help

• 04-08-2012
Styfer
3 digit guessing game help
Need help on solving/fixing the program... :frown:
condition
-Each digit of the number cannot be repeated
-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; }```
• 04-08-2012
überfuzz
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?
• 04-08-2012
rags_to_riches
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.
• 04-08-2012
iMalc
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?