Yes and this is what i get:
Breakpoint 1 (main.cpp:24) pending.
Child process PID: 4864
Program exited normally.
Debugger finished with status 0
Yes and this is what i get:
Breakpoint 1 (main.cpp:24) pending.
Child process PID: 4864
Program exited normally.
Debugger finished with status 0
Thanks, i forgot something
the main looks like this now:
The problem is , that it counts the character w-s and not the words.Code:nt main() { string hol="input.txt"; Enor t(hol); t.First(); while (!t.End()) { t.Next(); } cout<<darabw<<endl; return 0; }
in the input the number should be 1 at the end, but it will be six.
the darabw should only be added to if wk is false, and the character we are on is w or W.
Last edited by Psyho; 05-31-2011 at 01:48 PM.
It reads in from a file, and counts the words, which contain w or W, so if within the same word there are more than one, then that should be counted as 1.
My idea was, that: find the first 'real' character, and if it is w or W, than we found one word that's good. wk =true (we found a w here already); ++darabw
In Next(), we look at the next character. If it is "w" or "W" and we already found one, we don't do anything.
If it is a delimiter, then we aren't in a word, so wk=false, and nemlimit=false since the next 'real' character will be a start of the new word.
We only count one up in darabw, if ch is "w" or "W", and we haven't already found one, so wk is false.
Last edited by Psyho; 05-31-2011 at 01:59 PM.
Why so complicated then? Are you trying to do it with a state machine or something? The problem can be solved by storing characters up to a word boundary, then checking for 'w' or 'W' and starting over.
Code:#include <ctype.h> #include <fstream> #include <iostream> #include <string> using namespace std; #define TEST int main() { ifstream ifs("test.txt"); int count = 0; string word; while (ifs) { char ch; if (!ifs.get(ch) || isspace(ch)) { #if defined(TEST) cout << "Checking '"<< word << "'" << endl; #endif count += word.find_first_of("wW") != string::npos; word.clear(); } else { word.push_back(ch); } } cout << count << endl; }
It is an order, that i can't do that. Don't know why, but I mustn't.
Anyway in:
even if wk is true it increases darabw in the first if.Code:void Enor::Next() { char ch; f.get(ch); if (((ch=='w')|| ((ch=='W')) && (wk==false))) {++darabw; wk=true;} if ((ch==' ') || (ch== '\t') || (ch=='\n')) {nemlimit=false; wk=false;}
I just tried to play with it for a bit, it doesn't even care, what wk is. i rewrote it to if...&&(wk==true)... ;wk=false;} and it didn't change anything.
Last edited by Psyho; 05-31-2011 at 02:26 PM.
thanks i managed to do it in the end.
If your interested it was this:
from : if (((ch=='w')|| ((ch=='W')) && (wk==false))) => ((ch=='w'|| ch=='W')&& (wk==false))
those which are red, had to go.
Thank you for all the help you have given me.
Hope to hear from you again.