Can someone help me i am trying to create a program where it reads the text file from a user and opens the text file for input 1. counts the number of characters in the file,
2. counts the number of letters in the file
3. counts the number of vowels in the file
4. counts the number of consonants in the file
5. computes the % of letters in the file
6. computes the % of vowels in the file
7. computes the % of consonants in the file
8. reads a second text file name, different from the first from a user
9. opens the second file for output
10. writes the counts and percentages, well-formatted and well-labeled, to the second file
11. closes the input file
12. close the output file
....
i created a program already but something is wrong with it. when i run the program it sends me to the else statement and then it shuts down on me can someone help me to tell me what am i doing wrong..
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const long lngreadbuffersize=1000;
void initializebuffer(void);
void dumpbuffer(void);
void readinputfile(string);
void writeoutputfile(string);
void writeoutputtoscreen(void);
long countvowels(void);
long countconsonants(void);
long countconsonants(void);
long lngindex=-1;
long lngbytesread=-1;
long lngvowelcount=-1;
long lngconsonantscount=-1;
char readbuffer[lngreadbuffersize];
int main(int argc, char* argv[])
{
string strnameofinputfile="\0";
string strnameofoutputfile="\0";
if (argc==3)
{
strnameofinputfile = string(argv[1]);
strnameofoutputfile = string(argv[2]);
}
else
{
cout << "Please enter an input file and an output file, similar to the following" << endl;
cout << "c:\\temp\\inputfile.txt c:\\outputfile.txt" << endl;
}
system("pause");
return 0;
}
void initializebuffer(void)
{
readbuffer[lngreadbuffersize]='\0';
for (lngindex=0;lngindex<lngreadbuffersize;lngindex++) readbuffer[lngindex]='\0';
}
void dumpbuffer(void)
{
for (lngindex=0;lngindex<lngreadbuffersize;lngindex++) cout << readbuffer[lngindex];
}
try
{
ofstream outputfile(strnameofoutputfile);
outputfile << "Number of character in file are " << lngbytesread << endl;
outputfile << "Number of letters are " << lngvowelcount+lngconsonantscount << endl;
outputfile << "Number of vowels are " << lngvowelcount << endl;
outputfile << "Number of consonants are " << lngconsonantscount << endl;
if ((lngbytesread>=1) && (lngvowelcount>=1) && (lngconsonantscount>=1))
{
dblvowelcount=lngvowelcount;
dblconsonantscount=lngconsonantscount;
dblbytesread=lngbytesread;
outputfile << "% of letters in file are " << dblpercentoflettersinfile << endl;
outputfile << "% of vowels in file are " << dblpercentofvowelsinfile << endl;
outputfile << "% of consonants in file are " << dblpercentofconsonantsinfile <<endl;
}
else
{
outputfile << "% of letters in file are " << dblpercentoflettersinfile << endl;
outputfile << "% of vowels in file are " << dblpercentofvowelsinfile << endl;
outputfile << "% of consonants in file are " << dblpercentofconsonantsinfile <<endl;
}
cout << endl;
cout << "% of letters in file are " << dblpercentoflettersinfile << endl;
cout << "% of vowels in file are " << dblpercentofvowelsinfile << endl;
cout << "% of consonants in file are " << dblpercentofconsonantsinfile <<endl;
}
else
{
cout << endl;
cout << "% of letters in file are " << dblpercentoflettersinfile << endl;
cout << "% of vowels in file are " << dblpercentofvowelsinfile << endl;
cout << "% of consonants in file are " << dblpercentofconsonantsinfile <<endl;
}
}
catch(exception e)
{
cout << "cannot display output to screen!" << endl;
}
}
long countvowels(void)
{
long lngtempvowelcount=0;
char c='\0';
for (lngindex=0;lngindex<lngbytesread;lngindex++)
{
c = toupper(readbuffer[lngindex]);
switch(c)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
lngtempvowelcount=lngtempvowelcount+1;
}
}
return lngtempvowelcount;
}
long countconsonants(void)
{
long lngtempconsonantscount=0;
char c='\0';
for (lngindex=0;lngindex<lngbytesread;lngindex++)
{
c = toupper(readbuffer[lngindex]);
switch(c)
{
case 'B':
case 'C':
case 'D':
case 'F':
case 'G':
case 'H':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
lngtempconsonantscount=lngtempconsonantscount+1;
}
}
return lngtempconsonantscount;
}