Thread: Printf printing twice

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    2

    Question Printf printing twice

    Hello, I'm trying to return to the start by using "goto start:"
    but everytime it goes to the start it prints the menu twice.

    First run of the program is fine, I go to selection 'a' and when it finishes executing, "goto start:" goes to start, but then prints the menu twice. any idea whats going on?

    Code:
    #include <stdio.h>
    #include <math.h>
    main() {
    start:;
        char  selection, a, b, c;
        printf("Math Practice\n\n");
        printf("a. Multiplication\n");
        printf("b. Division\n");
        printf("c. Exit\n\n");
        printf("Select a,b or c\n");
        printf("Selection ---> ");
        scanf("%c", &selection);
        fflush(stdin);
    
    
        //selection A
        if(selection == 'a') {
            float firstNum, secNum, correct, total;
            printf("Please enter your first number ---> ");
            scanf("%f", &firstNum);
            printf("\nPLease enter your second number ---> ");
            scanf("%f", &secNum);
            printf("\nEnter the correct answer to the first and second number ---> ");
            scanf("%f", &correct);
            total=firstNum*secNum;
            if(correct == total) {
                printf("\ncorrect!\n\n");
            } else {
                printf("wrong! the correct answer is %.0f\n\n", total);
            }
            goto start;
        }
    
    
    
    
        //selection b
        else if(selection == 'b') {
            printf("sdkbfhewhjbfdsjbhfsjb");
            goto start;
        }
    
    
    
    
        //selection c
        else if(selection == 'c') {
            printf("goodbye :)");
        }
    
    
        else {
            printf("Choose a proper selection.\n\n");
            goto start;
        }
    
    }
    Ive noticed that on the first print of the menu it says "choose a proper selection" what's up with this? because that's what's making it print twice.

    Printf printing twice-capture2-png

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by travis9090
    I'm trying to return to the start by using "goto start:"
    You should be using a loop statement instead of a goto statement with a label.

    Quote Originally Posted by travis9090
    First run of the program is fine, I go to selection 'a' and when it finishes executing, "goto start:" goes to start, but then prints the menu twice. any idea whats going on?
    The reason is that scanf reads the input that you entered, but leaves the newline from entering the input in the input buffer. When you next call scanf with %c, it then reads that newline instead of the character that you want to enter.

    You appear to be using fflush(stdin) to solve this, but you placed it in the wrong place, and furthermore, fflush is only defined for output streams and update streams for which the last operation was output, so fflush(stdin) results in undefined behaviour. There are a few ways to solve this, e.g., insert a space at the right place in a scanf format string so that it will match the newline (whitespace) and discard it, or write a loop to read and discard characters until a newline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    2
    Quote Originally Posted by laserlight View Post
    You should be using a loop statement instead of a goto statement with a label.


    The reason is that scanf reads the input that you entered, but leaves the newline from entering the input in the input buffer. When you next call scanf with %c, it then reads that newline instead of the character that you want to enter.

    You appear to be using fflush(stdin) to solve this, but you placed it in the wrong place, and furthermore, fflush is only defined for output streams and update streams for which the last operation was output, so fflush(stdin) results in undefined behaviour. There are a few ways to solve this, e.g., insert a space at the right place in a scanf format string so that it will match the newline (whitespace) and discard it, or write a loop to read and discard characters until a newline.
    I am quite new to C, and well.. programming itsself. Could you show/rewrite the way to fix this? then I can see how its done. Also when you say "I should be using a loop statement", what loop statement and how?

    Any help would be highly appreciated. Any great videos explaining more about the loops in C you are talking about would be fantastic also.

    Im new to programming, this site, and trying to learn.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by travis9090
    I am quite new to C, and well.. programming itsself. Could you show/rewrite the way to fix this? then I can see how its done. Also when you say "I should be using a loop statement", what loop statement and how?
    Don't you have learning materials that teach you how to program in C, including how to use if statements and loop statements like a while loop?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's a poor book/tutor that shows you how to (ab)use goto before introducing loops.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf printing twice?
    By drshmoo in forum C Programming
    Replies: 5
    Last Post: 03-19-2011, 02:41 AM
  2. Printf not printing all data
    By murjax in forum C Programming
    Replies: 24
    Last Post: 07-20-2009, 09:33 PM
  3. Printing pointers with printf()
    By blacknail in forum C Programming
    Replies: 2
    Last Post: 05-25-2008, 08:15 AM
  4. printf not printing
    By deadhippo in forum C Programming
    Replies: 5
    Last Post: 05-04-2008, 12:36 AM
  5. Printf float printing
    By judgex in forum C Programming
    Replies: 3
    Last Post: 12-08-2007, 02:47 AM