No errors just doesn't do what its supposed to.

This is a discussion on No errors just doesn't do what its supposed to. within the C++ Programming forums, part of the General Programming Boards category; I know its my fault it doesnt work, I have only been learning c++ for a month. I did pretty ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    20

    No errors just doesn't do what its supposed to.

    I know its my fault it doesnt work, I have only been learning c++ for a month. I did pretty good with this except when it reads the file the word count is always 0.
    This is supposed to look at a user defined file and count the number of words in it and like I said even though the file has multiple words the result is always 0.
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<fstream>
    #include<iomanip>
    #include<cctype>
    #include<string>
    #include<vector>
    #include<cstdlib>
    using namespace std;
    
    class FileReader
    {
    public:
    
        FileReader(){counted=false, word_counter=0;}
        void reset();
        void load_ignore_file();
        void add_ignore();
        void file_count();
        int menu();
    
    private:
    
        bool counted;
        int  word_counter;
        ifstream infile;
        ofstream outfile;
        vector<string> ignored_words;
    };
    
    
    char quit();
    void menu_option_error();
    
    
    int main()
    {
        FileReader myFileReader;
    
        myFileReader.load_ignore_file();
    
        char choice = 0;
    
        do{
    
            myFileReader.reset();
    
            switch(myFileReader.menu())
            {
                case 1: myFileReader.add_ignore();
                        break;
                case 2: myFileReader.file_count();
                        break;
      default: menu_option_error();
            }
    
        }while(choice!='Y');
    
    return 0;
    }
    
    
    
    //FUNCTION DEFINITIONS
    
    int FileReader::menu()
    {
        int choice;
    
        system("clear");
    
        cout    << endl << endl << endl << endl
                << setw(40) << "          MENU            \n"
                << setw(40) << " ------------------------ \n"
                << setw(40) << "                          \n"
                << setw(40) << "  1. Add an ignore word   \n"
                << setw(40) << "                          \n"
                << setw(40) << "  2. Perform File Count   \n"
                << setw(40) << "                          \n"
                << setw(40) << "  3. Quit                 \n"
                << setw(40) << "                          \n"
                << setw(40) << " ------------------------ \n";
        if(counted)
        {cout   << setw(40) << "| Word Count = " << word_counter
                << setw(76) << "|\n"<< setw(40)
                << "------------------------";}
        cout    << endl << endl << endl << endl
                << "Enter choice: ";
        cin        >> choice;
    
        cin.ignore(80,'\n');
    
        return choice;
    }
    
    
    
    
    
    void menu_option_error()
    {
    cout << "\n\n\t\aT'was not a valid menu option!  (Try Again)";
        cout << "\n\n\tPress [ENTER] to continue...";
        cin.get();
    }
    
    char quit()
    {
        char yes_no;
    
        cout << "\n\n\tAre you sure you want to quit? (Y/N): ";
        cin  >> yes_no;
        cin.ignore(80,'\n');
    
        system("clear");
    
        cout << endl << endl;
    
        return toupper(yes_no);
    }
    
    
    void FileReader::reset()
    {
        bool counted = false;
        word_counter = 0;
    }
    
    void FileReader::load_ignore_file()
    {
        string temp;
    
        infile.clear();
    
        infile.open("ignore.dat");
    
        if(infile.fail())
           return;
    
        while(!infile.eof())
        {
            while(getline(infile, temp));
            ignored_words.push_back(temp);
        }
    
        infile.close();
    }
    
    
    
    void FileReader::add_ignore()
    {
     string temp;
    
        outfile.open("ignore.dat",ios::app);
    
        if(outfile.fail())
        {
            cout << "\n\n\t\aOUTPUT FILE WRITE FAIL!"
                 << "\n\t(Program will now terminate)"
                 << "\n\n\tPress [ENTER] to Continue...";
    
            cin.get();
    
            exit(1);
        }
    
            cout << "\n\n\n\tEnter word to be ignored: ";
            cin  >> temp;
    
            temp += ' ';
    
            outfile << temp;
    
            outfile.close();
    }
    
    
    void FileReader::file_count()
    {
        bool match;
    
        string temp;
        int back = ignored_words.size();
    
    
        cout << "\n\n\tExample:  C:\\myfile.txt"
             << "\n\t-------"
             << "\n\n\tEnter the path to your file to count: ";
        cin     >> temp;
    
        infile.clear();
        infile.open(temp.c_str(),ios::in);
    
        if(infile.fail())
        {
            cout << "\n\n\t\aCannot Locate File -OR- File Does Not Exist!"
                 << "\n\n\tPress [ENTER] to continue...";
    
            cin.ignore();
            cin.get();
    
            return;
      }
    
        infile.clear();
    
        while(!infile.eof())
        {
            infile >> temp;
    
            match = false;
    
            for(int c=0; !match && c<back; c++)
    
                if(temp == ignored_words[c])
    
                    match = true;
    
            if(!match)
    
                word_counter++;
        }
    
        infile.close();
    
        counted = true;
    
    }
    Last edited by jxmuller; 06-07-2006 at 09:11 AM.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    Sorry about posting the entire code, I just read the FAQ. If it helps I think my problem is after the last void. Its not reading the file like I think it should be. Maybe I am just doing it wrong.

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    In your main() function, you call

    Code:
    myFileReader.reset();
    This resets your FileReader object each time in the loop, which resets word_counter (hence it always prints 0).

    Remove that line, and put reset() at the end of your menu() function instead.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    Thanks that worked wonderfully. Now I need to try and finish by printing each word and the number of occurences of each word in the test.txt file!!!

    Thanks again.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    DOH: I just noticed that the word count is always off now by +1. If I have 10 words it says 11. If I have 5 words it thinks I have 6. I will try to figure this one out.

    Edit:

    Well now it doesnt recognize the option #3 Quit. This is frustrating.
    Last edited by jxmuller; 06-07-2006 at 10:10 AM.

  6. #6
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by jxmuller
    DOH: I just noticed that the word count is always off now by +1. If I have 10 words it says 11. If I have 5 words it thinks I have 6. I will try to figure this one out.
    In your file_count() member function, change

    Code:
        while(!infile.eof())
        {
            infile >> temp;
    to

    Code:
        while( infile >> temp )
        {
            match = false;

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    Ok now my word count is correct. I understand why now.

    I also got my menu option 3 to work... somehow i missed case 3
    Last edited by jxmuller; 06-07-2006 at 10:36 AM.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    Now the problem is my program is not ignoring words located in the ignore.dat file. Option 2 allows me to enter words to be ignored and that works fine (the file is created and words are added). But when I am trying to match words I suppose my logic is incorrect.

  9. #9
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Are your ignored words loaded correctly? You might want to print the values of the ignored_words vector.

    Also I would also change

    Code:
        while(!infile.eof())
        {
            while(getline(infile, temp));
            ignored_words.push_back(temp);
        }
    to

    Code:
    while( getline(infile,temp) ) 
      ignored_words.push_back(temp);

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    I think perhaps my ignore.dat file is being written wrongly:

    [ignore.dat]
    word test need dookie
    [\ignore.dat]

    I think it needs to be:
    [ignore.dat]
    word
    test
    need
    dookie
    [\ignore.dat]

    A new line for each word? I tried temp += '\n';
    AND
    outfile << temp\n;
    Last edited by jxmuller; 06-07-2006 at 10:56 AM.

  11. #11
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    while( cin >> temp ); would work for both cases (newline or spaces), I believe.

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    I have changed a few things around (ultimately I still have what I started with) and nothing seems to get this thing to ignore words from ignore.dat

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    I am about to die trying to figure this out. Soon I will be pushing daisies.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be you could post what you have changed...

    Do you set the word_counter to 0 before each count? ... Ah, you have it in the reset member - you don't need it. Hide the need to reset the value explicitly.

    Also, I don't understand why you want to print the word count in the menu method. Wouldn't it make more sense to display it right after you have counted them?

  15. #15
    Registered User
    Join Date
    Jun 2006
    Posts
    20
    Yes the counter is reset before each count. here is my current code:

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<fstream>
    #include<iomanip>
    #include<cctype>
    #include<string>
    #include<vector>
    #include<cstdlib>
    using namespace std;
    
    class FileReader
    {
    public:
    
        FileReader(){counted=false, word_counter=0;}
        void reset();
        void load_ignore_file();
        void add_ignore();
        void file_count();
        int menu();
    
    private:
    
        bool counted;
        int  word_counter;
        ifstream infile;
        ofstream outfile;
        vector<string> ignored_words;
    };
    
    
    char quit();
    void menu_option_error();
    
    
    int main()
    {
        FileReader myFileReader;
    
        myFileReader.load_ignore_file();
    
        char choice = 0;
    
        do{
    
    
            switch(myFileReader.menu())
            {
                case 1: myFileReader.add_ignore();
                        break;
                case 2: myFileReader.file_count();
                        break;
                case 3: choice = quit();
                        break;
      default: menu_option_error();
            }
    
        }while(choice!='Y');
    
    return 0;
    }
    
    
    
    //FUNCTION DEFINITIONS
    
    int FileReader::menu()
    {
        int choice;
    
        system("clear");
    
        cout    << endl
                << setw(40) << "          MENU            \n"
                << setw(40) << " ------------------------ \n"
                << setw(40) << "  1. Add an ignore word   \n"
                << setw(40) << "  2. Perform File Count   \n"
                << setw(40) << "  3. Quit                 \n"
                << setw(40) << " ------------------------ \n";
        if(counted)
        {cout   << setw(30) << " Word Count = " << word_counter
                << setw(38) << "|\n"<< setw(38)
                << "------------------------";}
        cout    << endl
                << "Enter choice: ";
        cin       >> choice;
        counted=false;
        cin.ignore(80,'\n');
        reset();
        return choice;
    }
    
    
    
    
    
    void menu_option_error()
    {
    cout << "\n\n\t\aT'was not a valid menu option!  (Try Again)";
        cout << "\n\n\tPress [ENTER] to continue...";
        cin.get();
    }
    
    char quit()
    {
        char yes_no;
    
        cout << "\n\n\tAre you sure you want to quit? (Y/N): ";
        cin  >> yes_no;
        cin.ignore(80,'\n');
    
        system("clear");
    
        cout << endl << endl;
    
        return toupper(yes_no);
    }
    
    
    void FileReader::reset()
    {
        bool counted = false;
        word_counter = 0;
    }
    
    void FileReader::load_ignore_file()
    {
        string temp;
    
        infile.clear();
    
        infile.open("ignore.dat");
    
        if(infile.fail())
           return;
    
        while( getline(infile, temp) )
        {
            ignored_words.push_back(temp);
        }
    
        infile.close();
    }
    
    
    
    void FileReader::add_ignore()
    {
     string temp;
    
        outfile.open("ignore.dat",ios::app);
     if(outfile.fail())
        {
            cout << "\n\n\t\aOUTPUT FILE WRITE FAIL!"
                 << "\n\t(Program will now terminate)"
                 << "\n\n\tPress [ENTER] to Continue...";
    
            cin.get();
    
            exit(1);
        }
    
            cout << "\n\n\n\tEnter word to be ignored: ";
            cin  >> temp;
    
            temp += ' ';
    
            outfile << temp;
    
            outfile.close();
    }
    
    
    void FileReader::file_count()
    {
        bool match;
    
        string temp;
        int back = ignored_words.size();
    
    
        cout << "\n\n\tExample:  C:\\myfile.txt"
             << "\n\t-------"
             << "\n\n\tEnter the path to your file to count: ";
        cin     >> temp;
    
        infile.clear();
        infile.open(temp.c_str(),ios::in);
    
        if(infile.fail())
        {
            cout << "\n\n\t\aCannot Locate File -OR- File Does Not Exist!"
                 << "\n\n\tPress [ENTER] to continue...";
    
            cin.ignore();
            cin.get();
    
            return;
      }
    
        infile.clear();
    
     while( infile >> temp )
        {
            match = false;
    
            for(int c=0; !match && c<back; c++)
    
                if(temp == ignored_words[c])
    
                    match = true;
    
            if(!match)
    
                word_counter++;
        }
    
        infile.close();
    
        counted = true;
    
    }
    The last few lines is where my problem is I just dont see it. It is not ignoring words in the ignore.dat file.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. errors using my parser- MSVC++ 6, bhtypes.h
    By AeroHammer in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2005, 06:11 PM
  3. opengl Nehe tutorial errors when compiling
    By gell10 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2003, 08:09 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

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