Having Trouble, please help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 2
Reputation: techick is an unknown quantity at this point 
Solved Threads: 0
techick techick is offline Offline
Newbie Poster

Having Trouble, please help

 
0
  #1
19 Days Ago
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;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
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
  1. // assuming a variable named string
  2. if( isalpha(string[i]) )
  3. // increment the counter
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: techick is an unknown quantity at this point 
Solved Threads: 0
techick techick is offline Offline
Newbie Poster
 
0
  #3
18 Days Ago
thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
0
  #4
18 Days Ago
Originally Posted by techick View Post
thanks
Please ask more questions if you are still having problems with this -- someone is likely to answer them.
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.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC