Solved

C++ program counting letters

Posted on 2012-03-28
8
400 Views
Last Modified: 2012-08-02
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.
0
Comment
Question by:Big_Dlee
8 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 37779926
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
 

Author Comment

by:Big_Dlee
ID: 37779941
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
 
LVL 35

Accepted Solution

by:
mccarl earned 250 total points
ID: 37780041
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Big_Dlee
ID: 37780145
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
 
LVL 35

Expert Comment

by:mccarl
ID: 37780223
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
 
LVL 24

Assisted Solution

by:fridom
fridom earned 250 total points
ID: 37781603
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 38249570
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now