CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Threaded View

  1. #1
    Join Date
    Feb 2013
    Posts
    36

    I cannot print on an output file

    So I am writing this code that analyzes a file called "analysis.txt" and prints out in an ouput file (called "report.txt") the results of the analysis, i.e.,(the frequency of all alphabet letters present in the file).

    I am having trouble with outputing on the file2.

    here is what I have:

    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    ifstream file1;
    void menu0();
    void menuA();
    void Frequency(char chr, float num, char a[1000]);
    char alphabet[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    
    
    
    int main(){
    bool done=false;
    char filename[40];
    int choice;    
    char char_to_check; 
    char a[1000];
    int counter=0;
        
    menu0();  
    
    do{
     cout<<"Please give file name: ";
     cin.getline(filename,sizeof(filename));  
     file1.open(filename); //filename is open
     if(file1.fail()){ file1.clear();}
     else {done=true; }
     }while(!done);  
     
     while(file1.eof()==false){
     file1>>a[counter++];}
     a[counter-1]='\0';
     float num=counter-1;
     cout<<"The number of all characters in the file is "<<num<<endl<<endl;
     
     file1.close();
    
    do{
     menuA();
     cout<<"Choose: ";
     cin>>choice;
     
     if(choice==1){
      cout<<"Please give character to check: ";    
      cin>>char_to_check; 
      Frequency(char_to_check, num, a) ;
      }
       
     else if(choice==2){
      cout<<"Report stored in file: report.txt "<<endl;
      ofstream file2("report.txt");
      for(int p=0;p<26;p++){
      file2<<Frequency(alphabet[p], num, a) ;
      file2<<endl;}
      file2.close();  
      }
      
     else cout<<"Not a valid option."<<endl;
     
    } while(choice !=0);
    
    cout<<"Now quitting.."<<endl<<endl;
     
    //system("PAUSE");
       //pause();
    return 0;
    }
    
    
    void menu0(){
      cout<<"This program accepts a text file from a user and performs"<<endl;
      cout<<"an analysis of the letters and characters used by the author."<<endl<<endl;   
    }   
    void menuA(){
      cout<<"Choose"<<endl;
      cout<<"1. To perform analysis on a single character."<<endl;   
      cout<<"2. To write analysis for all alphabet in a file."<<endl; 
      cout<<"0. To quit."<<endl; 
    }  
    void Frequency(char chr, float num, char a[1000]){
     float counts_chr=0;
     int k=0;
     float freq;
     for(int j=0;j<num;j++){
        if(a[j]==chr){k++;}  }
        counts_chr=k;
       freq=counts_chr/num;
       cout<<"Character "<<chr<<" appears "<<counts_chr<<" times in the text."<<endl;
       cout<<"It has a frequency of "<<freq <<"."<<endl;}
    It's giving me the error: " no match for 'operator<<' in 'file2 << Frequency(((int)alphabet[p]), num, ((char*)(&a)))' "

    I tried to lose the "<<" after the file 2, but it's still giving me an error.
    I guess my question is, how to output a void function on a text file?
    Last edited by math8; February 12th, 2013 at 05:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured