Loop Problem

Discussion in 'C Programming' started by Walter Roberson, May 17, 2005.

  1. In article <8da2c$428a0cad$54705512$>,
    Markus Pitha <> wrote:
    >My program should ask for repeating a calculation or not. I want to
    >solve it with a do..while, but the weird thing is that the program
    >totally ignores the scanf line where I want to receive the user input.
    >It starts the loop again by itself without receiving any input.


    >char quit;
    >
    > do {
    > get_vars(pointer_on_m, results);
    > calculate(pointer_on_m, results);
    >
    > printf("\n\rAnother calculation? (n for quit): ");
    > scanf("%c", &quit);
    > printf("\n\r");
    > }
    > while (quit != 'n');


    Where is your EOF handling?

    What -exactly- is your input? Is it *really* just the character 'n',
    or is the user entering an 'n' followed by a return/enter? Where is
    the input stream positioned after you read the first character?
    --
    "Mathematics? I speak it like a native." -- Spike Milligan
     
    Walter Roberson, May 17, 2005
    #1
    1. Advertising

  2. In article <82164$428a11a6$54705512$>,
    Markus Pitha <> wrote:
    >Walter Roberson schrieb:
    >> Where is your EOF handling?


    >my End of File handling? What exactly do you mean?


    What will happen with your code if the user enters the
    end-of-file sequence instead of further y/n inputs, or if the
    input is redirected from a source that runs out of data?
    What value will be stored into the character variable by the
    scanf() if there is no more data?
    --
    Any sufficiently advanced bug is indistinguishable from a feature.
    -- Rich Kulawiec
     
    Walter Roberson, May 17, 2005
    #2
    1. Advertising

  3. In article <82164$428a11a6$54705512$>,
    Markus Pitha <> wrote:

    >Walter Roberson schrieb:


    >> What -exactly- is your input? Is it *really* just the character 'n',
    >> or is the user entering an 'n' followed by a return/enter? Where is
    >> the input stream positioned after you read the first character?


    >I cannot even enter anything. The program shows the printf ...(n to
    >quit) line at the end of the first program cycle, but the first function
    >(of these 2) already started again and I was not even able to enter
    >anything.


    Let me guess: you have at least one input occuring before this point.
    Read the scanf() documentation with regard to "trailing white space".

    --
    Entropy is the logarithm of probability -- Boltzmann
     
    Walter Roberson, May 17, 2005
    #3
  4. Markus Pitha wrote:
    > Walter Roberson schrieb:
    >
    >>Let me guess: you have at least one input occuring before this point.
    >>Read the scanf() documentation with regard to "trailing white space".

    >
    >
    > Yes, in these two functions, I have some inputs, but only numbers, no
    > chars. I still wonder, why my problem works with numbers, but not with
    > chars. For C it must be no difference, isn't it?


    It's picking up the white space (probably the return character) after
    the numbers.

    If you type 23456, and then hit the return key, the return key is STILL
    waiting in the queue to be picked up by scanf. Now, if the next read is
    for a number, then it will skip over whitespace (including your return).
    But if it's just looking for a character, your return that you already
    typed in but is still waiting to be read will do just fine.

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
     
    Jonathan Bartlett, May 17, 2005
    #4
  5. Walter Roberson

    Chris Torek Guest

    In article <8da2c$428a0cad$54705512$>
    Markus Pitha <> wrote:
    >... the weird thing is that the program
    >totally ignores the scanf line ...


    Never, ever use scanf().

    (For advanced C programmers: rarely use scanf(). :) )

    See the comp.lang.c FAQ, questions 12.17 and 12.18a.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, May 17, 2005
    #5
  6. Walter Roberson

    Markus Pitha Guest

    Hi,
    My program should ask for repeating a calculation or not. I want to
    solve it with a do..while, but the weird thing is that the program
    totally ignores the scanf line where I want to receive the user input.
    It starts the loop again by itself without receiving any input.
    The funny thing is that it works with numbers. When I change the input
    to receiving numbers, the program waits on this part, but why not when I
    want to give a char as input?
    Here is this part:

    char quit;

    do {
    get_vars(pointer_on_m, results);
    calculate(pointer_on_m, results);

    printf("\n\rAnother calculation? (n for quit): ");
    scanf("%c", &quit);
    printf("\n\r");
    }
    while (quit != 'n');

    It would work like this, but why?:

    int quit;

    do {
    get_vars(pointer_on_m, results);
    calculate(pointer_on_m, results);

    printf("\n\rAnother calculation? (0 for quit): ");
    scanf("%d", &quit);
    printf("\n\r");
    }
    while (quit != 0);
     
    Markus Pitha, May 17, 2005
    #6
  7. In article <428a28af$>,
    Jonathan Bartlett <> wrote:
    :Markus Pitha wrote:
    :> Walter Roberson schrieb:

    :>>Let me guess: you have at least one input occuring before this point.
    :>>Read the scanf() documentation with regard to "trailing white space".

    :> Yes, in these two functions, I have some inputs, but only numbers, no
    :> chars. I still wonder, why my problem works with numbers, but not with
    :> chars. For C it must be no difference, isn't it?

    :It's picking up the white space (probably the return character) after
    :the numbers.

    :If you type 23456, and then hit the return key, the return key is STILL
    :waiting in the queue to be picked up by scanf.

    Exactly. Which the scanf() documentation makes clear. I was giving
    some pretty specific hints about exactly which sections of the FM that
    the OP should RT, but I was hoping to "teach to fish" rather than
    just giving the complete answer.
    --
    Would you buy a used bit from this man??
     
    Walter Roberson, May 17, 2005
    #7
  8. Walter Roberson

    Markus Pitha Guest

    Hello,

    Walter Roberson schrieb:
    > Where is your EOF handling?


    my End of File handling? What exactly do you mean?

    > What -exactly- is your input? Is it *really* just the character 'n',
    > or is the user entering an 'n' followed by a return/enter? Where is
    > the input stream positioned after you read the first character?


    I cannot even enter anything. The program shows the printf ...(n to
    quit) line at the end of the first program cycle, but the first function
    (of these 2) already started again and I was not even able to enter
    anything.
     
    Markus Pitha, May 17, 2005
    #8
  9. In article <b7037$428a2f47$54705512$>,
    Markus Pitha <> wrote:
    :Chris Torek schrieb:
    :> Never, ever use scanf().

    :Ok, so fgets would be the right solution, wouldn't it?

    Note, with fgets() you have to deal with the issue that there might
    not be a newline within the number of size of the buffer you provided.

    --
    History is a pile of debris -- Laurie Anderson
     
    Walter Roberson, May 17, 2005
    #9
  10. Walter Roberson

    Markus Pitha Guest

    Walter Roberson schrieb:
    > Let me guess: you have at least one input occuring before this point.
    > Read the scanf() documentation with regard to "trailing white space".


    Yes, in these two functions, I have some inputs, but only numbers, no
    chars. I still wonder, why my problem works with numbers, but not with
    chars. For C it must be no difference, isn't it?
     
    Markus Pitha, May 17, 2005
    #10
  11. Walter Roberson

    Markus Pitha Guest

    Hello,

    Chris Torek schrieb:
    > Never, ever use scanf().
    >
    > (For advanced C programmers: rarely use scanf(). :) )
    >
    > See the comp.lang.c FAQ, questions 12.17 and 12.18a.


    Ok, so fgets would be the right solution, wouldn't it?
     
    Markus Pitha, May 17, 2005
    #11
  12. Walter Roberson

    CBFalconer Guest

    Chris Torek wrote:
    > Markus Pitha <> wrote:
    >
    >> ... the weird thing is that the program
    >> totally ignores the scanf line ...

    >
    > Never, ever use scanf().
    >
    > (For advanced C programmers: rarely use scanf(). :) )
    >
    > See the comp.lang.c FAQ, questions 12.17 and 12.18a.


    The phrase "if (1 == scanf("%d", &integer))" is quite useful for
    getting numbers out of a stream. I prefer to encapsulate it in a
    function, together with a routine to do line flushing. But once
    you can't check performance with "1 == scanf" I agree with you.

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
     
    CBFalconer, May 17, 2005
    #12
  13. Walter Roberson

    pete Guest

    Markus Pitha wrote:
    >
    > Hello,
    >
    > Chris Torek schrieb:
    > > Never, ever use scanf().
    > >
    > > (For advanced C programmers: rarely use scanf(). :) )
    > >
    > > See the comp.lang.c FAQ, questions 12.17 and 12.18a.

    >
    > Ok, so fgets would be the right solution, wouldn't it?


    scanf is OK for string input, but not so good for anything else.
    Strings, fortunately can be easily parsed any way you like.

    new.c shows a more less bulletproof way of getting string input.
    The drawback is that it doesn't let you know if you've
    entered a line that's longer than your array.
    However, in situations such as this one, you don't care.

    C:\Program Files\DevStudio\SharedIDE\bin\Debug>new
    Another calculation? (n for quit): Yes please

    Another calculation? (n for quit): No thank you


    C:\Program Files\DevStudio\SharedIDE\bin\Debug>


    /* BEGIN new.c */

    #include <stdio.h>

    #define STRING_LENGTH 1
    #define str(s) # s
    #define xstr(s) str(s)

    int main(void)
    {
    char string[STRING_LENGTH + 1];
    int rc;

    do {
    printf("Another calculation? (n for quit): ");
    fflush(stdout);
    rc = scanf("%" xstr(STRING_LENGTH) "[^\n]%*[^\n]", string);
    if (!feof(stdin)) {
    getchar();
    }
    putchar('\n');
    } while (rc == 1 && *string != 'n' && *string != 'N');
    return 0;
    }

    /* END new.c */

    If rc is one then you have a string in the array.
    If rc isn't, then you don't.
    If rc is zero, that means that only a newline character was input.
    If rc is EOF, then you have an end of file condition.
     
    pete, May 17, 2005
    #13
  14. Walter Roberson

    Chico Guest

    Markus Pitha wrote:
    > Hi,
    > My program should ask for repeating a calculation or not. I want to
    > solve it with a do..while, but the weird thing is that the program
    > totally ignores the scanf line where I want to receive the user

    input.
    > It starts the loop again by itself without receiving any input.
    > The funny thing is that it works with numbers. When I change the

    input
    > to receiving numbers, the program waits on this part, but why not

    when I
    > want to give a char as input?
    > Here is this part:
    >
    > char quit;
    >
    > do {
    > get_vars(pointer_on_m, results);
    > calculate(pointer_on_m, results);
    >
    > printf("\n\rAnother calculation? (n for quit): ");
    > scanf("%c", &quit);
    > printf("\n\r");
    > }
    > while (quit != 'n');
    >
    > It would work like this, but why?:
    >
    > int quit;
    >
    > do {
    > get_vars(pointer_on_m, results);
    > calculate(pointer_on_m, results);
    >
    > printf("\n\rAnother calculation? (0 for quit): ");
    > scanf("%d", &quit);
    > printf("\n\r");
    > }
    > while (quit != 0);



    I had the same problem; I "fixed" it by writing:

    scanf("%s", &input);

    Although a lot of people here would eat me alive because that says to
    look for a string not a character.
     
    Chico, May 18, 2005
    #14
  15. Walter Roberson

    Grumble Guest

    Chico wrote:

    > scanf("%s", &input);
    >
    > Although a lot of people here would eat me alive because that says to
    > look for a string not a character.


    What type did you use for the variable 'input'?
     
    Grumble, May 18, 2005
    #15
  16. Walter Roberson

    Markus Pitha Guest

    Hi,
    thanks for the tip. I'll keep it in my mind.
     
    Markus Pitha, May 18, 2005
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,403
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,274
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    709
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    429
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page