Basic question - How to wait before asking another input?

This is a discussion on Basic question - How to wait before asking another input? within the C Programming forums, part of the General Programming Boards category; Hi everyone, Within a function A, I have a printf that asks for a login and another one that asks ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    15

    Basic question - How to wait before asking another input?

    Hi everyone,

    Within a function A, I have a printf that asks for a login and another one that asks for a password.

    When I call that function from the main function, it works... it waits for me to insert the first and then the second.

    The problem occurs when I call the function A from another function B, it goes directly to the second printf without letting me to insert the first one.

    Here is part of the code:
    Code:
    printf("Login:\n");
    fgets(login,sizeof(login),stdin);
    
    printf("Password:\n");
    fgets(password,sizeof(password), stdin);
    Any ideas?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    There's probably a newline leftover in the input stream, but to be sure post the code in the calling function.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    Hi itCbitC,

    It gives me the same problem!

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    I've made some more tests.

    If if call it this way (within the main function):

    Code:
    goToLogin();
    it goes without problems.

    But with something like this (within the main function):

    Code:
    .
    .
    .
    case 'L':
    if(goToLogin())
      printf("sucess\n");
    .
    .
    .
    It happens the same problem.. It shows both printfs at the same time and I can only enter one input.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    There's probably a newline leftover in the input stream, but to be sure post the code in the calling function.
    Okay, do you understand the issue bithub is trying to indicate?

    STDIN pitfalls
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    To be honest, No.

    I'm new to this stuff.

    Can you explain?

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    int function, returns 1 in case of sucess

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by xyphen View Post
    To be honest, No.

    I'm new to this stuff.

    Can you explain?
    Yeah, it is the #1 most common beginner issue here. Which is why I wrote this:

    STDIN pitfalls

    That is not for sure the problem, but there's a good chance that it is. Read it and get back to us.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you get a char from the user, the newline char '\n' is left behind on the input stream.

    In your first example, you don't get a char from the user first, so there is no newline in the buffer. In the second example, you do - so you have one.

    Pull the newline out of the buffer, after the char input, and see if that doesn't fix your problem:

    junkVariable = getchar();

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    (reading your post right now)

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Did you try it? Nothing sucks more than ignoring something only to find out that it was indeed the cause.
    Just add a getchar() call between the reads, or perhaps post some of your code that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    OK i got it! I'm just starting on this so sorry for my incompetence.

    getchar(); between the first printf and fgets solved the problem

    Thanks everyone for the great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  3. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 04:06 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. fstream char array input question
    By mcdms in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21