Thread: A "bug" in my counting vowels C program. Need advice, please help.

  1. #1
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Smile A "bug" in my counting vowels C program. Need advice, please help.

    Dear CProgramming administrators and members:

    Good day everyone. I hope everybody is in good health upon reading this thread. Well, here's the code I've done recently (just sort of trying though):

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #define ENTERKEY '\r'
    int main(){
      char buff[80];
      char letter;
      char temp[80];
      int count=0;
      clrscr();
      printf("Please enter characters:");
      do{
            letter = getche();
            letter = toupper(letter);
            if((letter=='A') | (letter=='E') | (letter=='I') | (letter=='O') | (letter=='U'))
                    count++;
            else
                    continue;
      }while(letter !=  ENTERKEY);
    
      printf("\n\nYou entered %d vowel letters.", count);
      getch();
      return 0;
    }
    The output seems fine, example, if I type apple then the output is You entered 2 vowel letters....but when I press Backspace, example I change my input to banana, then I noticed it just added the previous count to the vowels inside banana, Now there are five (5) vowel letters instead of just three (3). And if I press Backspace, the letters didn't erase, it will just be overwritten by my "other" input. I know the problem relies on the count if the program detected an A, E, I, O, U on the input, but I'm kind of stuck to the program, I mean, everytime I change leads to error, so I just hoping by consulting from this forum will help me how to solve my bugs, I will really appreciate any change you might feel on the code.

    Thank you so much guys and God bless everyone.

    Respectfully yours,


    MarkSquall
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure I understand -- once the program prints "You entered 2 vowel letters" that's all there is to it. The program stops. Now if you mean that you type "apple\b\b\b\b\bbanana\r" (i.e., no return until banana), then yes, it will tell you that you typed five vowels. That's a good thing, because you did in fact type five vowels. \b just gets added to the string, it doesn't actually erase previously entered letters.

    The point of getch and friends is that The User Is Not Allowed To Edit Their Input. If that's not what you want, don't use it.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You have buff and temp, which suggests you should read a line (using fgets), then count the vowels in the line.

    Oh, and | (bitwise-or) is very different from || (logical-or)
    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.

  4. #4
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Smile Thank you guys for the information.

    Dear tabstop and Salem,

    Thank you for the information you gave, I guess getch() is a NO-NO on my program, I'll try to change this based on your suggestions, but if someone is willing (just in case), I hope someone would correct my code in this thread, but I will also do what can I do.


    Thanks everyone. I really appreciate your comments/suggestions.


    Respectfully Yours,

    MarkSquall
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    They told you how to change it. Make an attempt. They could have just rewritten it the first time instead of replying, but that doesn't do anything for anyone.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
            else
                    continue;
    Does absolutely nothing useful in your particular code, as the loop will continue without this as well [actually, come to think of it, I think it would prevent the loop from ending when you hit enter - and if that is what you really want, perhaps you should use "do ... while(1)" instead to make it clear that this is what you want].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Talking Thanks guys for the information...

    Dear members and administrators,


    Thank you Mr. Salem for the fgets() function and the bitwise and logical OR, actually I didn't know what their difference until I research on them. I "fixed" my own bugs. Sigh...such a nice feeling. Thanks everyone. More to hear from you all.


    God bless you all.


    Respectfully Yours,

    MarkSquall
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Line Counting Program
    By EdanD in forum C Programming
    Replies: 5
    Last Post: 06-25-2004, 12:31 AM
  3. Chat program advice
    By gogo in forum C++ Programming
    Replies: 11
    Last Post: 01-04-2002, 10:45 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM