-
Problems with program
I have a question about selection 1 on my menu. Its supposed to count the occurrence of each letter in the file its processing. Then its supposed to create a percentage of each letter occuring in the text file. I've looked in the book and at my profs examples but still can't find anything. I'm clueless as how to do this, any help or pointers in the right direction would be greatly appreciated.
Thank you,
K
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (void)
{
string infilename, outfilename;
string WordToFind, WordToReplace, ReplaceWith;
int selection = 0;
cout << "Please type in the name of the file to be opened." << endl;
cin >> infilename;
do
{
cout << " Select one of the following functions ";
cout << "\n 1)Tabulate the frequency of occurrence of letters (A...Z) in a file ";
cout << "\n 2)Count the occurrence of a specified word in a file. ";
cout << "\n 3)Replace a specified word by another word in a file. ";
cin >> selection;
}
while (selection < 1 || selection > 3);
return selection;
if (selection == 1)
cout << infilename.length() << endl;
else (selection == 2)
cout << "Please enter the word you would like to look for";
cin >> WordToFind;
infilename.find(WordToFind);
cout << infilename << WordToFind << infilename.find(WordToFind) << endl;
else if (selection == 3)
cout << "Please enter the word you would like to be replaced";
cin >> WordToReplace;
cout << "What word would you like to replace << WordToReplace << with?" << endl;
cin >> ReplaceWith;
WordToReplace.swap(ReplaceWith);
cout << "Enter the file name to write into:";
cin >> outfilename;
ofstream outfile (outfilename.c_str());
outfile.close();
return 0;
}
-
char chCount[26];
Go through the file, one character at a time, if it's a-z or A-Z increment the corresponding array element. Once it's done, add all the elements together and you'll get the total number of A-Z characters in the file. From that, you can display the percentages and such.
-
Call me stupid. But I'm still confused. How am I going to store each letter of the alphabet and print it out? Would I make another array? And could I use switch instead of if else statements?
Thanks,
K
-
You don't need to store each letter, only count each letter.
char chCount[26];
This array will hold 26 elements, A-Z. Read through the file and each time you get a letter increment the corresponding element.
_something_ like:
PHP Code:
char THECHAR;
char chCount[26];
// Read in from the file
switch (THECHAR) {
case 'a': case 'A':
chCount[0]++;
case 'b': case 'B':
chCount[1]++;
case 'c': case 'C':
chCount[2]++;
// etc...
}
-
Thanks guys. I understand now & am no longer confused.
K
-
I used the one like Dizolve did and now I get an error that says:
switch expression of type char[26] is illegal. It could be because I have a switch inside of a switch. See below.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (void)
{
string infilename, outfilename;
string WordToFind, WordToReplace, ReplaceWith;
int selection = 0;
char chCount[26];
char TheChar[26];
cout << "Please type in the name of the file to be opened." << endl;
cin >> infilename;
do
{
cout << " Select one of the following functions ";
cout << "\n 1)Tabulate the frequency of occurrence of letters (A...Z) in a file ";
cout << "\n 2)Count the occurrence of a specified word in a file. ";
cout << "\n 3)Replace a specified word by another word in a file. ";
cin >> selection;
}
while (selection < 1 || selection > 3);
return selection;
switch(selection)
{
case '1':
switch(TheChar)
{
case 'a': case 'A':
chCount[0]++;
case 'b': case 'B':
chCount[1]++;
case 'c': case 'C':
chCount[2]++;
case 'd': case 'D':
chCount[3]++;
case 'e': case 'E':
chCount[4]++;
case 'f': case 'F':
chCount[5]++;
case 'g': case 'G':
chCount[6]++;
case 'h': case 'H':
chCount[7]++;
case 'i': case 'I':
chCount[8]++;
case 'j': case 'J':
chCount[9]++;
case 'k': case 'K':
chCount[10]++;
case 'l': case 'L':
chCount[11]++;
case 'm': case 'M':
chCount[12]++;
case 'n': case 'N':
chCount[13]++;
case 'o': case 'O':
chCount[14]++;
case 'p': case 'P':
chCount[15]++;
case 'q': case 'Q':
chCount[16]++;
case 'r': case 'R':
chCount[17]++;
case 's': case 'S':
chCount[18]++;
case 't': case 'T':
chCount[19]++;
case 'u': case 'U':
chCount[20]++;
case 'v': case 'V':
chCount[21]++;
case 'w': case 'W':
chCount[22]++;
case 'x': case 'X':
chCount[23]++;
case 'y': case 'Y':
chCount[24]++;
case 'z': case 'Z':
chCount[25]++;
break;
}
case '2':
cout << "Please enter the word you would like to look for";
cin >> WordToFind;
infilename.find(WordToFind);
cout << infilename << WordToFind << infilename.find(WordToFind) << endl;
break;
case '3':
cout << "Please enter the word you would like to be replaced";
cin >> WordToReplace;
cout << "What word would you like to replace << WordToReplace << with?" << endl;
cin >> ReplaceWith;
WordToReplace.replace(m, n, ReplaceWith);
cout << "Enter the file name to write into:";
cin >> outfilename;
ofstream outfile (outfilename.c_str());
outfile.close();
}
return 0;
}
Thanks!
:-)
K
-
You can't base a switch statement on an array. TheChar would have to be a single character, not an array of 26.
-