Hi,

I have a homework problem in where I have to count the vowels from a readin file from a flashdrive/harddrive. Then the amounts of each vowel has to be displayed & which occurred the most amount of times & which occurred the least amount of times. Extra credit (which I always like) is given if we can sort from highest to lowest.

I have the core of the program down & it runs fine. The problem I am running into is with the sorting - I can only sort from lowest to highest....I can't get it to sort highest to lowest.

The second (and probably more important) is how I am corresponding the actual vowel name ('A' for example) with the sorted array data. Basically, I can get a line of output to read: 'There are 8 occurrences of the vowel ", but I need the last part of the output sentence to read "There are 8 occurrences of the vowel 'A'" or "There are 8 occurrences of the vowel 'O'".

Any input is greatly appreciated. I am fearing I have jumped into arrays a bit too early. We haven't really gone over them yet, which is why the sorting is extra credit.

If there is anything wrong with my code, I appreciate the input. Although I am new at C++, ignorance is no excuse. Feel free to point out newbie mistakes.

Thank you kindly for your help.

//project 3: vowels//
#include "stdafx.h"
#include <iostream>
#include "math.h"
#include <fstream>
#include <algorithm>
using namespace std;

int main ()
{
//declare variables//
char ch;
int a,e,i,o,u;
a=e=i=o=u=0;
const char* filename="f:\\voweltest.txt"; //declare input file to get vowels from//

ifstream infile(filename); //opening file//
if (!infile){ //if no input file found, advise user//
cout<<"no input file\n";
exit(0);
}

else if (infile){
//starting a loop to scan for vowels//

while ((ch=infile.get()) != EOF){
	ch = tolower(ch);
	if (ch=='a') a++; //if a character is an a, keep track of the amount//
	if (ch=='e') e++; //if a character is an e, keep track of the amount//
	if (ch=='i') i++; //if a character is an i, keep track of the amount//
	if (ch=='o') o++; //if a character is an o, keep track of the amount//
	if (ch=='u') u++; //if a character is an u, keep track of the amount//
	}
}



//sorting from lowest to highest//
int v[5]= {a,e,i,o,u};
sort(v, v+5);
for (int z=0; z<5; z++){
	if (v[z]==0){ //detemining which vowels have no instances to display proper verbiage//
	cout<<"There are "<<v[z]<<" no instances of the vowel ";
	cout<<endl;
	}

	else if (v[z]==1){ //detemining which vowels have one instance to display proper verbiage// 
	cout<<"There is "<<v[z]<<" instance of the vowel ";
	cout<<endl;
	}

	else if (v[z]>1){ //detemining which vowels have more than one instance to display proper verbiage// 
	cout<<"There are "<<v[z]<<" instances of the vowel ";
	cout<<endl;
	}

}

if (a=e=i=o=u==0)
cout<<"No vowels occurred! What kind of words are you entering?\n";


infile.close();
return 0;
}

Start by defining two arrays, first to hold the values and second to hold the vowels themselves. Then use loops to check the input characters with the letter array and increment the correct value in the other array (or define a 2D array to make it easier).

Then for the sort be sure you sort both arrays at the same time. Don't use the sort() function, but sort the values by writing your own code.

Thanks Walt!

I think I needed to bounce ideas off of someone. :)
I stayed away from an array as I really don't want to confuse myself & would rather wait for the instruction in class. I will see how that works after the instruction though! I did setup some if statements inside my sort loop. The code is below. the problem I am running into now is that if I have an equal count of vowels for say 'A' & 'E', the program will output '8 instances of A' and '8 instances of A' instead of the second one saying '8 instances of E'. I know my loop is hitting the A first each times it passes through - I just can't figure out how to have it ignore the A after the first time it loops. Another example is if there are no vowels - the program will say 'there are 0 occurrences of A', but five times. i hope I am not being confusing. Revised code below:

//project 3: vowels//
#include "stdafx.h"
#include <iostream>
#include "math.h"
#include <fstream>
#include <algorithm>
using namespace std;

