Thread: infinite loop

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    Havelock, NC
    Posts
    2

    infinite loop

    I wrote this program to read a standard input file,
    eliminate all vowels and write the consonants to an
    output file. My problem is once the input file is
    read the program keeps running only returning a
    "yyyyyy....etc" character (seems to be an infinite
    loop). How can I make this program end once the characters in the input file are read? Thanks.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int is_vowel(int c);
    
    int main(void)
    {
           int c;
           FILE *ifp, *ofp;
    
           ifp = fopen("textin.txt", "r");
           ofp = fopen("textout.txt", "w");
    
           while ((c = getc(ifp)) != 'EOF') 
           {
              if (!is_vowel(c)) 
                {
                     putc(c, ofp);
                }
           }
      return 0;
    }
    
    int is_vowel(int c)
    {
      c = toupper(c);
    
    return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
           while ((c = getc(ifp)) != EOF)
    Works fine. You should listen to compiler-warnings.
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Havelock, NC
    Posts
    2
    Thanks for the help. I guess that I never really paid attention to the warnings. As long as I got "Linking, bulid succeeded" I guess I thought I was good. Just begining to understand the concepts.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by scudshouse
    Thanks for the help. I guess that I never really paid attention to the warnings. As long as I got "Linking, bulid succeeded" I guess I thought I was good. Just begining to understand the concepts.
    A basic rule is that, if a compiler complains it is because it (or, more accurately, the compiler writer) considers you have done something suspicious.

    Blindly ignoring warnings is a good way to get in trouble. Ignoring warnings is really only OK if you know what the warnings mean and are sure they do not indicate some real problem with your code.

    OTOH, running code through several compilers, and ensuring the code compiles cleanly (without error or warning) is quite helpful in creating code that is truly portable. Particularly if you tweak all of the compilers to ensure they complain about everything possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM