How do you read values from a file you created, to print it in an array to create a frequency?

Recommended Answers

All 9 Replies

>How do you read values from a file you created
Open it and read it? How is that not working for you?

Let me rephrase the question...In C++, say for example i typed some numeric values in a notepad document, i want to print it out in the command prompt format in an array to create a frequency.

>Let me rephrase the question...
I understood your question perfectly the first time. Let's try something different, why don't you post your attempt at the problem so that I know how much hand holding you need.

commented: I don't think Mr.Narue would approve of the hand holding. -2

This where i am at, at the moment, i just want to place the data in it respected columns and be certain that i am doing it correctly. Furthermore, i want frequency algorithm to examine the data in the array, one element at a time, and add 1 to the the corresponding element in the frequency array based on the data values taken from the file.

P.S. - I have attached the text file from which the data values are collected. I really appreciate your help,

a lot.

include <iostream>

using namespace std;

include <fstream>

using std::ifstream;

include <iomanip>

using std::setw;
using std::setprecision;

include <cstdlib>

void outputLine(int);

int main()
ifstream inClientFile( "getData.txt", ios::in );

if ( !inClientFile )
     cerr << "File could not be opened" << endl;
     exit( 1 );
int freq = 20;

cout << "Element" << setw(13) << "Frequency" << setw(13) << "Histogram" << setw(17) << endl << fixed << showpoint;

for ( int i = 0; i < freq; i++ ) 
    cout << setw(3) << i << setw(7) << endl;

while ( inClientFile >> freq )
      outputLine( freq );

return 0;


void outputLine( int freq )
for ( int j = 0; j < freq; j++ )
cout << '*';
cout << setw(8) << freq
<< setw(7) << setprecision(2)
<< endl;

Member Avatar

What you trying to do?

I am simply trying to see if i can take values from a text file i personally created, and store the data in an array, print it, add 1 to the corresponding element in the frequency array and print out a vertical histogram using asterisks for each occurence of the element.

Member Avatar

Yes you can do that what do you mean by frequency?

>what do you mean by frequency?
He wants to read the file and get a count of each character. That's the frequency, then he wants to display a histogram of that frequency. For example, if there are 5 a's in the file, 2 b's, 1 c, and 4 d's, the histogram might be like this:

a: *****
b: **
c: *
d: ****


//CPTR352: Data Structures II 
//Christopher Follette - 26041517
//September 12, 2007
//written in Dev-C++
//NOrthern Caribbean University 

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void menu();
void getData();
void printData();
void makeHistogram();
void makeFrequency();

int main(int argc, char *argv[]){
    //creates a menu to display available options
    int choice = 0; 
	cout<< "This program creates a frequency histogram\ncreated from values read from a file of integers\n\n";
	cout <<"Please choose one of options below:\n 1. Load file: \'values.txt\'\n 2. Exit program\n"<<" ";
	cin >> choice;

		return EXIT_SUCCESS;	

       cout <<endl<<endl<<"Please choose one of options below:\n 1. Display raw data\n 2. Display Histogram\n 3. Exit\n"<<" ";
       cin >> choice; 
       if(choice==1 ){
       else if(choice==2){
       else {
    cout << endl << endl << endl;;
    return EXIT_SUCCESS;

int* dataArrayPtr;//array  for raw data from file
int* freqArrayPtr;//frequency array
int length = 0;//length of dataArrayPtr

// getData() reads the file and stores the data in an array. 

void getData(){    
     //Count the integers in the file      	
     ifstream in_stream;"values.txt");		  
	 while (!in_stream.eof()){
         int hold = 0;          
         in_stream >> hold;
         if(hold > 20 || hold < 1) continue;       
         length++;//global value for dataArray size
     ifstream* pFileStream = 0;     
     //open file for reading
     pFileStream = new ifstream("values.txt");          
     if (pFileStream->fail()){        
        cerr << "Could not open values.txt" << endl;
        delete pFileStream;
    dataArrayPtr = new int[length];
    for(int j= 0; j< length; j++){
        dataArrayPtr[j]= 22; 
    int i = 0;    
    while (!pFileStream->eof()){  
          int nValue = 0;                
         (*pFileStream) >> nValue;                 
         //stop if the file failed 
         if (pFileStream->fail()){
             cerr << "\nError: file read failed" << endl;                              
         //bypass any numbers > 20 || < 1
         if(nValue > 20 || nValue < 1){
         dataArrayPtr[i] = nValue;         
    cout <<endl<<"File found and read\n";     

//makeFrequency() examines the data in the array, and adds 1
//to the corresponding element in a frequency array.

void  makeFrequency(){     
     //initialise freqArrayPtr 
     freqArrayPtr = new int[20];           
     for(int j= 0; j< 20; j++){
        freqArrayPtr[j]= 0;         
     cout <<endl <<endl;
     for(int i=0; i< length; i++){  
        int dValue = dataArrayPtr[i];     
        if(dValue <= 20){ 
           freqArrayPtr[dValue]= freqArrayPtr[dValue] + 1;           

* makeHistogram() prints out a vertical histogram using
* ('*') for each element
void makeHistogram() {
     cout <<endl<<endl<<endl<<"Frequency Histogram"<<endl;
     for(int i=0; i < 20; i++){  
        if(i < 9){cout <<" ";}//spacing       
        cout << i+1 <<": "<<freqArrayPtr[i];  
        for(int j=0; j < freqArrayPtr[i]; j++){
            cout << "*";    
        cout << endl;                       

* printData() prints the data in the array
void printData(){    
    cout <<endl <<"Values found in file." <<endl; 
    for(int i=0; i <= length; i++){               
          if(dataArrayPtr[i]<10)cout <<" ";                            
          cout << dataArrayPtr[i]<<" ";              
          if(i%20==0 && i!= 0 ) cout << endl;         
     cout << "\n\nEnd of data.\n";               
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.