-
More Help!
ok so here is the updated version:
Code:
name >> words;
while(!name.eof())
{
getwords(words);
numdigits(words);
name >> words;
}
cout << endl;
cout << endl;
cout << "# of words: " << endl; //getwords(words) << endl;
cout << "# of sentences: " << endl; //<< numsent(words);
cout << "# of letters: " << endl; //<< numletters(words);
cout << "# of digits: " << numdigits(words) << endl;
cout << "# of occurrences of " << search << ":" << endl; // << numsearch(words);
cout << endl;
}
int numdigits(string& name)
{
char ch;
int count = 0;
if (ch == '1')
{
count++;
}
return count;
}
she said i have to pass in each string word and have it go through each of the functions but the problem i am having with my digits function is how can i pass a string word in and have it see if its a character?? it is obvisouly giving me error of words cant = character. how do i get it to convert. and i know i only have 1 there but i am just trying to see if it will recognize anything. I also need it to keep going through the loop for all of the words in the file. Help!!!
-
You can access each character of a string with operator[]. So name[0] is the first character and name[name.length() - 1] is the last character. Just loop through all the characters in the string.
Another note- you will have to keep a count inside your main function of the number of digits. Right now when you call numdigits, you ignore the return value. You need to look at that return value, and keep a running count of all the values returned from that function to get the total number of digits.
-
ok then what is my count++ doing in my digits function? nothing? i am trying not to have anything in my main function expect the cout statements and the calling of the functions. Can i just fix my digits function to make it count right?
-
>> what is my count++ doing in my digits function?
It is counting the digits in the current word. This is necessary. You still have to return the information back to main so that it can be output. As it is right now, main doesn't know anything about the count variable in the numdigits function. It needs to know the value in order to output it.
-
ok but if i am returning count to main, and then in my output i am printing that whole function- shouldnt it print what i return? I mean it would have the number stored in count in the digits function right? So when i call the digits function from main in my print statement- why wouldnt it print the number?
Code:
cout << "# of digits: " << numdigits(words) << endl;
i think i am going to scratch this whole thing and start over. i am having such a hard time with this. thanks though
-
You call numdigits once for each word in the file. Each time you call it, count starts over at 0 and returns only the number of digits in that particular word. Your output code won't work because you are only calling numdigits with a single word when you are outputting it. Functions just don't work that way.
All you need is a variable in main to remember the total count. Add the return value of numdigits to it for each word, then output that variable when you are done.
-
ok i get that but heres my new idea-
have one function that does all the counting for number of digits, number of sentences, number of words, numbers of letters, and number of occurences of the search string. I will also print out from this function after everything i counted.
then have another function that will do the formatting of the whole file. We have to reformat the whole file to have only 40 characters on each line. so i will have one function that will count each word and then add up the lengths and have it do less than 40 kinda thing.
then i have int main which will just call the functions.
please tell me this sounds good because i am just not understanding!
-
About counting all at once I would start with something like that:
in psevdocode
Code:
int main()
{
open_file
parse_file
close_file
}
void parse_file()
{
initialize counters
for(;;)
{
read line
if(eof reached)
break;
sentences += count_sentences(line);
words += count_words(line);
digits += count_digits(line);
...
}
print counters
}
If you want to "format" file at the same moment, you can add possibility to write the string aftere you finish counting elements in this string, formatting it before writing, to another file.
In this case you read file only once but parse every line several times.
But I would start solving this problem only aftere the more simple solution is working fine..
-
that is what i need to do- read through the file only once and count everything in it. she only wants one pass through. so the sample code you posted will do all my counting at once? thanks for your help
-
new counter
Ok well i got the digits to work finally!!!!!!
now i am working on doing a find in the file. I am asking the user to enter in a searcg string. I then need to go through my file and count how many times that word is found. this is what i have but it is returning 0 to me again. I am sure my for loop is not correct.
Code:
int count(ifstream& name, string search)
{
int count = 0;
char ch;
string words;
int wordsearch;
name >> words;
while(!name.eof())
{
name >> ch;
if(ch == '0'|| ch == '1'||ch == '2'||ch == '3'||ch == '4'||ch == '5'||ch == '6'||ch== '7'||ch == '8'||ch== '9')
{
count++;
}
name >> words;
}
cout << "# of digits: " << count << endl;
while(!name.eof())
{
int i = 0;
int wordsearch = 0;
// getline(name, words, '\n');
for(i = words.find(search, 0); i != string::npos; i = words.find(search, i))
{
wordsearch++;
i++; // Move past the last discovered instance to avoid finding same
// string
}
}
cout << "# of occurrences of " << search << ":" << wordsearch << endl;
}
-
Unfortunately you are back to the same problems again. You are reading into the words variable and the ch variable, but whatever you read into words will not be checked by ch and vice versa. You are also going through the file twice even though your teacher suggested that you not do that.
Look at vart's pseudocode again. It is very different from your code.