trace loop

This is a discussion on trace loop within the C Programming forums, part of the General Programming Boards category; could someone trace the loop for me. i can't seem to get the correct answer 1 A 23 (3) while ...

  1. #1
    Unregistered
    Guest

    trace loop

    could someone trace the loop for me.
    i can't seem to get the correct answer

    1 A 23
    (3) while ((c1= getchar()) != '2')
    c2= c1; c1: _______ c2: _____
    c3= getchar();
    i= getchar(); c3: _______ i: _______

    the right answer is:
    (3) c1: '2' c2: ' '
    c3: '3' i: EOF

    thanks

  2. #2
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    why have you resorted to using heirogyphics in your mail??

    mayb its me
    but i cant make sense out of your code

    pls reformat it

  3. #3
    Unregistered
    Guest
    int i;
    ch c1,c2,c3;


    1 A 23

    while ((c1= getchar()) != '2') {
    c2= c1;
    c3= getchar();
    i= getchar();
    }



    the right answer is:
    c1: '2'
    c2: ' '
    c3: '3'
    i: EOF


    sorry about that thanks

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, for starters I can tell you that the answer you have there is wrong... Given that code you posted, well, it actually looks like an infinite loop, but it's kinda hard to tell. Try using this code instead...
    Code:
    while ((c1= getchar()) != '2')
       c2= c1;
    c3= getchar();
    i= getchar();
    The only difference is that I removed the brackets, but it makes a big difference, now only the c2 = c1 bit is in the body of the while loop, the rest executes afterwards. This code which I posted will evaluate to the results that you gave.

    Let us know if you need more help with this though.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

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