1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
void PrintMenu(); //The Menu to be printed out
void IntSelect(int&, char); //To decide if a menu option was selected
void OpenFile(ifstream&, string&); //Opening a file
void countVow(int, char&, ifstream&);
void countCon(int num3, char ch3, ifstream& inFile, int ch2);//Counting vowels and consonants
int main()
{
int num1 = 0, num2, num3;
float fnum1, fnum2, fnum3;
string filename;
bool endLoop = false;
char ch = 'a', ch2, ch3;
ifstream inFile;
while (!endLoop)
{
PrintMenu();
IntSelect(num1, ch);
cout << num1 << endl;
cin.ignore (2000, '\n');
if (num1 == 1 || num1 == 2)
{
OpenFile(inFile, filename);
if (num1 == 1)
{
countVow(num2, ch2, inFile);
}
else;
countCon(num3, ch3, inFile, ch2);
}
else if (num1 == 0)
{
endLoop = true;
}
else
cout << string(10, '*') << " Invalid Selection " << string(10, '*') << endl;
cout << "====> Invalid integer value entered\n";
cout << "====> Please enter 0, 1 or 2.\n";
cout << string(41, '*') << endl << endl;
}
return 0;
}
void PrintMenu() //Print the Menu
{
cout << string(15,'*') << " Options " << string(15, '*') << endl;
cout << "0. Exit Program \n";
cout << "1. Count number of vowels \n";
cout << "2. Count number of consonants \n";
cout << string(41, '*') << endl << endl;
cout << "Input your selection: ";
}
void IntSelect(int& num1, char ch) //Function called to determine if and what integer was selected
{
cin >> num1;
while (!num1) //cin isn't valid
{
//invalid character entered
cin.clear(); //clears the input stream
cin >> ch; //reads character
cout << ch << endl; //outputs character
cin.ignore (2000, '\n');
cout << string(10, '*') << " Invalid Selection " << string(10, '*') << endl;
cout << "====> Invalid character entered!!\n";
cout << "====> Please enter 0, 1 or 2.\n";
cout << string(41, '*') << endl << endl;
PrintMenu();
cin >> num1;
}
cout << num1 << endl;
cin.ignore (2000, '\n');
}
void OpenFile(ifstream& inFile, string& filename) //Function called to open our filestream
{
cout << "Enter the name of the input file now: ";
cin >> filename;
cout << filename << endl;
inFile.open(filename.c_str()); // Attempt to open file for input
while (inFile.fail()) // Test status of input file
{
cout << endl << string(11,'*') << " File Open Error " << string(11,'*') << endl;
cout << "==> Input file failed to open properly!!\n";
cout << "==> Attempted to open file: " << filename << endl;
cout << "==> Selected operation has been cancelled\n";
cout << string(41,'*')<< endl << endl;
inFile.clear();
cout << "Enter the name of the input file: ";
cin >> filename;
cout << filename << endl;
inFile.open(filename.c_str()); // Attempt to open file for input again.
}
}
void countVow(int num2, char& ch2, ifstream& inFile) //Function called to count vowels
{
num2 = 0;
inFile >> ch2;
if (!inFile)
{
cout << string(12, '*') << " File is Empty " << string(12, '*') << endl;
cout << "==> Input File is Empty" << endl;
cout << "==> No information to process" << endl;
cout << string(41, '*') << endl;
}
while (inFile)
{
if (ch2 == 'A' || 'a' || 'E' || 'e' || 'I' || 'i' || 'O' || 'o' || 'U' || 'u')
{
num2++;
}
}
cout << "Counting Number of Vowels" << endl;
cout << string(25, '-') << endl;
cout << "There are " << num2 << " vowels in the file" << endl;
cout << "There are ";
inFile.close();
}
void countCon(int num3, char ch3, ifstream& inFile, int ch2) //Function called to count consonants
{
num3 = 0;
inFile >> ch3;
if (!inFile)
{
cout << string(12, '*') << " File is Empty " << string(12, '*') << endl;
cout << "==> Input File is Empty" << endl;
cout << "==> No information to process" << endl;
cout << string(41, '*') << endl;
}
else
while (inFile)
{
if (!ch2 && isalpha(ch3))
{
num3++;
}
}
inFile.close();
}
|