Getting out of loop

This is a discussion on Getting out of loop within the C Programming forums, part of the General Programming Boards category; it is solved...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    Getting out of loop

    it is solved
    Last edited by sfff; 10-26-2009 at 02:54 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    For the first, you need to clear the input buffer after reading your data. Search the boards. It has been asked zillions of times.
    Secondly, I ask that you deduce the logic necessary. Write down the steps of your program in psuedo code, then let us see where we can go from there.
    You should be familiar with the different looping structures available.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    how could i "to clear the input buffer after reading your data" in this case where input always enter end the 'enter' key

    i searched in the forum but cant really understand the concept~

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    The thing is that when you use getchar, it first asks for input, which is stored inside a input buffer.
    First getchar returns the first character that was entered. Then the next. The last character in the input buffer will always be the enter character. We do not want that.
    So after reading the data, clear the input buffer to get rid of the enter key stored inside there. That way you can read more data next call.
    If you have read all data up to the enter key, a simply getchar() call will do to get rid of it.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    sorry but where should i put the getchar()...
    i am really new to C programming

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    As I tried to tell you in your previous thread (but I perhaps wasn't explicit enough), you shouold remove the three characters "ch=" from lines such as these:
    Code:
    	printf("%d ", ch=ch>>i|ch<<(8-i));
    That's because you don't actually want to modify ch, you only want to print the rotated value of it.
    This will also happen to fix your problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    Code:
    i=i++;
    This is wrong. If you want to add one to i, you have a number of options:
    Code:
    i = i + 1;
    i += 1;
    i++;
    among others. i = i++ is undefined, which means anything can happen. Practically speaking, it means you might not get the value you expect. Given the following:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int i = 1;
    
      i = i++;
    
      printf("%d\n", i);
    
      return 0;
    }
    I have three compilers which produce code that prints out 2, and two that print out 1. Both outputs are valid because the program's behavior is undefined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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