int main ()
{
//declare variables//
char ch;
int a,e,i,o,u;
a=e=i=o=u=0;
const char* filename="f:\\voweltest.txt"; //declare input file to get vowels from//

ifstream infile(filename); //opening file//
if (!infile){ //if no input file found, advise user//
cout<<"no input file\n";
exit(0);
}

else if (infile){
//starting a loop to scan for vowels//

	while ((ch=infile.get()) != EOF){
		ch = tolower(ch);
		if (ch=='a') a++; //if a character is an a, keep track of the amount//
		if (ch=='e') e++; //if a character is an e, keep track of the amount//
		if (ch=='i') i++; //if a character is an i, keep track of the amount//
		if (ch=='o') o++; //if a character is an o, keep track of the amount//
		if (ch=='u') u++; //if a character is an u, keep track of the amount//
		}
}



//sorting from lowest to highest//
int v[5]= {a,e,i,o,u};
sort(v, v+5);
for (int z=0; z<5; z++){
	if (v[z]==0){ //detemining which vowels have no instances to display proper verbiage//
		if (v[z]==a){
		cout<<"There are "<<v[z]<<" instances of the vowel A";
		cout<<endl;
		continue;
		}
		if (v[z]==e){
		cout<<"There are "<<v[z]<<" instances of the vowel E";
		cout<<endl;
		continue;
		}
		if (v[z]==i){
		cout<<"There are "<<v[z]<<" instances of the vowel I";
		cout<<endl;
		continue;
		}
		if (v[z]==o){
		cout<<"There are "<<v[z]<<" instances of the vowel O";
		cout<<endl;
		continue;
		}
		if (v[z]==u){
		cout<<"There are "<<v[z]<<" instances of the vowel U";
		cout<<endl;
		continue;
		}
	}

	else if (v[z]==1){ //detemining which vowels have one instance to display proper verbiage// 
		if (v[z]==a){
		cout<<"There is "<<v[z]<<" instance of the vowel A";
		cout<<endl;
		continue;
		}
		if (v[z]==e){
		cout<<"There is "<<v[z]<<" instance of the vowel E";
		cout<<endl;
		continue;
		}
		if (v[z]==i){
		cout<<"There is "<<v[z]<<" instance of the vowel I";
		cout<<endl;
		continue;
		}
		if (v[z]==o){
		cout<<"There is "<<v[z]<<" instance of the vowel O";
		cout<<endl;
		continue;
		}
		if (v[z]==u){
		cout<<"There is "<<v[z]<<" instances of the vowel U";
		cout<<endl;
		continue;
		}
	}

	else if (v[z]>1){ //detemining which vowels have more than one instance to display proper verbiage// 
		if (v[z]==a){
		cout<<"There are "<<v[z]<<" instances of the vowel A";
		cout<<endl;
		continue;
		}
		if (v[z]==e){
		cout<<"There are "<<v[z]<<" instances of the vowel E";
		cout<<endl;
		continue;
		}
		if (v[z]==i){
		cout<<"There are "<<v[z]<<" instances of the vowel I";
		cout<<endl;
		continue;
		}
		if (v[z]==o){
		cout<<"There are "<<v[z]<<" instances of the vowel O";
		cout<<endl;
		continue;
		}
		if (v[z]==u){
		cout<<"There are "<<v[z]<<" instances of the vowel U";
		cout<<endl;
		continue;
		}
	
	}

}

if (a==0 && e==0 && i==0 && o==0 && u==0)
cout<<"No vowels occurred! What kind of words are you entering?\n";


infile.close();
return 0;
}

Have you considered using a switch or while loop? After all, you only need to go through each element once right?

Well there are a multitude of ways you can approach this; one being setting a flag for each vowel to disregard it after it has been used(tedious/crude) alternatively you can make a structure of a int and character(probally not what you are looking towards). So what do you do? Well you its simple really, you have already sorted your vowels from highest to lowest, so you dont even need an those encompassing if statements!

for (int z=0; z<5; z++){
		if (v[z]==a){
		cout<<"There are "<<v[z]<<" instances of the vowel A";
		cout<<endl;
		continue;
		}
		if (v[z]==e){
		cout<<"There are "<<v[z]<<" instances of the vowel E";
		cout<<endl;
		continue;
		}
		if (v[z]==i){
		cout<<"There are "<<v[z]<<" instances of the vowel I";
		cout<<endl;
		continue;
		}
		if (v[z]==o){
		cout<<"There are "<<v[z]<<" instances of the vowel O";
		cout<<endl;
		continue;
		}
		if (v[z]==u){
		cout<<"There are "<<v[z]<<" instances of the vowel U";
		cout<<endl;
		continue;
		}
}

