Thread: counting vowels

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    14

    counting vowels

    After this program reads in a file, I want it to count all the vowels in the file and then output how many vowels there are. Right now it runs without any errors, but it always gives the number of vowels as 0. I'm guessing I need to add a for loop or something somewhere in the function?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    bool isVowel(char ch);
    
    int main()
    {
    
    int vowel = 0;
    int total = 0;
    
    
    ifstream inFile;
    char inChar = ' ';
    string inputFile;
    
    cout << "Enter the name of the text file: ";
    cin >> inputFile;
    
    inFile.open(inputFile.data());
    if (!inFile.is_open())
    {
    cout << "input file did not open properly" << endl;
    return 1;
    }
    
    for (;;)
    {
    if (inFile.eof())
    {
    break;
    }
    inFile.get(inChar);
    cout << inChar;
    }
    
    
    
    isVowel(ch); 
    cout << " The number of vowels is " << vowel << endl;
    
    return 0;
    } 
    
    bool isVowel(char ch)
    {
    int vowel = 0;
    switch(ch)
    
    {
    
    case 'A': case 'E': 
    
    case 'I': case 'O': 
    
    case 'U': case 'Y':
    
    case 'a': case 'e': 
    
    case 'i': case 'o': 
    
    case 'u': case 'y': 	
    
    return true;
    vowel++;
    default: 
    return false;
    
    }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well the int vowel you increment in the function is not the same int vowel declared in main(), which remains untouched.

    Try a simple
    if ( isVowel(ch) ) vowel++;
    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.

  3. #3
    i found it difficult to follow some of your code
    so i wrote a simple one real quick and if you can follow it maybe you will see something to help you out.


    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    int main()
    {
    	FILE * myfile;
    	myfile = fopen("myvowelfile.txt","r");
    	int myVowel;
    	int numofvowels = 0;
    	myVowel = fgetc(myfile);
    	while(myVowel != EOF)
    	{
    		if(myVowel == 'A' || myVowel == 'a')
    		{
    			numofvowels++;
    		}
    		if(myVowel == 'E' || myVowel == 'e')
    		{
    			numofvowels++;
    		}
    		if(myVowel == 'I' || myVowel == 'i')
    		{
    			numofvowels++;
    		}
    		if(myVowel == 'O' || myVowel == 'o')
    		{
    			numofvowels++;
    		}
    		if(myVowel == 'U' || myVowel == 'u')
    		{
    			numofvowels++;
    		}
    		if(myVowel == 'Y' || myVowel == 'y')
    		{
    			numofvowels++;
    		}
    		myVowel = fgetc(myfile);
    	}
    
    	cout << "You have " << numofvowels << " Vowels in your file!" << endl;
        getch();
    	return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    i want the function to count the number of vowels and the main part to print out the results. tried if ( isVowel(ch) ) vowel++; but it still prints out 0.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    bool isVowel(char ch);
    char ch;
    int main()
    {
    	
    	
    	int vowel = 0;
    	
    	ifstream inFile;
    	char inChar = ' ';
    	string inputFile;
    	
    	cout << "Enter the name of the text file: ";
    	cin >> inputFile;
    	
    	inFile.open(inputFile.data());
    	if (!inFile.is_open())
    	{
    		cout << "input file did not open properly" << endl;
    		return 1;
    	}
    	
    	for (;;)
    	{
    		if (inFile.eof())
    		{
    			break;
    		}
    		inFile.get(inChar);
    		cout << inChar;
    	}
    	
    	isVowel(ch); 
    	cout << "The number of vowels is " << vowel << endl;
    	
    	return 0;
    } 
    
    bool isVowel(char ch)
    {
    	int vowel = 0;
    	switch(ch)
    		
    	{
    		
    	case 'A': case 'E': 
    		
    	case 'I': case 'O': 
    		
    	case 'U': case 'Y':
    		
    	case 'a': case 'e': 
    		
    	case 'i': case 'o': 
    		
    	case 'u': case 'y': 	
    		return true;
    	default: 
    		return false;
    	}
    	
    	
    	if (isVowel(ch)) vowel++;
    	
    }

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    See if you can follow this.

    Code:
    #include <iostream>
    using namespace std;
    
    // Function returns true if vowel
    bool is_vowel ( char the_char )
    {
    
    	// Check for any case of vowels
    	switch ( the_char ) {
    
    		case 'A': 
    		case 'a':
    		case 'E':
    		case 'e':
    		case 'I':
    		case 'i':
    		case 'O':
    		case 'o':
    		case 'U':
    		case 'u':
    
    			return true;
    
    		default: return false;
    
    	}
    
    }
    
    int main ()
    {
    
    	char the_string [] = "Hello World";
    	int vowel = 0;
    
    	// Loop for the length of the string to check all of the characters
    	for ( int loop = 0; loop < strlen ( the_string ); loop++ ) {
    
    		// If there is a vowel increase the counter
    		if ( is_vowel ( the_string [loop] ) ) vowel++;
    
    	}
    
    	// Output: There are 3 vowels in "Hello World"
    	cout << "There are " << vowel << " vowels in \"" << the_string << "\"";
    
    	return 0;
    
    }
    Your main roblem I believe was the scope of your variables. The only counter you increased was trapped in the function.
    Last edited by Vicious; 10-01-2004 at 04:42 PM.
    What is C++?

  6. #6
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    bool isVowel(char ch);
    int main()
    {
    	int vowel = 0;
    	
            ifstream inFile;
    	char inChar = ' ';
    	string inputFile;
    	
    	cout << "Enter the name of the text file: ";
    	cin >> inputFile;
            cin.ignore(1);//ignore the \n
    	
    	inFile.open(inputFile.c_str());
    	if (!inFile.is_open())
    	{
    		cout << "input file did not open properly" << endl;
                    cin.get();
    		return 1;
    	}
    	
    	for (;;)
    	{
    		if (inFile.eof())
    		{
    			break;
    		}
    		inFile.get(inChar);
                    if(isVowel(inChar)) vowel++;//put it here
    		cout << inChar;
    	}
    
    	cout << "The number of vowels is " << vowel << endl;
            cin.get();//give the user the time to read output
    	
    	return 0;
    } 
    
    bool isVowel(char ch)
    {
    	switch(ch)
    		
    	{
    		
    	case 'A': case 'E': 
    		
    	case 'I': case 'O': 
    		
    	case 'U': case 'Y':
    		
    	case 'a': case 'e': 
    		
    	case 'i': case 'o': 
    		
    	case 'u': case 'y': 	
    		return true;
                    break;
    	default: 
    		return false;
                    break;
    	}
    	
    }

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I don't know where to begin. You need to work a little bit on your understanding of program flow. A program needs more than all of the parts to work, the parts must be put together in the right way to work together.

    First, you have a loop that reads in the file one char at a time. Inside that loop, all you do is output that char. The variable inChar only holds the value until the next time through the loop when it is overwritten with the next value read in. So really, you are reading in the entire file and doing nothing with it. The point of the loop would be to read in each character, and check each character if it is a vowel, and if it is, add one to the count of the number of vowels.

    The problem is that you don't call the isVowel function until after the entire file has been read in. That function will not be called inside the for loop unless you put it inside the for loop.

    Now, inside isVowel function, you have a switch statement. The code will always execute something from the switch statement, because you have a default case that handles everything that isn't a vowel. That means that every time the code in the function is executed, either the return true; or the return false; will be executed. That means that your function will always return and be finished there. The extra line of code: if (isVowel(ch)) vowel++; will never be executed.

    The key is to view the program as a whole in a logical order. Imagine you are the computer, and you are doing the things the program tells you to do. This is how your current program would look:
    1. Create a char variable named ch.
    2. Start the main function.
    3. Create an int variable named vowel and give it a value of 0.
    4. Create an ifstream variable called infile, a char variable called inChar (initialized to ' ') and a string variable called inputFile.
    5. Ask the user for the name of the file.
    6. Read in the first word entered by the user and store it in the variable inputFile.
    7. Open the file using the ifstream inFile. (Note: that should be c_str(), not data()).
    8. If the file fails to open, then output an error message and exit the main function. Program DONE. Otherwise continue.
    9. Do the following steps forever:
      1. If the input file has reached eof, break out of the forever loop. (Note: you should check for eof after you try to read in a character but before you try to use that character).
      2. Otherwise get the next character from the file.
      3. Output the character to the screen.
      4. Continue the loop.
    10. Call the isVowel function. Pass it the data that is in the ch variable (which is currently the nul character because the ch variable refers to the global variable created in step 1).
    11. In the isVowel function, create a variable called vowel and initialize it to 0.
    12. Look at the character passed in. If it is a vowel, return true. Otherwise return false.
    13. Exit the function isVowel with either true or false (in this case false because the variable ch will always be not a vowel).
    14. Back inside the main function, ignore the true or false returned from the isVowel function.
    15. Output the value of the vowel variable created in step 3 (which is different than the one created in step 11). That value has not changed, and so it will always be 0.
    16. Exit the main function, returning a value of 0.
    17. Program done, tell the system that the program exited and returned a value of 0.
    Hopefully you can see why it always outputted 0. Now look at the solutions provided by others above and see if you can follow the program flow they provided, and see where it is different and more effective than yours.

    Good Luck!

  8. #8
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    Try this:
    1)create a char array that holds all the characters in the file, call it "ch" or something.
    2)create an integer which is equal to the number of characters in the file:
    while (!.eof)
    {
    int b = 0;
    b++;
    }
    //then
    int score;
    for (int i = 0, i<b, i++)
    {
    if (ch[i] == "a" || ch[i] == "e" || ch[i]=="i" || ch[i] == "o" || ch[i] == "u")
    {
    score++;
    }
    if (score >0)
    {
    return score;
    }
    elseif (score==0)
    {
    cout<<"No vowels"<<endl;
    return 0;
    }


    This is a usefull trick I discovered while making a program that prints prime numbers.

    Hope this helps

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    One question: Why has nobody using switch...case used the tolower() function to save you from having to trap lower and upper case letters? Is it a taboo, or just something nobody does?

  10. #10
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    To be honest it never crossed my mind.

    I suppose that would be ok. I am now also wondering if it is a no-no...
    What is C++?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > tried if ( isVowel(ch) ) vowel++; but it still prints out 0.
    Because you chose the wrong one

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    bool isVowel(char ch);
    //char ch;
    int main()
    {
    	
    	
    	int vowel = 0;
    	
    	ifstream inFile;
    	char inChar = ' ';
    	string inputFile;
    	
    	cout << "Enter the name of the text file: ";
    	cin >> inputFile;
    	
    	inFile.open(inputFile.data());
    	if (!inFile.is_open())
    	{
    		cout << "input file did not open properly" << endl;
    		return 1;
    	}
    	
    	for (;;)
    	{
    		if (inFile.eof())
    		{
    			break;
    		}
    		inFile.get(inChar);
    		cout << inChar;
    		if (isVowel(inChar)) vowel++;  // HERE!!!!!!
    	}
    	
    	// HUH??? isVowel(ch); 
    	cout << "The number of vowels is " << vowel << endl;
    	
    	return 0;
    } 
    
    bool isVowel(char ch)
    {
    	int vowel = 0;
    	switch(ch)
    		
    	{
    		
    	case 'A': case 'E': 
    		
    	case 'I': case 'O': 
    		
    	case 'U': case 'Y':
    		
    	case 'a': case 'e': 
    		
    	case 'i': case 'o': 
    		
    	case 'u': case 'y': 	
    		return true;
    	default: 
    		return false;
    	}
    	
    	// NOT HERE!!!!
    	// if (isVowel(ch)) vowel++;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-11-2008, 12:50 AM
  2. Counting up(and down) using recursive function. Please help.
    By MarkSquall in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2008, 04:26 AM
  3. Counting Vowels within a string.
    By patso in forum C Programming
    Replies: 12
    Last Post: 04-09-2008, 04:21 PM
  4. Counting Characters HELP !!
    By Zozo17 in forum C Programming
    Replies: 2
    Last Post: 03-01-2005, 08:00 PM
  5. Counting using Lib Cstring
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 05:51 PM