Hi, I've been learning basic C++ for a couple months now and I'm currently writing a program to count letters from a .txt file and then output the number of each letter present. At the moment I have managed to get the program to open the file and begin counting, however it counts all the letters as the letter a.
The text file is made up only mixed numbers and letters (but only lower case) so I dont need to worry about converting them.
The program doesnt need to count spaces or the numbers just the letters.
Use '==' in if statements. The first if statement (testing for 'a') always returns true because you used a single equal sign and all the others are skipped.
By the way, have you learned about arrays yet? An array could very greatly simplify this program. You could do something like this:
Thanks for the reply Pyrius, it didnt occur to me to make it into an array. But now having made those changes im at a lost for calculating the percentages for each letter. At the moment all the values return 0.00000
You shouldn't name an array "array", because if you were to skim through your code it would be confusing as to what the variable array is for. Give it a name relevant to what it's used for. Also, what are the "#include <iomanip>", "fixed", "setw(3)" and "int number" for?
To solve your problems with the percentages, put (float) in front of array[letter]. You could also make the program return 0 if the file can't be opened instead of putting the rest of the program in an else block.
EDIT: One more thing: you don't need to pass global variables to functions as parameters.
Thanks again Pyrius, I'll try and tidy up the code. As far as im aware the "#include <iomanip> allows editing of the output, hence the "fixed" and "setw(3)" basically makes the output look nice, similar to setprecision().And the "int number" was for something else I was trying to do.