should suffice(provided you do not have to output a different things for special cases i.e. "there are zero 'A's")

That is true, I can do away with one of the if statements. I would prefer to keep the one if only 1 instance of a vowel is found, just to keep plurals & singulars separate. A switch is a good idea...silly me for not thinking of it :) I'll let you know how it works....thanks!

Thank you for the responses. I got it to work sufficiently. I am sure it could be more compact & quicker, but I don't think I have learned what I was trying to do. That will all come in time.

Here is my completed project:

//project 3: vowels//
#include "stdafx.h"
#include <iostream>
#include "math.h"
#include <fstream>
#include <algorithm>
using namespace std;

int main ()
{
//declare variables//
char ch;
int a,e,i,o,u,w,x,y,h,s,high;
a=e=i=o=u=w=x=y=s=h=high=0;
const char* filename="c:\\voweltest.txt"; //declare input file to get vowels from//

ifstream infile(filename); //opening file//
if (!infile){ //if no input file found, advise user//
cout<<"no input file\n";
exit(0);
}

else if (infile){
//starting a loop to scan for vowels//

	while ((ch=infile.get()) != EOF){
		ch = tolower(ch);
		if (ch=='a') a++; //if a character is an a, keep track of the amount//
		if (ch=='e') e++; //if a character is an e, keep track of the amount//
		if (ch=='i') i++; //if a character is an i, keep track of the amount//
		if (ch=='o') o++; //if a character is an o, keep track of the amount//
		if (ch=='u') u++; //if a character is an u, keep track of the amount//
		}
}

w=(a>e)?a:e;
x=(w>i)?w:i;
y=(x>o)?x:o;
h=(y>u)?y:u;
cout<<"\nThe highest instance was "<<h;
cout<<endl;

//sorting from lowest to highest//
int v[5]= {a,e,i,o,u};
sort(v, v+5);
for (int z=0; z<5; z++){
	if (v[z]==1){ //detemining which vowels have no instances to display proper verbiage//
		if (v[z]==a){
			cout<<"There is "<<v[z]<<" instance of the vowel A";
			cout<<endl;
			a=s=1; 
			continue;
			}
		if (v[z]==e){
			cout<<"There is "<<v[z]<<" instance of the vowel E";
			cout<<endl;
			e=s=1;
			continue;
			}
		if (v[z]==i){
			cout<<"There is "<<v[z]<<" instance of the vowel I";
			cout<<endl;
			i=s=1;
			continue;
			}
		if (v[z]==o){
			cout<<"There is "<<v[z]<<" instance of the vowel O";
			cout<<endl;
			o=s=1;
			continue;
			}
		if (v[z]==u){
			cout<<"There is "<<v[z]<<" instance of the vowel U";
			cout<<endl;
			u=s=1;
			continue;
			}
	}

	else if (v[z]==0 || v[z]>=1){ //detemining which vowels have more than one instance to display proper verbiage// 
		if (v[z]==a){
			cout<<"There are "<<v[z]<<" instances of the vowel A";
			cout<<endl;
			a=s=1;
			continue;
		}
		if (v[z]==e){
			cout<<"There are "<<v[z]<<" instances of the vowel E";
			cout<<endl;
			e=s=1;
			continue;
		}
		if (v[z]==i){
			cout<<"There are "<<v[z]<<" instances of the vowel I";
			cout<<endl;
			i=s=1;
			continue;
		}
		if (v[z]==o){
			cout<<"There are "<<v[z]<<" instances of the vowel O";
			cout<<endl;
			o=s=1;
			continue;
		}
		if (v[z]==u){
			cout<<"There are "<<v[z]<<" instances of the vowel U";
			cout<<endl;
			u=s=1;
			continue;
		}
	
	}

}

if (a==0 && e==0 && i==0 && o==0 && u==0)
cout<<"No vowels occurred! What kind of words are you entering?\n";


infile.close();
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.