2 Replies - 2234 Views - Last Post: 02 March 2009 - 07:50 PM Rate Topic: -----

#1 ss_hat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-March 09

counting vowels,consonants & integers from an input file then outp

Posted 02 March 2009 - 04:00 PM

I'm attempting to count vowels, consonants & integers from an input file and then put the results that are printed on screen into a new file. I managed to get the vowels part correct, but I keep getting error messages when I get into the consonants part. Maybe my eyes are just lost on the page and a second look is all I need?

Any assistance is greatly appreciated. I don't need you to tell me the output to file code, I'll figure that out on my own. I just want to get past this roadblock.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main(void)
{

	char fn[30],ch;
	int vowels =0;
	int consonants =0;
	cout << "Enter a File Name ";
	cin >> fn;
	ifstream infile(fn);
		if (infile)
	{
		while (!infile.eof() )
		{
			infile.get(ch);
			if ((ch== 'a') || (ch== 'A') || (ch== 'e') || (ch== 'E') || (ch== 'i') || (ch== 'I')|| (ch== 'o')|| (ch== 'O')||(ch== 'U')|| (ch== 'u')) vowels++;
			cout << ch;
		}
		cout << "\nVowels:" << vowels << endl;
		{
			while (!infile.eof() )

		{
			infile.get(ch);
			if ((ch=='B') || (ch=='b')|| (ch=='C') || (ch=='c') || (ch=='D') || (ch=='d') || (ch=='F') || (ch=='f') || (ch=='G') || (ch=='g') || (ch=='H') || (ch=='h') || (ch=='J') || (ch=='j') || (ch=='K') || (ch=='k')|| (ch=='L') || (ch=='l') || (ch=='M') || (ch=='m') || (ch=='N') || (ch=='n') || (ch=='P') || (ch=='p')||(ch=='Q') || (ch=='q')|| (ch=='R') || (ch=='r')|| (ch=='S') || (ch=='s') || (ch=='T') || (ch=='t') || (ch=='V') || (ch=='v')|| (ch=='W') || (ch=='w') || (ch=='X') || (ch=='x')|| (ch=='Y') || (ch=='y')||(ch=='Z') || (ch=='z')) consonants++;
			cout << ch;
		}
			cout << "\nConsonants:" << consonants << endl;
	
		
		else cout << "Invalid file";
		return 0;
}



My error messages are as follows:
Error 1 error C2181: illegal else without matching if c:\documents and settings\gnome\my documents\visual studio 2008\projects\ehneyprogram4\ehneyprogram4\fileio.cpp 35
Error 2 fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\gnome\my documents\visual studio 2008\projects\ehneyprogram4\ehneyprogram4\fileio.cpp(16)' was matched c:\documents and settings\gnome\my documents\visual studio 2008\projects\ehneyprogram4\ehneyprogram4\fileio.cpp 38

Is This A Good Question/Topic? 0
  • +

Replies To: counting vowels,consonants & integers from an input file then outp

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: counting vowels,consonants & integers from an input file then outp

Posted 02 March 2009 - 07:17 PM

You need to stick in a bracket to end the big if
if (infile)
	{
		while (!infile.eof() )
		{
			infile.get(ch);
			if ((ch== 'a') || (ch== 'A') || (ch== 'e') || (ch== 'E') || (ch== 'i') || (ch== 'I')|| (ch== 'o')|| (ch== 'O')||(ch== 'U')|| (ch== 'u')) vowels++;
			cout << ch;
		}
		cout << "\nVowels:" << vowels << endl;
		{
			while (!infile.eof() )

		{
			infile.get(ch);
			if ((ch=='B') || (ch=='b')|| (ch=='C') || (ch=='c') || (ch=='D') || (ch=='d') || (ch=='F') || (ch=='f') || (ch=='G') || (ch=='g') || (ch=='H') || (ch=='h') || (ch=='J') || (ch=='j') || (ch=='K') || (ch=='k')|| (ch=='L') || (ch=='l') || (ch=='M') || (ch=='m') || (ch=='N') || (ch=='n') || (ch=='P') || (ch=='p')||(ch=='Q') || (ch=='q')|| (ch=='R') || (ch=='r')|| (ch=='S') || (ch=='s') || (ch=='T') || (ch=='t') || (ch=='V') || (ch=='v')|| (ch=='W') || (ch=='w') || (ch=='X') || (ch=='x')|| (ch=='Y') || (ch=='y')||(ch=='Z') || (ch=='z')) consonants++;
			cout << ch;
		}
			cout << "\nConsonants:" << consonants << endl;
   
   }/////////here!
		else cout << "Invalid file";
		return 0;
}


Checking for consonants could just be a letter than is not a vowel, here's an example that may help,

You should be able to not have to check for all the possible consonants, something like isalpha combined with a notVowel function of your own,

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: counting vowels,consonants & integers from an input file then outp

Posted 02 March 2009 - 07:50 PM

Another thing you have to watch is that once you loop through the file and hit the end, you can't go into another loop where you read more without resetting the file pointer back to the beginning again.

So what you should look into doing is creating one loop which reads in each character at a time and validates whether not it is a vowel, consonant or other and increment the proper counting variables.

:)

This post has been edited by Martyr2: 02 March 2009 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1