My code works right now but my teacher does not want the program to prompt for t
ID: 3648659 • Letter: M
Question
My code works right now but my teacher does not want the program to prompt for the text file, instead he wants it passed as a command line argument. I know I need to change my int main() to int main(int argc, char *argv[]), but after that I am lost. I have been trying many different things and can not figure out how i need to change my variables to accept the command line argument or even open the file correctly.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
float vowelscount = 0;
char infile[15000];
ifstream input;
float consonantscount = 0;
float wordscount = 0;
float vowelRatio = 0;
float consonantRatio = 0;
float VperW = 0;
char vowels[] = "aeiouy";
char consonants[] = "bcdfghjklmnpqrstvwxz";
int frequency[27] = {0};
char ch[27] = "abcdefghijklmnopqrstuvwxyz";
char tempch;
int tempfreq;
cout << "What is the input file? ";
cin.getline(infile, 15000);
input.open(infile);
while(!input.eof())
{
input.getline(infile,15000);
// covernts file to lowercase
for(int i = 0; i < strlen(infile); i++)
{
infile[i] = tolower(infile[i]);
}
// function to counts total amount of vowels
for(int i = 0; i < strlen(infile); i++)
for(int j = 0; j < strlen(vowels); j++)
if(infile[i] == vowels[j])
vowelscount++;
// counts total amount of consonants
for(int i = 0; i < strlen(infile); i++)
for(int j = 0; j < strlen(consonants); j++)
if(infile[i] == consonants[j])
consonantscount++;
// counts total amount of words
for(int i = 0; i < strlen(infile); i++)
if(infile[i] == ' ' || infile[i] == '.' || infile[i] == ',' ||
infile[i] == '!' || infile[i] == '?')
wordscount++;
// counts frequencies of each letter
for(int i = 0; i < strlen(infile); i++)
{
for(int j = 0; j < strlen(ch); j++)
{
if(ch[j] == infile[i])
frequency[j]++;
}
}
}
input.close();
// bubble sort lists to order most common occurring letter
for(int i = 0; i < 26 - 1; i++)
for(int x = 1; x < 26 - i; x++)
if(frequency[x] < frequency[x + 1])
{
tempfreq = frequency[x];
tempch = ch[x];
frequency[x] = frequency[x + 1];
ch[x] = ch[x + 1];
frequency[x + 1] = tempfreq;
ch[x + 1] = tempch;
}
vowelRatio = vowelscount / (vowelscount + consonantscount) * 100;
consonantRatio = consonantscount / (vowelscount + consonantscount) * 100;
VperW = vowelscount / wordscount;
cout << "There are " << wordscount << " words in the file." << endl;
cout << "There are " << vowelscount << " vowels in the file." << endl;
cout << "There are " << consonantscount << " consonants in the file."
<< endl;
cout << "The percentage of vowels to consonants is "
<< std::setprecision(3) << vowelRatio << "%" << endl;
cout << "The percentage of consonants to vowels is "
<< consonantRatio << "%" << endl;
cout << "The average number of vowels per word is " << VperW << endl;
cout << "The top 5 occurring letters are: " << endl;
for(int i = 0; i < 5; i++)
{
cout << frequency[i] << " " << ch[i] << "'s" << endl;
}
return 0;
}
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char * argv[])
{
float vowelscount = 0;
char infile[15000];
ifstream input;
float consonantscount = 0;
float wordscount = 0;
float vowelRatio = 0;
float consonantRatio = 0;
float VperW = 0;
char vowels[] = "aeiouy";
char consonants[] = "bcdfghjklmnpqrstvwxz";
int frequency[27] = {0};
char ch[27] = "abcdefghijklmnopqrstuvwxyz";
char tempch;
int tempfreq;
input.open(argv[1]);
while(!input.eof())
{
input.getline(infile,15000);
// covernts file to lowercase
for(int i = 0; i < strlen(infile); i++)
{
infile[i] = tolower(infile[i]);
}
// function to counts total amount of vowels
for(int i = 0; i < strlen(infile); i++)
for(int j = 0; j < strlen(vowels); j++)
if(infile[i] == vowels[j])
vowelscount++;
// counts total amount of consonants
for(int i = 0; i < strlen(infile); i++)
for(int j = 0; j < strlen(consonants); j++)
if(infile[i] == consonants[j])
consonantscount++;
// counts total amount of words
for(int i = 0; i < strlen(infile); i++)
if(infile[i] == ' ' || infile[i] == '.' || infile[i] == ',' ||
infile[i] == '!' || infile[i] == '?')
wordscount++;
// counts frequencies of each letter
for(int i = 0; i < strlen(infile); i++)
{
for(int j = 0; j < strlen(ch); j++)
{
if(ch[j] == infile[i])
frequency[j]++;
}
}
}
input.close();
// bubble sort lists to order most common occurring letter
for(int i = 0; i < 26 - 1; i++)
for(int x = 1; x < 26 - i; x++)
if(frequency[x] < frequency[x + 1])
{
tempfreq = frequency[x];
tempch = ch[x];
frequency[x] = frequency[x + 1];
ch[x] = ch[x + 1];
frequency[x + 1] = tempfreq;
ch[x + 1] = tempch;
}
vowelRatio = vowelscount / (vowelscount + consonantscount) * 100;
consonantRatio = consonantscount / (vowelscount + consonantscount) * 100;
VperW = vowelscount / wordscount;
cout << "There are " << wordscount << " words in the file." << endl;
cout << "There are " << vowelscount << " vowels in the file." << endl;
cout << "There are " << consonantscount << " consonants in the file."
<< endl;
cout << "The percentage of vowels to consonants is "
<< std::setprecision(3) << vowelRatio << "%" << endl;
cout << "The percentage of consonants to vowels is "
<< consonantRatio << "%" << endl;
cout << "The average number of vowels per word is " << VperW << endl;
cout << "The top 5 occurring letters are: " << endl;
for(int i = 0; i < 5; i++)
{
cout << frequency[i] << " " << ch[i] << "'s" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.