If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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).

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.

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.

Re: I cannot print on an output file

Originally Posted by math8

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.

Re: I cannot print on an output file

Originally Posted by GCDEF

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.

Re: I cannot print on an output file

Originally Posted by math8

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.

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?

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.

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 .

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.

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.

Re: I cannot print on an output file

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.