Thread: Trouble with while loop

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    7

    Trouble with while loop

    Hey I am new to programming altogether and have been following tutorials and writing basic programs etc. I am currently trying to make a program that takes an inputted text and gives the frequency of individual letters. However, my program seems to get stuck in the initial while loop and I it seems that no number of changes can get it to get out of the loop. Here is the code:

    Code:
     
    #include <stdio.h>
    #include <ctype.h>
    
    int main(int argc, char *argv[]) {
        int letters[26] = {0};
        int count = 0;
        int unused_count = 0;
        int i = 0;
    
        printf("Enter text you wish to see letter frequency for:\n");
        int current_letter = getchar();
    
        while(current_letter != EOF) {
            if(isalpha(current_letter)) {
                count++;
                letters[tolower(current_letter) - 'a']++;}
            else {
                unused_count++;}
            current_letter = getchar(); }
    
        printf("Frequencies:\n");
        while (i < 26) {
            printf("[%c] = %d = %lf%%\n", 'a' + i, letters[i], ((double)letters[i] / count));
            i++;
        }
        printf("Unused characters: %d", unused_count);
    
        getchar();
        return 0;
    If someone could point me in the right direction I would be very thankful.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Are you getting wrong number of unused characters?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Unless the user presses something (CTRL + D) on Linux to send EOF explicitly, the program will wait for more input. That's it. Maybe add "|| current_char != '\n" or press the key sequence when you're finished.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Brafil View Post
    Unless the user presses something (CTRL + D) on Linux to send EOF explicitly, the program will wait for more input. That's it. Maybe add "|| current_char != '\n" or press the key sequence when you're finished.
    I agree with the above; the code worked for me on Windows after adding missing final "}"
    Note: On windows Control-Z is EOF if I recall right. It worked for me.

    Tim S

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    7
    thanks for the responses.... i was using ctrl + D in windows when i needed to be using ctrl + z..... thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM