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;
}