Thread: Why do I have to press enter key twice the first time I call the function?

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    3

    Why do I have to press enter key twice the first time I call the function?

    Code:
    #include <stdio.h>
    
    
    char getInput(){
        char temp;
        while(temp != EOF && (temp=getc(stdin)) != '\n');
        temp = getc(stdin);
        return temp;
    }
    
    
    void main(){
        char input; 
    start:
        input = getInput();
        printf("%c\n",input);
        goto start;
    }
    Result:

    I had to hit enter key twice the first time, but there is no problem on the succeeding input prompts. Why does this happen? Is there a way to fix this? Please, help me. What am I doing wrong?

    Why do I have to press enter key twice the first time I call the function?-problem-png
    Last edited by sdtabot; 04-09-2020 at 09:56 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could get rid of that while loop in getInput. It looks like that's what's causing the problem.

    In main, you should use an explicit loop instead of a label with goto. You probably want to check for EOF in the condition of this loop. If your aim is to discard newlines, you can just check for them here and print if the character is not a newline.

    Also getInput should return an int because getc returns an int. getc returns an int in order to always allow you to correctly differentiate a valid char input from EOF. But if all you're really doing in getInput is calling getc, then you might as well call getc directly in main.

    Likewise, in main input should be an int.

    Change the return type of main to int.
    Last edited by laserlight; 04-09-2020 at 10:25 PM.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. main returns int.
    2. Don't use goto to jump backwards, pick one of the loops like while, for, do
    3. temp is a char, and needs to be an int to store and compare with EOF successfully.
    4. you don't initialise temp, so you're comparing with garbage the first time around the loop.
    5. you're calling getc twice each time

    Code:
    #include <stdio.h>
    
    
    char getInput(){
        int temp;
        while( (temp=getc(stdin)) != EOF && temp == '\n');
        return temp;
    }
    
    int main() {
        char input;
        do {
          input = getInput();
          printf("%c\n",input);
        } while ( input != 'q' );
    }
    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.

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    3

    Exclamation

    Quote Originally Posted by Salem View Post
    1. main returns int.
    2. Don't use goto to jump backwards, pick one of the loops like while, for, do
    3. temp is a char, and needs to be an int to store and compare with EOF successfully.
    4. you don't initialise temp, so you're comparing with garbage the first time around the loop.
    5. you're calling getc twice each time

    Code:
    #include <stdio.h>
    
    
    char getInput(){
        int temp;
        while( (temp=getc(stdin)) != EOF && temp == '\n');
        return temp;
    }
    
    int main() {
        char input;
        do {
          input = getInput();
          printf("%c\n",input);
        } while ( input != 'q' );
    }

    Thank you, Salem. It solved the problem. Though, I don't understand why I need to change the main to int when I only want to print the integer and not return any value. Please, tell me why.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    Quote Originally Posted by laserlight View Post
    You could get rid of that while loop in getInput. It looks like that's what's causing the problem.

    In main, you should use an explicit loop instead of a label with goto. You probably want to check for EOF in the condition of this loop. If your aim is to discard newlines, you can just check for them here and print if the character is not a newline.

    Also getInput should return an int because getc returns an int. getc returns an int in order to always allow you to correctly differentiate a valid char input from EOF. But if all you're really doing in getInput is calling getc, then you might as well call getc directly in main.

    Likewise, in main input should be an int.

    Change the return type of main to int.
    Thank you, laserlight. I changed the code based on your inputs. The reason I made a separate function is because I would like to use this in different functions and I would like to reduce my lines. I have 8 functions which uses it. So, with a getInput function, I managed to get 4 less lines than directly writing the 2 lines in each function to disregard the new line and prompt user input.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Though, I don't understand why I need to change the main to int when I only want to print the integer and not return any value. Please, tell me why.
    Because the standard says that main returns an int.
    Quote Originally Posted by C99 Standard
    5.1.2.2.3 Program termination
    If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;
    reaching the } that terminates the main function returns a value of 0.
    If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
    Underneath your actual program is the C runtime, which is the bit of code that runs after the OS has done loading your program, and before your code.
    Somewhere in there will be
    Code:
    result = main();  // call the user program
    if ( result ...
    If you say void, then there is no return value, and then garbage happens.

    Modern compilers will do a return 0 if you say nothing, but to be super sure, you should explicitly state one of the following:
    return 0;
    return EXIT_FAILURE;
    return EXIT_SUCCESS;
    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. Press Enter to go back, press 0 to exit
    By Deanz Tinio in forum C Programming
    Replies: 5
    Last Post: 06-07-2014, 01:01 AM
  2. Press Enter Twice
    By Alone882 in forum C++ Programming
    Replies: 14
    Last Post: 01-04-2011, 01:45 PM
  3. input function without a need to press Enter?
    By chao06 in forum C Programming
    Replies: 1
    Last Post: 06-19-2009, 03:18 PM
  4. Every time I press enter, prompt closes
    By printF in forum C Programming
    Replies: 2
    Last Post: 11-13-2005, 11:27 AM
  5. if i press enter
    By abbynormal87 in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2002, 05:37 PM

Tags for this Thread