| | |
Having Trouble, please help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
The program is suppose to read from a text and count each letter of the alphabet and the counter_other counts everything else except whitespace. I'm having trouble putting this together, especially the switch. We have not gone over strings yet in class so I don't think I'm suppose to use it in the program.
#include<iostream>
#include<fstream>
#include<cctype>
#include<string>
using namespace std;
void readData(istream &, int []);
void initialize(int[], int);
void printResults(ostream &, int []);
const int SIZE= 27;
int main()
{
int letterCount[SIZE];
char index;
ifstream infile;
ofstream outfile;
initialize(letterCount, SIZE);
readData(infile, letterCount);
printResults(outfile, letterCount);
infile.close();
outfile.close();
system("pause");
return 0;
}
void initialize(int letterCount[], int n)
{
for(int i=0; i<SIZE; i++)
letterCount[i] = 0 ;
}
void readData(istream &in, int letterCount[])
{
int counter=0;
char index;
index = in.get();
in>>index; // priming read
while(index) // while last read was successful
{
in>>index; // get next character
if(index=0; index<27; index++)
case 'A': counter[0]++; break;
case 'B': counter[1]++; break;
case 'C': counter[2]++; break;
case 'D': counter[3]++; break;
case 'E': counter[4]++; break;
case 'F': counter[5]++; break;
case 'G': counter[6]++; break;
case 'H': counter[7]++; break;
case 'I': counter[8]++; break;
case 'J': counter[9]++; break;
case 'K': counter[10]++; break;
case 'L': counter[11]++; break;
case 'M': counter[12]++; break;
case 'N': counter[13]++; break;
case 'O': counter[14]++; break;
case 'P': counter[15]++; break;
case 'Q': counter[16]++; break;
case 'R': counter[17]++; break;
case 'S': counter[18]++; break;
case 'T': counter[19]++; break;
case 'U': counter[20]++; break;
case 'V': counter[21]++; break;
case 'W': counter[22]++; break;
case 'X': counter[23]++; break;
case 'Y': counter[24]++; break;
case 'Z': counter[25]++; break;
default : counter[26]++; break;
}
return;
}
void printResults(ostream &out, int letterCount [ ] )// Prints each alphabet character and its frequency
{
ofstream outfile("C:/Users/gina 0/Documents/histogram.txt");
outfile<< " LETTER "<< " OCCURRENCES "<< " PERCENTAGE " <<endl;
for (int i = 0; i < 26; i++)
{
if (letterCount[i] > 0)
{
outfile << letterCount[i] << " " << char('A' + i) << endl;
}
outfile.width(15);
}
outfile.close();
return;
}
#include<iostream>
#include<fstream>
#include<cctype>
#include<string>
using namespace std;
void readData(istream &, int []);
void initialize(int[], int);
void printResults(ostream &, int []);
const int SIZE= 27;
int main()
{
int letterCount[SIZE];
char index;
ifstream infile;
ofstream outfile;
initialize(letterCount, SIZE);
readData(infile, letterCount);
printResults(outfile, letterCount);
infile.close();
outfile.close();
system("pause");
return 0;
}
void initialize(int letterCount[], int n)
{
for(int i=0; i<SIZE; i++)
letterCount[i] = 0 ;
}
void readData(istream &in, int letterCount[])
{
int counter=0;
char index;
index = in.get();
in>>index; // priming read
while(index) // while last read was successful
{
in>>index; // get next character
if(index=0; index<27; index++)
case 'A': counter[0]++; break;
case 'B': counter[1]++; break;
case 'C': counter[2]++; break;
case 'D': counter[3]++; break;
case 'E': counter[4]++; break;
case 'F': counter[5]++; break;
case 'G': counter[6]++; break;
case 'H': counter[7]++; break;
case 'I': counter[8]++; break;
case 'J': counter[9]++; break;
case 'K': counter[10]++; break;
case 'L': counter[11]++; break;
case 'M': counter[12]++; break;
case 'N': counter[13]++; break;
case 'O': counter[14]++; break;
case 'P': counter[15]++; break;
case 'Q': counter[16]++; break;
case 'R': counter[17]++; break;
case 'S': counter[18]++; break;
case 'T': counter[19]++; break;
case 'U': counter[20]++; break;
case 'V': counter[21]++; break;
case 'W': counter[22]++; break;
case 'X': counter[23]++; break;
case 'Y': counter[24]++; break;
case 'Z': counter[25]++; break;
default : counter[26]++; break;
}
return;
}
void printResults(ostream &out, int letterCount [ ] )// Prints each alphabet character and its frequency
{
ofstream outfile("C:/Users/gina 0/Documents/histogram.txt");
outfile<< " LETTER "<< " OCCURRENCES "<< " PERCENTAGE " <<endl;
for (int i = 0; i < 26; i++)
{
if (letterCount[i] > 0)
{
outfile << letterCount[i] << " " << char('A' + i) << endl;
}
outfile.width(15);
}
outfile.close();
return;
}
0
#2 19 Days Ago
This is a very simple programming problem which can be done with only 8 lines of code! All you have to do is create an array of 255 ints then use each letter of the string as an index into the array. So if you have 'A' then all you have to do is increment counts['A']. Yes, you do not really need all 255 array elements, but it makes the program a lot more efficient if you use it.
The instructions say you only count the number of alphabetical characters -- 'A'-'Z' and 'a'-'z'. The macro isalpha will help you do that
Finally, to print out the results, just loop through the array and for those values greater than 0 print the value of the loop counter cast as char, and the value of the counters array.
The instructions say you only count the number of alphabetical characters -- 'A'-'Z' and 'a'-'z'. The macro isalpha will help you do that
C++ Syntax (Toggle Plain Text)
// assuming a variable named string if( isalpha(string[i]) ) // increment the counter counts[string[i]]++;
Finally, to print out the results, just loop through the array and for those values greater than 0 print the value of the loop counter cast as char, and the value of the counters array.
Last edited by Ancient Dragon; 19 Days Ago at 10:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Hotmail Access Trouble (Web Browsers)
- Trouble loading Norton Go Back Software (Windows Software)
- I'm having trouble with IE6 (Web Browsers)
- rundll32 trouble (Viruses, Spyware and other Nasties)
- my win 98 is in trouble (Windows 95 / 98 / Me)
- Trouble opening certain files... (Windows 95 / 98 / Me)
- Trouble installing Pather (OS X)
- sims trouble (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: C++ Matrix Multiplication
- Next Thread: Program Looping
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






