C++ program counting letters

need help with a program that counts number of times a letter occurs in a line from a file. using nested for loops with a switch inside the for loops.
Big_DleeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Welcome to EE! I realise that you are new here, but you have to give us something to go on, to be able to help you. Can you post the code and detail what you need help with? Is it not compiling, is it giving an error or does it just not do what you are expecting?
Big_DleeAuthor Commented:
not doing what it should im trying to count the number of times a character occurs in a line using a nested for loop with a switch inside. this is what i have so far.
Program-5.cpp
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Ok, there may be quite a few things that you need to do to this to get it going but lets start with the easier stuff...

On line 30 is where you read in each character from the file. However, within each case block, you are reading in another character again, but nothing will happen to this. Remove all the "infile >> ch" lines from within the case blocks. (Leave the one on line 30, it is the only one you need)

Now the next major thing is the way you are counting the letters. The result is that you want the number of times 'a' or 'A' is seen, and then a separate count of the number of times 'b' or 'B' is seen, etc, etc. However, your variable "Count" can only hold one number and so it can't hold all the results that you want. Now the naive way would be to create 26 different Count variables, say CountA, CountB, CountC, etc. But that is a slow and error prone way to go. However, to get your understanding of the basic program right, it is probably a first step. So instead of declaring "Count = 0" on line 15, do "CountA = 0, CountB = 0, ..... CountJ = 0;" and then within the case blocks just increment the appropriate variable, eg. for the case 'a' block, replace "Count += 'A';" with "CountA++;"

This should get you going at least.

Have you learn't about arrays? This is what you will need to make your code MUCH simpler, and should be your next step to improving your program. Using arrays will allow you to NOT have to declare all those Count... variables, it will eliminate your "switch" statement and make you code much easier to understand. But we can deal with that once you are more comfortable with what is happening here!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Big_DleeAuthor Commented:
OK thanks it really helped now the only problem is i need to count characters for each line and total for that specific line not keep it running through the whole file. how can i reset it after each line?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Put in another case block, such as...

case 0x13:
case 0x10:
    // Statements to print out the current lines counts
    //

    // Reset the counters ready for the next line
    CountA = 0;
    CountB = 0;
    // Etc, for all the other counts
    //
    break;

Open in new window


Obviously, this doesn't line up with using the for loop to count through lines, but the way you are reading from the files means that you CAN'T use a for loop for that, you would have to change most of the structure of the program to use a for loop properly. Same goes for the "Letter" for loop to, actually.
fridomCEO/ProgrammerCommented:
Use data structures
case 'A':case 'a':
					{	// if letter A or a
						infile >> ch;
						Count += 'A';
						break;
					}

Open in new window


having such code 25 times is a very bad idea.

I'd go for
struct charAndCounter
    char a_char_if_one_wants_it;
    long counter;

and you better have some
NUMBER_OF_CHARS = 26;
struct charAndCounter charAndCounterArray [NUMBER_OF_CHARs];


Then you can easily index into it with

ch = toupper(ch);

charAndCounterArray[ch - 'A'].longcounter += 1;
this works because 'A' - 'A' = 0 and so you'd increase the counter of the first char (which obviously should be 'A'
with 'B' in second place the subscript would be 1 etc.

resetting after each line is easy doable
for (i = 0; i < NUMBER_OF_CHARS; i++) {
                  charAndCounterArray[i].longcounter = 0;
}

Open in new window

Dirk HaestProject managerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.