Word and Vowel Counting Program Help

Word and Vowel Counting Program Help

Page 1 of 1

6 Replies - 3366 Views - Last Post: 07 December 2009 - 11:35 AM Rate Topic: -----

#1 needcplusplushelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 08

Word and Vowel Counting Program Help

Posted 16 October 2008 - 09:19 PM

Hey guys, I'm trying to create a program that will count the number of vowels, consonants, and words in a sentence. As of now, the program runs but it doesn't run correctly. It counts one vowel and the rest are consonants, no words. I can't figure out how to change this. Thanks for the help if you can. This is what i have so far:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <ctype.h>
#include <conio.h>

using namespace std;


int main ()
{
	int vowel = 0, consonant = 0, words = 0, i;

	char letter;

	ifstream data;
	data.open("/Users/Yea/Documents/Projects/data.txt");

	for (i =1; i < 16; i++)
	{
		
		data.get(letter);
		putchar (tolower(letter));

			if (letter == 'a')
			{
				vowel++;

				if (letter == 'e')
				{
					vowel++;

					if (letter == 'i')
					{
						vowel++;

						if (letter == 'o')
						{
							vowel++;

							if (letter == 'u')
							{
								vowel++;

								if (letter == ' ')
								{
									words++;
								}
							}
						}
					}
				}
			}
									else 
									{
										consonant++;
									}
	}
	cout << "\n\nVowels: "<< vowel;
	cout << "\nConsonants: "<< consonant;
	cout << "\nWords: "<< words << endl;

	data.close();

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Word and Vowel Counting Program Help

#2 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6601
  • View blog
  • Posts: 30,793
  • Joined: 10-May 07

Re: Word and Vowel Counting Program Help

Posted 16 October 2008 - 09:28 PM

Maybe a switch would suite this situation better, since the consonant is the else to the a. As well all of the nested if statements require that letter is equal to a.
switch(letter) {
  case 'a':
     vowel++;
     break;
  case 'e':
     vowel++;
     break;
  case 'i':
     vowel++;
     break;
  case 'o':
     vowel++;
     break;
  case 'u':
     vowel++;
     break;
  case ' ':
     words++;
     break;
  default:
     consonant++;
     break;


Was This Post Helpful? 1
  • +
  • -

#3 needcplusplushelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 08

Re: Word and Vowel Counting Program Help

Posted 17 October 2008 - 07:22 AM

View Postno2pencil, on 16 Oct, 2008 - 09:28 PM, said:

Maybe a switch would suite this situation better, since the consonant is the else to the a. As well all of the nested if statements require that letter is equal to a.
switch(letter) {
  case 'a':
     vowel++;
     break;
  case 'e':
     vowel++;
     break;
  case 'i':
     vowel++;
     break;
  case 'o':
     vowel++;
     break;
  case 'u':
     vowel++;
     break;
  case ' ':
     words++;
     break;
  default:
     consonant++;
     break;



Okay, so I changed my code and it works, (thank you no2pencil) ...well it worked. I changed it again because now I want it to read a period or question mark as the end and to end the count there. I also want it to output each letter so that the output file has the sentence too. It's counting but for some reason its giving me an incorrect count. Anyone see the possible flaw in the code?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <ctype.h>
#include <conio.h>

using namespace std;


int main ()
{
	int vowel = 0, consonant = 0, words = 1, a = 0, e = 0, i = 0, o = 0, u = 0, punc = 0, q;
	//vowel represents the vowel count, consonant is the consonant count, words is the word count.
	//Word is initialized at 1 because it is assumed you start with one word. The 'q' is for the for loop.
	//Punc represents punctuation, all other int variables are for the specific letter count. All initialized at 0.

	char letter;
	//This is the variable for the input value, the letter read.

	ofstream output;
	output.open("/Users/Yea/Documents/Projects/output.txt");
	//Output file

	ifstream data;
	data.open("/Users/Yea/Documents/Projects/data.txt");
	//Input file

	for (q =1; q < 16; q++)
	{
	//Start of for loop, set to only read 15 characters.

		data.get(letter);
	//Getting the next character from input no matter what, spaces included.

		putchar (tolower(letter));
	//Puts all characters to lowercase if it is not already (saves coding in the switch).

		switch(letter)
			{ //Start of the switch.

			case 'a':  
			vowel++ && a++ && output << letter;  
			break;
		//If character is 'a', increment vowel count and 'a' count.

			case 'e':  
			vowel++ && e++ && output << letter;  
			break;  
		//If character is 'e', increment vowel count and 'e' count.

			case 'i':  
			vowel++ && i++ && output << letter;  
			break;  
		//If character is 'i', increment vowel count and 'i' count.

			case 'o':  
			vowel++ && o++ && output << letter;  
			break;  
		//If character is 'o', increment vowel count and 'o' count.

			case 'u':  
			vowel++ && u++ && output << letter;  
			break;  
		//If character is 'u', increment vowel count and 'u' count.

			case ' ':  
			words++ && output << letter;  
			break;  
		//If character is ' ', increment word count.

			case '.':
			punc++ && output << letter;
			break;
		//If character is '.', end loop.

			case '?':
			punc++ && output << letter;
			break;
		//If character is '?', end loop.

			default:  
			consonant++ && output << letter;  
			break;  
		//Else, increment consonant count.

			}//End Switch.

	}//End For Loop.

	cout << "\n\nAmount of A's: "<< a;
	cout << "\nAmount of E's: "<< e;
	cout << "\nAmount of I's: "<< i;
	cout << "\nAmount of O's: "<< o;
	cout << "\nAmount of U's: "<< u;
	//Displays the count of specific vowels.

	cout << "\n\nVowels: "<< vowel;
	cout << "\n\nConsonants: "<< consonant;
	cout << "\n\nWords: "<< words << "\n\n\n";
	//Displays the count of total vowels, consonants, and words.

	output << "\n\nAmount of A's: "<< a;
	output << "\nAmount of E's: "<< e;
	output << "\nAmount of I's: "<< i;
	output << "\nAmount of O's: "<< o;
	output << "\nAmount of U's: "<< u;

	output << "\n\nNumber of Vowels: "<< vowel;
	output << "\nNumber of Consonants: "<< consonant;
	output << "\nNumber of Words: "<< words;
	//Sends all data, including the input sentance to output file.

	output.close();
	//Output file closed.
	data.close();
	//Input file closed.

	return 0;

}//End Main Program.


Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Word and Vowel Counting Program Help

Posted 17 October 2008 - 10:14 AM

Just a few quick changes ... The output seems better now ... but ... you see how it is ... (I didn't check the logic.)

#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;


int main ()
{
	int vowel = 0, consonant = 0, words = 1, a = 0, e = 0, i = 0, o = 0, u = 0, punc = 0, q;
	//vowel represents the vowel count, consonant is the consonant count, words is the word count.
	//Word is initialized at 1 because it is assumed you start with one word. The 'q' is for the for loop.
	//Punc represents punctuation, all other int variables are for the specific letter count. All initialized at 0.

	char letter;
	//This is the variable for the input value, the letter read.

	ofstream output("output.txt"); //Output file

	ifstream data("countWords.cpp"); //Input file

	for (q =1; data.get(letter); q++)
	{
	//Start of for loop, set to only read 15 characters.

		//data.get(letter);
	//Getting the next character from input no matter what, spaces included.

		putchar (tolower(letter));
	//Puts all characters to lowercase if it is not already (saves coding in the switch).

		switch(letter)
			{ //Start of the switch.

			case 'a':  
			vowel++; 
            a++;  
			break;
		//If character is 'a', increment vowel count and 'a' count.

			case 'e':  
			vowel++;
            e++; 
			break;  
		//If character is 'e', increment vowel count and 'e' count.

			case 'i':  
			vowel++;
            i++;
			break;  
		//If character is 'i', increment vowel count and 'i' count.

			case 'o':  
			vowel++;
            o++;  
			break;  
		//If character is 'o', increment vowel count and 'o' count.

			case 'u':  
			vowel++; 
            u++; 
			break;  
		//If character is 'u', increment vowel count and 'u' count.

			case ' ':  
			words++;  
			break;  
		//If character is ' ', increment word count.

			case '.':
			punc++;
			break;
		//If character is '.', end loop.

			case '?':
			punc++;
			break;
		//If character is '?', end loop.

			default:  
			consonant++; 
			break;  
		//Else, increment consonant count.

			}//End Switch.
            output << letter;
	}//End For Loop.

	cout << "\n\nAmount of A's: "<< a;
	cout << "\nAmount of E's: "<< e;
	cout << "\nAmount of I's: "<< i;
	cout << "\nAmount of O's: "<< o;
	cout << "\nAmount of U's: "<< u;
	//Displays the count of specific vowels.

	cout << "\n\nVowels: "<< vowel;
	cout << "\n\nConsonants: "<< consonant;
	cout << "\n\nWords: "<< words << "\n\n\n";
	//Displays the count of total vowels, consonants, and words.

	output << "\n\nAmount of A's: "<< a;
	output << "\nAmount of E's: "<< e;
	output << "\nAmount of I's: "<< i;
	output << "\nAmount of O's: "<< o;
	output << "\nAmount of U's: "<< u;

	output << "\n\nNumber of Vowels: "<< vowel;
	output << "\nNumber of Consonants: "<< consonant;
	output << "\nNumber of Words: "<< words;
	//Sends all data, including the input sentance to output file.

	output.close(); 	//Output file closed.
	data.close(); 	    //Input file closed.

    system( "notepad output.txt" );
	return 0;

}//End Main Program. 

This post has been edited by David W: 17 October 2008 - 10:16 AM

Was This Post Helpful? 1
  • +
  • -

#5 needcplusplushelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 08

Re: Word and Vowel Counting Program Help

Posted 17 October 2008 - 12:03 PM

Thank you both, I fixed the output so that it shows the sentence, not sure why I didn't think of doing it that way. I also added another switch to count the specific vowels. Thanks guys, much appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 EDengele2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-October 09

Re: Word and Vowel Counting Program Help

Posted 07 December 2009 - 11:32 AM

View Postneedcplusplushelp, on 17 Oct, 2008 - 06:22 AM, said:

View Postno2pencil, on 16 Oct, 2008 - 09:28 PM, said:

Maybe a switch would suite this situation better, since the consonant is the else to the a. As well all of the nested if statements require that letter is equal to a.
switch(letter) {
  case 'a':
     vowel++;
     break;
  case 'e':
     vowel++;
     break;
  case 'i':
     vowel++;
     break;
  case 'o':
     vowel++;
     break;
  case 'u':
     vowel++;
     break;
  case ' ':
     words++;
     break;
  default:
     consonant++;
     break;



Okay, so I changed my code and it works, (thank you no2pencil) ...well it worked. I changed it again because now I want it to read a period or question mark as the end and to end the count there. I also want it to output each letter so that the output file has the sentence too. It's counting but for some reason its giving me an incorrect count. Anyone see the possible flaw in the code?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <ctype.h>
#include <conio.h>

using namespace std;


int main ()
{
	int vowel = 0, consonant = 0, words = 1, a = 0, e = 0, i = 0, o = 0, u = 0, punc = 0, q;
	//vowel represents the vowel count, consonant is the consonant count, words is the word count.
	//Word is initialized at 1 because it is assumed you start with one word. The 'q' is for the for loop.
	//Punc represents punctuation, all other int variables are for the specific letter count. All initialized at 0.

	char letter;
	//This is the variable for the input value, the letter read.

	ofstream output;
	output.open("/Users/Yea/Documents/Projects/output.txt");
	//Output file

	ifstream data;
	data.open("/Users/Yea/Documents/Projects/data.txt");
	//Input file

	for (q =1; q < 16; q++)
	{
	//Start of for loop, set to only read 15 characters.

		data.get(letter);
	//Getting the next character from input no matter what, spaces included.

		putchar (tolower(letter));
	//Puts all characters to lowercase if it is not already (saves coding in the switch).

		switch(letter)
			{ //Start of the switch.

			case 'a':  
			vowel++ && a++ && output << letter;  
			break;
		//If character is 'a', increment vowel count and 'a' count.

			case 'e':  
			vowel++ && e++ && output << letter;  
			break;  
		//If character is 'e', increment vowel count and 'e' count.

			case 'i':  
			vowel++ && i++ && output << letter;  
			break;  
		//If character is 'i', increment vowel count and 'i' count.

			case 'o':  
			vowel++ && o++ && output << letter;  
			break;  
		//If character is 'o', increment vowel count and 'o' count.

			case 'u':  
			vowel++ && u++ && output << letter;  
			break;  
		//If character is 'u', increment vowel count and 'u' count.

			case ' ':  
			words++ && output << letter;  
			break;  
		//If character is ' ', increment word count.

			case '.':
			punc++ && output << letter;
			break;
		//If character is '.', end loop.

			case '?':
			punc++ && output << letter;
			break;
		//If character is '?', end loop.

			default:  
			consonant++ && output << letter;  
			break;  
		//Else, increment consonant count.

			}//End Switch.

	}//End For Loop.

	cout << "\n\nAmount of A's: "<< a;
	cout << "\nAmount of E's: "<< e;
	cout << "\nAmount of I's: "<< i;
	cout << "\nAmount of O's: "<< o;
	cout << "\nAmount of U's: "<< u;
	//Displays the count of specific vowels.

	cout << "\n\nVowels: "<< vowel;
	cout << "\n\nConsonants: "<< consonant;
	cout << "\n\nWords: "<< words << "\n\n\n";
	//Displays the count of total vowels, consonants, and words.

	output << "\n\nAmount of A's: "<< a;
	output << "\nAmount of E's: "<< e;
	output << "\nAmount of I's: "<< i;
	output << "\nAmount of O's: "<< o;
	output << "\nAmount of U's: "<< u;

	output << "\n\nNumber of Vowels: "<< vowel;
	output << "\nNumber of Consonants: "<< consonant;
	output << "\nNumber of Words: "<< words;
	//Sends all data, including the input sentance to output file.

	output.close();
	//Output file closed.
	data.close();
	//Input file closed.

	return 0;

}//End Main Program.



my question is where do you initialize the variable 'letter' in this program?
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Word and Vowel Counting Program Help

Posted 07 December 2009 - 11:35 AM

It's read from the file:
data.get(letter);

Why do you keep bumping old threads?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1