CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  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.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: I cannot print on an output file

    The function "Frequency" doesn't return anything. This:
    Code:
    file2<<Frequency(alphabet[p], num, a) ;
    Is an error.

    Viggy

  3. #3
    Join Date
    Feb 2013
    Posts
    36

    Re: I cannot print on an output file

    Thanks, I can see it is an error. If I wanted to get the results of void Frequency(alphabet[p], num, a) on the screen, I would just write
    Frequency(alphabet[p], num, a) ; (with no cout<<). However, my question is, what should I do if I want to write the results of the void function on the output file2. Obviously, file2<< won't work since it is a void function.

  4. #4
    Join Date
    Feb 2013
    Posts
    36

    Re: I cannot print on an output file

    I just figured out what was wrong.

    I cannot use the same function Frequency when printing out on the output file. I had to create a copy of the Frequency function and in the new one, I replaced the "cout<<" with "file2<<". And it worked.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I cannot print on an output file

    Quote Originally Posted by math8 View Post
    I just figured out what was wrong.

    I cannot use the same function Frequency when printing out on the output file. I had to create a copy of the Frequency function and in the new one, I replaced the "cout<<" with "file2<<". And it worked.
    That's a poor approach from a design point of view. You should have one Frequency function that should return a value. It shouldn't output anything. You had a better approach the first time, you just need Frequency to return a value.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I cannot print on an output file

    I also want to take issue with your lack of white space and your indentation and brace matching style. That seems to throw noobs for a loop. Compare your
    Code:
    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;}
    with
    Code:
    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;
    }
    Which is easier to read?

  7. #7
    Join Date
    Feb 2013
    Posts
    36

    Re: I cannot print on an output file

    Quote Originally Posted by GCDEF View Post
    That's a poor approach from a design point of view. You should have one Frequency function that should return a value. It shouldn't output anything. You had a better approach the first time, you just need Frequency to return a value.
    Could you give me an example of a function of type double, float,or char that can return some text both for the screen and for the ouput file. I tried earlier, and I couldn't make it work, that's why I had to have two different Frequency functions.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I cannot print on an output file

    Quote Originally Posted by math8 View Post
    Could you give me an example of a function of type double, float,or char that can return some text both for the screen and for the ouput file. I tried earlier, and I couldn't make it work, that's why I had to have two different Frequency functions.
    A function should really only have on purpose. If the purpose of Frequency is to figure out how often something occurs, that should be all it does.

    Code:
    float Frequency(char chr, float num, char a[1000])
    {
        float counts_chr = 0;
        int k = 0;
         for(int j = 0; j < num; j++) 
        {
            if(a[j] == chr)
            {
                k++;
            }  
        }
        counts_chr = k;
        return counts_chr / num;
    }
    Now this statement should work, but move the text that was in Frequency to this statement also.

    file2 << Frequency(alphabet[p], num, a) ;

  9. #9
    Join Date
    Feb 2013
    Posts
    36

    Re: I cannot print on an output file

    Alright, I see what you are saying. But technically speaking, in my method, I write the same amount of text "cout<< / file2<<" - wise (except that I have 2 different but similar functions to call as opposed to one).
    So except for esthetic purpose, is there another reason why my method would be considered a poor approach? I mean time-wise or something more important-wise?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I cannot print on an output file

    One of the primary things to keep in mind when writing code is reusability. By putting output statements in Frequency, you can't use it unless you actually want to output as well. You can't use it in other calculations and you can't use it with different output. It's also kind of sloppy as you end up with output statements all over the place, where it would be easier to write and maintain if they were grouped together. It may not seem so important in a small project like this, but in a bigger one, it's vital. I don't know what you have now, but you say you have two functions that do essentially the same thing. What if you find a bug or need to change the way it works. No you have two pieces of code to change, if you even remember you have two. In a few years, you probably won't.

  11. #11
    Join Date
    Feb 2013
    Posts
    36

    Re: I cannot print on an output file

    Thanks, I will keep that in mind.

    However, I am running into this new problem: In the void Frequency function that I had , besides calculating the frequency, it was also calculating the number of times a character appears (counts_chr). And I need that to go in my cout<< / file2<< as well.

    Now by using the float Frequency function which only returns the frequency, I am not taking advantage of the variable counts_chr anymore. And by typing what's below, it is giving me a wrong answer for counts_chr .

    Code:
     if(choice==1){
      cout<<"Please give character to check: ";    
      cin>>char_to_check; 
      Frequency(char_to_check, num, a) ;
      cout<<"Character "<<char_to_check<<" appears "<<counts_chr<<" times in the text."<<endl;
      cout<<"It has a frequency of "<<Frequency(char_to_check, num, a) <<"."<<endl;
      }
    The only thing that comes to my mind is that I would have to redo the calculations for counts_chr inside the loop or create a separate function that returns the counts_chr, which again makes me wonder whether this is a better scenario than what I had before.
    Last edited by math8; February 13th, 2013 at 11:18 AM.

  12. #12
    Join Date
    Aug 2009
    Posts
    440

    Re: I cannot print on an output file

    Look at how you come up with counts_chr within your frequency function. Once you have that bit of information you can figure out what counts_chr is. Or, and probably a better solution, is to pass another variable into your frequency function. Pass this one by reference, then, when you update it within the function, the variable itself gets updated.
    Last edited by Alterah; February 13th, 2013 at 11:35 AM.

  13. #13
    Join Date
    Feb 2013
    Posts
    36

    Re: I cannot print on an output file

    hmmm, I rearranged some things around, and this is what I came up with.

    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    ifstream file1;
    ofstream file2;
    void menu0();
    void menuA();
    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'};
    float counts(char chr, float num, char a[1000]);
    
    
    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; 
      
      cout<<"Character "<<char_to_check<<" appears "<<counts(char_to_check,num, a)<<" times in the text."<<endl;
      cout<<"It has a frequency of "<< counts(char_to_check,num, a)/num<<"."<<endl;
      }
       
     else if(choice==2){
      cout<<"Report stored in file: report.txt "<<endl;
      file2.open("report.txt");
      for(int p=0;p<26;p++){
      file2<<"Character "<<alphabet[p]<<" appears "<<counts(alphabet[p],num, a)<<" times in the text."<<endl;
      file2<<"It has a frequency of "<< counts(alphabet[p],num, a)/num<<"."<<endl;
      
      file2<<endl;}
      file2.close();  
      }
      
     else if(choice!=0) 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; 
    }  
    
       
    float counts(char chr,float num,  char a[1000]){
         float counts_chr=0;
         int k=0;
         for(int j=0;j<num;j++){
            if(a[j]==chr){k++;}  }
         return k;
    }
    It seems to work just fine, and I am only using one function float counts. This is the best that I can do.
    Last edited by math8; February 13th, 2013 at 11:48 AM.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I cannot print on an output file

    Congratulations, now get to work on formatting so your code is readable.

  15. #15
    Join Date
    Aug 2009
    Posts
    440

    Re: I cannot print on an output file

    I agree with GCDEF on the formatting. One thing I want to point out is that your counts function should probably have an int return value. You are counting the number of times a character appears (can't have 'a' appear 2.5 times...). I'd also look at the function again and see if there are any unused variables in it.

Page 1 of 2 12 LastLast

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