Thread: Problem with a guess game using a while and do-while loops

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

    Problem with a guess game using a while and do-while loops

    Hello! I am a beginner in C programming. I was trying to write a program which asks a user to guess a secret number which is created by rand() function.
    At this stage the program should tell how many digits of the nuber given by user coincide with digits of the secret number.
    The problem is that even if the user guesses n digits, the program gives a number <n.
    Example:

    Enter a new number:3546
    6305
    number of guessed digits is 2

    (In the middle is the secret number.)


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int main(){
        setvbuf(stdout,NULL,_IONBF,0);
    
    
        int n=10000;
        srand(time(NULL));
        int r=rand()%n;
    
    
        int d1,d2,d3,d4;
    
    
        d1=r/1000;
        d2=(r-d1*1000)/100;
        d3=(r-d1*1000-d2*100)/10;
        d4=r-d1*1000-d2*100-d3*10;
    
    
        printf("%d,%d,%d,%d\n",d1,d2,d3,d4);
    
    
        char c;
    
    
        if(d1!=d2&&d1!=d3&&d1!=d4&&d2!=d3&&d2!=d4&&d3!=d4){
    
    
         printf("Enter a number:");
         scanf("%c",&c);
    
    
         while(c!='\n'){
         int k=0;
    
    
         do{
          c=getchar();
          int digit=c-48;
    
    
           if(digit==d1){
            ++k;
           }
           else if(digit==d2){
            ++k;
            }
           else if(digit==d3){
            ++k;
           }
           else if(digit==d4){
            ++k;
           }
    
    
          }while(c!='\n');
    
    
    
    
         printf("%i\n",r);
         printf("number of guessed digits is %i\n\n",k);
    
    
         printf("Enter a new number:");
         scanf("%c",&c);
         }
        }
    
    
        return 0;
    }
    Thanks for help in advance!
    Last edited by A_Baur; 02-04-2016 at 10:36 AM. Reason: mistake in text (merging of digits)

  2. #2
    Registered User naaissus's Avatar
    Join Date
    Jan 2016
    Location
    Balkan
    Posts
    23
    Hi

    Lines 34 and 70, why are you reading that character? Remove those lines.

    If the number is 7894 and if user enters 7894, 1st character goes to scanf and getchar reads only 894 so output is 3.
    If user enters x7894, 'x' goes to scanf and getchar reads 7894 so output is 4.
    Note: if user enters 7777 output will be 4. Find a way to overcome that.

    Are you trying to terminate program if user enters only '\n'?
    I think that it's better to use some other character.
    You can edit your code e. g. like this:

    Code:
    printf("Enter a number:");
    while(true){
        int k=0;
        do {  
            c = getchar();
            if (c == '-') return 0;
            ...
        }
        ...
    }
    What do you think about implementing this logic into your game:

    while true (i. e. while user doesn't enter '-')
    --- generate random number
    --- while user doesn't guess number ask him for input
    Last edited by naaissus; 02-04-2016 at 12:53 PM.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    2
    Thank you a lot!
    Now it provides the right output-right number of coinciding digits. I have corrected the code according to your advice. I will make other additions later.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int main(){
    	setvbuf(stdout,NULL,_IONBF,0);
    
    
    	int n=10000;
    	srand(time(NULL));
    	int r=rand()%n;   //makes a random (secret) number
    
    
        int d1,d2,d3,d4;   //declares digits of the random number
    
    
        d1=r/1000;   //these 4 lines calculate separate digits of the random number
        d2=(r-d1*1000)/100;
        d3=(r-d1*1000-d2*100)/10;
        d4=r-d1*1000-d2*100-d3*10;
    
    
        char c;  //declares char c which will be a digit of a number given by user (guess)
    
    
      if(d1!=d2&&d1!=d3&&d1!=d4&&d2!=d3&&d2!=d4&&d3!=d4){  //prevents using a random number which has some duplicate digits
    
    
      printf("Enter a number(without duplicating digits)and question mark:");
    
    
        while(c!='\n'){
         int k=0;        //initialize a counter of right guesses
    
    
         do{                //scan all digits(characters) of user's guess until new line
           c=getchar();     //scan each character of a user's number
           int digit=c-48;  //convert the character into digit by using its ASCII value
    
    
           if(digit==d1){  //if user digit coincides with the first digit of random number add 1 to counter
        	++k;
           }
           else if(digit==d2){   //if no, check if it coincides with second digit
        	++k;
            }
           else if(digit==d3){
            ++k;
           }
           else if(digit==d4){
        	++k;
           }
          }while(c!='?');
    
    
         printf("number of guessed digits is %i\n",k);
         printf("secret number =%i\n\n",r);
         printf("Enter a new number(without duplicating digits)and question mark:");//asks the user to try another number
        }
       }
    
    
    	return 0;
    }

  4. #4
    Registered User naaissus's Avatar
    Join Date
    Jan 2016
    Location
    Balkan
    Posts
    23
    You're welcome.

    (without duplicating digits)
    Very clever

    and question mark
    You have misunderstood me Change that line (54) back to while(c!='\n')
    I suggested you to make an infinite loop and that special character (e. g. '?') was supposed to be entered by user if he wants to quit.

    I have implemented those things in my version of your game, but I'll not post that until you finish your game.

    Code:
    while (true) { // c variable is uninitialised before this loop so it's not a good idea to use it in a condition... better idea is to make an infinite loop
        int k=0;
        do{
            c=getchar();
            if (c == '?') return 0; // if user enters '?' then game over
            int digit=c-48;
            if (digit == d1){
                ++k;
            }
            else if(digit==d2){
                ++k;
            }
            else if(digit==d3){
                ++k;
            }
            else if(digit==d4){
                ++k;
            }
       }while(c!='\n');
    }
    Btw what if user enters 0123456789?
    Last edited by naaissus; 02-04-2016 at 04:43 PM.

  5. #5
    Registered User naaissus's Avatar
    Join Date
    Jan 2016
    Location
    Balkan
    Posts
    23
    Of course, add
    Code:
    printf("number of guessed digits is %i\n",k);
    printf("secret number =%i\n\n",r);
    printf("Enter a new number(without duplicating digits)and question mark:");//asks the user to try another number
    to my above code after while(c!='\n'); and before }.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. guess game help...
    By gnomelook in forum C Programming
    Replies: 5
    Last Post: 01-05-2012, 04:58 AM
  2. Guess number game (simple)
    By Fotis in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2011, 05:00 PM
  3. Guess the number game
    By Ninestar in forum C Programming
    Replies: 12
    Last Post: 12-08-2005, 11:30 AM
  4. Guess Who Game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-04-2004, 01:05 AM
  5. I Guess This Counts As A Game....
    By Krak in forum Game Programming
    Replies: 12
    Last Post: 07-07-2003, 08:57 PM

Tags for this Thread