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?

Improve company productivity with a Business Account.Sign Up

x
 
mccarlConnect With a Mentor IT 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!
0
 
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?
0
 
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
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
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?
0
 
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.
0
 
fridomConnect With a Mentor CEO/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

0
 
DhaestCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.