nested loop, simple but i'm missing it

This is a discussion on nested loop, simple but i'm missing it within the C Programming forums, part of the General Programming Boards category; > while(scanf(" %c", &letter) != '.') scanf returns the number of "conversions and assignments" or EOF Which in this case ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > while(scanf(" %c", &letter) != '.')
    scanf returns the number of "conversions and assignments" or EOF
    Which in this case gives you 0, 1 or EOF as possible results.
    None of which are ever likely to be equal to '.'

    Happy_Reaper's answer is pretty good.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #17
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    okay, so i understand that my

    Code:
    while(scanf(" %c", &letter) != '.')
    won't ever work out... i suppose my confusion comes from trying to write the loop without using the getchar() command (need to use scanf)... i need the program to terminate upon the input of solely a period, but i'm not seeing how to do so. i know this really isn't complicated, its just not clicking for some reason.

  3. #18
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    If you're so intent on using scanf, you can try using :

    Code:
    while(scanf("%c",%letter) == 1 && letter != '.')
    {
          //  Do loop as previously specified
    }
    I think that will work, as I believe statements in conditionals are evaluated left to right, but I don't know what the standard says about that.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Quote Originally Posted by Happy_Reaper
    If you're so intent on using scanf, you can try using :

    Code:
    while(scanf("%c",%letter) == 1 && letter != '.')
    {
          //  Do loop as previously specified
    }
    I think that will work, as I believe statements in conditionals are evaluated left to right, but I don't know what the standard says about that.
    It will work, but not because it's a conditional statement. Rather && is defined to be a sequence point, where the program will run the left side before the right side.

    Other sequence points include || (OR), '?:'(tertiary conditionl operator),','(comma operator*), and ';'(technically not an opperator).

    *Note that the comma is not an operator, and thus not a sequence point, when used in variable declaration and function calls. The most common use for the comma opperator is in the first line of a for loop.
    Last edited by King Mir; 10-23-2006 at 05:59 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Thank you for the specifictation.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Nested For Loop
    By snugy in forum C Programming
    Replies: 3
    Last Post: 02-17-2009, 09:17 AM
  2. Can I make a nested loop out of this?
    By RpgActioN in forum C++ Programming
    Replies: 9
    Last Post: 02-15-2005, 09:59 PM
  3. Nested Loop problem
    By chead in forum C Programming
    Replies: 7
    Last Post: 01-03-2005, 11:47 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM

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