Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; Yes and this is what i get: Breakpoint 1 (main.cpp:24) pending. Child process PID: 4864 Program exited normally. Debugger finished ...

  1. #16
    Registered User
    Join Date
    May 2011
    Posts
    13
    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

  2. #17
    Registered User
    Join Date
    May 2011
    Posts
    13
    Thanks, i forgot something

    the main looks like this now:

    Code:
    nt main()
    {
    string hol="input.txt";
        Enor t(hol);
        t.First();
        while (!t.End())
            {
                t.Next();
            }
    
        cout<<darabw<<endl;
        return 0;
    }
    The problem is , that it counts the character w-s and not the words.
    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.

  3. #18
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    What is this program supposed to do?

  4. #19
    Registered User
    Join Date
    May 2011
    Posts
    13
    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.

  5. #20
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    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;
    }

  6. #21
    Registered User
    Join Date
    May 2011
    Posts
    13
    It is an order, that i can't do that. Don't know why, but I mustn't.

    Anyway in:

    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;}
    even if wk is true it increases darabw in the first if.

    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.

  7. #22
    Registered User
    Join Date
    May 2011
    Posts
    13
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21