Thread: Double printing on the screen problem. Help needed.

  1. #1
    Registered User Scantymix's Avatar
    Join Date
    Apr 2014
    Location
    Padova (IT)
    Posts
    4

    Double printing on the screen problem. Help needed.

    I wrote this simple program (Number Guessing Name - Pastebin.com) where the computer try to guess which is the number insered by the user.
    When the program ask to the user "The secret number is Higher or Lower than my guess %i?\n" the question is repeted two times on the screen. Someone can help me?

    Thanks.
    Code:
    	
    
        #include <stdio.h>
        #include <stdlib.h>
        #include <time.h>
         
         
        int main()
        {
         
            int num;
            int guess;
            int height=100;
            int bottom=1;
            char answ='a';
         
            printf("Instruction: You have to insert a number from 1 to 100 and I have to guess it.\n"
                    "But you have to help me saying me if my guess is higher typing 'h'\n"
                    "or 'l' otherwise.\n\n");
         
            printf("Please insert a number:\n");
            num *scanf("%d", &num);
         
              srand(time(NULL)); // initialize random number generator
              guess = rand()%100 +1;
         
            while(guess != num)
        {
            printf("The secret number is Higher or Lower than my guess %i?\n",guess);
         
            answ=getchar();
         
             if (answ=='l')
             {
                height=guess;
                srand(time(NULL)); // initialize random number generator
                guess = rand()%(height-1-bottom) +(bottom+1);
             }
         
             if (answ=='h')
             {
                bottom=guess;
                srand(time(NULL)); // initialize random number generator
                guess = rand()%(bottom-height-1) +(bottom+1);
             }
         
        }
         
            printf("I found it! It's %i",guess);
            return 0;
        }
    Last edited by Salem; 04-15-2014 at 03:55 AM. Reason: Inline 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
    38,471
    > answ=getchar();
    This gets only a single char.
    You also need to get the \n which is also typed, and make sure you ignore it.

    Other points.
    You should only call srand() once. Calling it multiple times doesn't make it any more random, but could make it significantly less random.

    > num *scanf("%d", &num);
    Just write
    scanf("%d", &num);
    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
    Registered User Scantymix's Avatar
    Join Date
    Apr 2014
    Location
    Padova (IT)
    Posts
    4
    Before using answ=getchar(); i tried to use scanf(%s, &answ); but I noticed that when I replay with letter the variable num turn to 0 and because of this i won't go out from the while. Can you explain me why num variable changes?
    Last edited by Scantymix; 04-15-2014 at 04:14 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,471
    Let's review what you might type in for a number, and the first high/low response

    42\n
    h\n


    Most scanf conversions happily ignore leading \n, and will stop at trailing \n.

    getchar() on the other hand deals exactly with the next char of input.

    At the moment, all the \n are unprocessed by your code. You need to take into account where they are in the input stream and deal with them appropriately.
    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.

  5. #5
    Registered User Scantymix's Avatar
    Join Date
    Apr 2014
    Location
    Padova (IT)
    Posts
    4
    Still i didn't get why if i use answ=getchar() instead of scanf(%s, &answ) in the screen will appear two time the same sentence. Maybe I don't understand how to manage proprely \n symbol in the code. Can you say me how to modify this part of the code?

    printf("The secret number is Higher or Lower than my guess %i prova %i?\n",guess,num);


    scanf("%s ", &answ);
    Last edited by Scantymix; 04-15-2014 at 05:15 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,471
    Because (as I said previously), the %s conversion ignores leading \n

    So having typed in
    42\n
    h\n

    Then
    scanf("%d", &num);
    will consume 42 and leave the \n

    Then scanf with %s will skip the initial \n, consume "h" and then leave the next \n.

    In fact, this is probably a lot better for you.
    scanf(" %c", &answ);
    Note very carefully the initial space in this format string.
    It tells scanf to skip all leading newlines, tabs and spaces and place the next non-space character (say 'h') into the answ char variable.
    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.

  7. #7
    Registered User Scantymix's Avatar
    Join Date
    Apr 2014
    Location
    Padova (IT)
    Posts
    4
    Now it works perfectly! Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with printing double numbers
    By stdq in forum C Programming
    Replies: 7
    Last Post: 01-28-2012, 03:08 AM
  2. Why is this not printing to screen?
    By lilbo4231 in forum C Programming
    Replies: 2
    Last Post: 03-07-2011, 03:10 PM
  3. Printing \n on the screen in C
    By swgh in forum C Programming
    Replies: 4
    Last Post: 05-18-2007, 03:52 PM
  4. Problem With search a file and printing to screen
    By sell682 in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2004, 05:55 AM
  5. Double Screen Laptops
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-19-2002, 04:34 PM