You have a file. You have variables that you have to read that file into. Your job is to write code that matches the file layout and the variables. getline and eof() are tools. Depending on the file layout, they may or may not be the right tools. If you are a beginner, you may not yet have a firm grasp of what tools to use for what purpose.
getline "gets a line" and throws away the newline. It is used for strings. If you don't have a string variable, either don't use getline or use it to read to a string, then convert the string to something else. If you are looking to read in an integer, you can use getline, then convert to a C-Style string using c_str(), then use the atoi from cstdlib or something similar, or you can read into an integer directly using >>.
If you have a file and you want to extract the letters and nothing but the letters and put them all in a big string, don't use getline or eof(). I mean you could, but it's extra work. Use the plain old >> operator.
string allLetters = "";
char temp;
while(fin >> temp)
{
if (isalpha(temp))
{
allLetters += temp;
}
}
You may need to elaborate a little more about exactlyt what's supposed to end up in this long string.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Here is a problem here:
if(isalpha(textstring[i]))
letters_total++;
if(isupper(textstring[i]))
textstring[i]=tolower(textstring[i]);
histogram_abs[(textstring[i] - 'a')]++;
Adding in brackets:
if(isalpha(textstring[i]))
{
letters_total++;
}
if(isupper(textstring[i]))
{
textstring[i]=tolower(textstring[i]);
}
histogram_abs[(textstring[i] - 'a')]++;
Note that the last line executes regarless of either if test. Almost certainly you do not want this. Suppose the character is '0', which is 48 in ASCII. I forget what 'a' is, but it's more than 48, which means that '0' - 'a' is a negative number, which crashes the program. You need to add some more testing/handling for non-letters. Try adjusting the brackets above to make sure.
And now is a good time to get familiar with the assert command from the cassert library.
if(isalpha(textstring[i]))
{
letters_total++;
}
if(isupper(textstring[i]))
{
textstring[i]=tolower(textstring[i]);
}
int index = textstring[i] - 'a';
assert(index >= 0);
histogram_abs[index]++;
Run it with this change and you'll see EXACTLY where the program bombs and debug from there.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Suppose textstring an STL string object and the content of the string is "My 7th program". The goal is to calculate how many of each letter there is in the string by looking at each char in textstring one at a time:
int len = textsting.length(); //determine the length of the string
for(int i = 0; i < len; ++i) //look at each char in the string
{
if this char is alphabetical letter
{
increment total number of letters found
if this char is upper case
{
convert this char to lower lower case
}
increment the counter varible for this letter.
}
//else
//this char isn't a letter so ignore it.
}
Note how you can nest one if statement within the other so you only run the second if statement if the first one is true.
Assuming the counter variables are set to zero before you use them in this snippet it shouldn't matter what all is in textstring or where/how you get textstring.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
if(isalpha(textstring[i]))
{
letters_total++;
}
if(isupper(textstring[i]))
{
textstring[i]=tolower(textstring[i]);
histogram_abs[(textstring[i] - 'a')]++;
}
Better, but still a bit of a problem. If I'm understanding the goal, the whole point is that an 'E' is to be treated as an 'e' (i.e. a letter is tallied regardless of its case). Go through the code above and see if it works on both lower and upper case.
Change your input file and make it nice and easy:
a
A
b
B
B
a
a
A
c
c
C
Get it so you get the desired results. Throw in a few change-ups till its all right.
As an added bonus, it's ofter very helpful to, before doing any processing, simply regurgitate the file that you've read in to prove to yourself that you read it in correctly.
As far as the other code I posted, yes, it's a skeleton to illustrate a point and won't compile in your code without some modification. Anyway, you must first isolate whether the problem is reading the file or whether it's the letter tallying. If it's the former, post a simple input file with the corresponding correct output so we can run it.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I think the proper logic for the if-statements is this:
if(isalpha(textstring[i])) //if it is a letter (not a number or other crap)
{
letters_total++; //increment letter count.
if(isupper(textstring[i])) //if upper-case,
{
textstring[i]=tolower(textstring[i]); //then make it lower case.
}
histogram_abs[(textstring[i] - 'a')]++; //at this point, only a lower-case letter could be in textstring[i].
}
As it was before, only the capital E would be counted and only capital I (which is obviously more common in English).
mike_2000_17
Posting Virtuoso
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457