Thread: Need help with counting vowels prog

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    10

    Question Need help with counting vowels prog

    ok so i have to write a program that counts the # of vowels from the file. my only problem is i know how to get the total but i cant figure out how to get the total number of a/A's, e/E's, and so on. need help asap.....the following is what i have so far:

    Code:
    #include <iostream>  
    #include<fstream>  
    
    using namespace std;      
    int main( )  
    {  
    ifstream inFile;   
    
    char c;  
    int nNum = 0;   
    
    inFile.open("j://cppproj 3.txt");  
    
    while(!inFile.eof())  
    
    {  
    inFile.get(c);  
    
    if (c == 'a' || c == 'e' ||  c == 'i' ||  c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')  
    	
    nNum++;  
    }  
    
    inFile.close();  
    
    cout << "The total number of vowels is " << nNum << ".\n";  
    return 0;  
    
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    So you figured out that putting all those terms in the long OR condition will count all the vowels, and forgot how to break it apart again.

    Come on man, it'll hit you if you just think a bit.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Here's one way to do it:

    Code:
    #include <iostream>  
    #include<fstream>  
    
    using namespace std;      
    int main( )  
    {  
    ifstream inFile;   
    
    char c;  
    int numOfVowels = 0;   
    int numOfAs = 0;
    int numOfEs = 0;
    int numOfIs = 0;
    int numOfOs = 0;
    int numOfUs = 0;
    
    inFile.open("j://cppproj 3.txt");  
    
    while(!inFile.eof())  
    
    {  
    inFile.get(c);  
    
    switch (c) {
        case 'A':
            numOfAs++;
            break;
        case 'a':
            numOfAs++;
            break;
        case 'E':
            numOfEs++;
            break;
        case 'e':
            numOfEs++;
            break;
        case 'I':
            numOfIs++;
            break;
        case 'i':
            numOfIs++;
            break;
        case 'O':
            numOfOs++;
            break;
        case 'o':
            numOfOs++;
            break;
        case 'U':
            numOfUs++;
            break;
        case 'u':
            numOfUs++;
            break;
    }
    	
    numOfVowels++;  
    }  
    
    inFile.close();
    
    cout << "The total number of vowels is " << numOfVowels << ".\n";  
    cout<< "The total number of A's is: " << numOfAs <<endl;
    cout<< "The total number of E's is: " << numOfEs <<endl;
    cout<< "The total number of I's is: " << numOfIs <<endl;
    cout<< "The total number of O's is: " << numOfOs <<endl;
    cout<< "The total number of U's is: " << numOfUs <<endl;
    return 0;  
    
    }
    Last edited by Programmer_P; 07-09-2010 at 07:31 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    10

    hey

    thank you sooooo much this really help alot

  5. #5
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    Code:
    char toUpper(char *c)
    {
        return((*c >= 0x61 && *c <= 0x7A) ? *c &= 0xDF : *c);
    }
    You guys seems to be missing this...

    You shouldnt give him a complete example when he was so close to what he wanted... He just had to think a little longer... programmers need to think by themselfs.
    Last edited by Dante Wingates; 07-10-2010 at 04:03 PM.
    2B OR !2B? That is the question!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dante Wingates View Post
    You guys seems to be missing this...
    Just as you've missed the existence of a standard function for conversion to upper case, and the fact that your function assumes a specific (type of) character set.

    Of course, the examples here presume the english language as well.

    The actual definition of a vowel is a bit deeper than "in the set aeiou".

    The phonetic definition of a vowel is a sound in spoken language pronounced with an open vocal tract so that there is no build-up of air pressure at any point above the glottis. This contrasts with consonants where there is a constriction or closure at some point along the vocal tract.

    The phonological definition of a vowel is "sound that forms a peak of a syllable" (the 'a' in "cat" is the peak).

    Technically, there is sometimes conflict between these definitions. For example, 'y' and 'w' (as pronounced in the English words "yes" and "wet") are produced without much constriction of the vocal tract so, phonetically they can be considered to be vowels but phonologically they are not pronounced at the peak.

    If you want a real challenge, try to write a program that counts vowels - by one of these definitions - that works for any spoken or written language
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56

    Wink

    Quote Originally Posted by grumpy View Post
    Just as you've missed the existence of a standard function for conversion to upper case, and the fact that your function assumes a specific (type of) character set.
    No, Im completly aware of that. The fact that I wrote a simple function was simply because I felt like it.

    Im not missing the existence of standard functions, but I often encourage people to write their own functions, that is a matter of opinion. But you've missed one thing: It was not written to convert any character set nor only vowels to upper case. This simple function was written with the ascii character set in mind, so it works with it. Complaining about that is the same as saying that a ps3 should run xbox 360 games simply because both consoles were made to run games.

    Of course, the examples here presume the english language as well.

    The actual definition of a vowel is a bit deeper than "in the set aeiou".

    The phonetic definition of a vowel is a sound in spoken language pronounced with an open vocal tract so that there is no build-up of air pressure at any point above the glottis. This contrasts with consonants where there is a constriction or closure at some point along the vocal tract.

    The phonological definition of a vowel is "sound that forms a peak of a syllable" (the 'a' in "cat" is the peak).

    Technically, there is sometimes conflict between these definitions. For example, 'y' and 'w' (as pronounced in the English words "yes" and "wet") are produced without much constriction of the vocal tract so, phonetically they can be considered to be vowels but phonologically they are not pronounced at the peak.

    If you want a real challenge, try to write a program that counts vowels - by one of these definitions - that works for any spoken or written language
    ... So? Again, the function was written to convert any ascii character set lower case letter to its upper case representation, not only its vowels. My first language is portuguese(português) for Im brasilian. You can safely say that Im aware of that better than many english speakers, but it should come as no surprise that Im not worried with that now.

    Now try this and tell me if it is not doing what it is supposed to

    Code:
    #include <iostream>
    
    char toUpper(char c)
    {
        return((c >= 0x61 && c <= 0x7A) ? c &= 0xDF : c);
    }
    
    int main(int argc, char **argv)
    {
        for(char c = 0x20; c <= 0x7E; c++)
            std::cout << toUpper(c) << (char)0x20;
        std::cin.get();
        return(0);
    }
    let me know if it is printing any lower case letter.
    2B OR !2B? That is the question!

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Dante Wingates View Post
    No, Im completly aware of that. The fact that I wrote a simple function was simply because I felt like it.

    Im not missing the existence of standard functions, but I often encourage people to write their own functions, that is a matter of opinion.
    So you recommend people to waste their time reinventing the wheel, instead of using a solid, proven, readily available solution. Time that could instead be spent performing something useful.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MWAAAHAAA
    So you recommend people to waste their time reinventing the wheel, instead of using a solid, proven, readily available solution.
    Plus reinventing the wheel with the poor use of magic numbers and assumptions (however warranted) that remain undocumented until challenged...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    inFile.open("j://cppproj 3.txt");  
    
    while(!inFile.eof())
    Don't use eof to control a loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-11-2008, 12:50 AM
  2. Counting Vowels within a string.
    By patso in forum C Programming
    Replies: 12
    Last Post: 04-09-2008, 04:21 PM
  3. Counting Characters HELP !!
    By Zozo17 in forum C Programming
    Replies: 2
    Last Post: 03-01-2005, 08:00 PM
  4. counting vowels
    By trippedwire in forum C++ Programming
    Replies: 10
    Last Post: 10-01-2004, 11:58 PM
  5. Terrible Counting prog
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2002, 07:48 PM