I test for a result, but any result over 1 fails

This is a discussion on I test for a result, but any result over 1 fails within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> #include <string.h> #include <fstream> #include <vector> #include <cstdlib> // exit() #include <cctype> // isalpha(), tolower() ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    229

    I test for a result, but any result over 1 fails

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <fstream>
    #include <vector>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    using namespace std;
    
    void readByString(std::string f1, std::string f2, std::string f3){
        int count = 0;
        if(f1 == f2)
        count++;
        if(count > 0)
            cout << f1 << " " << f2 << endl << f3 << endl;
    
    }
    
    int main()
    {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.txt");
        std::ifstream fin2("file_in_2.txt");
        std::ofstream fout2("file_out_2.txt");
        if (!fin || !fout ||!fin2 || !fout2) { std::cerr<<"file error\n"; std::exit(1); }
    
        string temp3;
    
        while( getline( fin2, temp3 ) ){
            float c_;
            bool word = false;
            char letters[] = "abcdefghijklmnopqrstuvwxyz";
            char *this_letter;
    
            while ((c_ = fin.get()) != EOF) {
                if (c_ == '\'') continue; // skip apostrophe
                if (std::isalpha(c_)) {
                    if ((this_letter = strchr(letters, tolower(c_))) != NULL){
                        fout << this_letter[0];
                    }
                        word = true;
                }
                        else if (word) {
                            fout<<'\n';
                            word = false;
                        }
            }
            fout.close();
            fin.close();
            float c_2;
            bool word2 = false;
            char letters2[] = "abcdefghijklmnopqrstuvwxyz";
            char *this_letter2;
    
            while ((c_2 = fin2.get()) != EOF) {
                if (c_2 == '\'') continue; // skip apostrophe
                if (std::isalpha(c_2)) {
                    if ((this_letter2 = strchr(letters2, tolower(c_2))) != NULL){
                        fout2 << this_letter2[0];
                    }
                    word2 = true;
                }
                else if (word2) {
                    fout2<<'\n';
                    word2 = false;
                }
            }
            fout2.close();
            fin2.close();
    
            ifstream ifs ("file_out.txt");
            ifstream ifs2 ("file_out_2.txt");
            std::ifstream ifs3("file_in_2.txt");
            if (!ifs || !ifs2) { std::cerr<<"file error\n"; std::exit(1); }
    
            int j = 0;
            vector<string> text_file;
            string temp;
            vector<string> text_file2;
            string temp2;
            vector<string> text_file4;
            string temp4;
    
            if(ifs.is_open()){
                while( getline( ifs3, temp4 ) ){
                    while( getline( ifs, temp ), getline( ifs2, temp2 ), getline( ifs3, temp4 ) ){
                    for (j = 0; temp[j] != '\0'; j++)
                    temp[j] = tolower(temp[j]);
                    readByString(temp, temp2, temp4);
                    text_file.push_back( temp );
                    text_file2.push_back( temp2 );
                    }
                text_file4.push_back( temp4 );
                }
                ifs.close();
                ifs2.close();
                ifs3.close();
            }
        }
    }
    The text in file_in.txt;
    zeethe price is right

    The text in file_in_2.txt;
    a blue budgie
    zeethe price is alright

    Here are the console results;

    zeethe zeethe
    zeethe price is alright
    price price

    is is


    Process returned 0 (0x0) execution time : 0.125 s
    Press any key to continue.

    ___________________


    I want to test for more than one word. So in the readByString function I want to test if (count > 1), but when I use 1 instead of 0 the results fail.
    Right now it looks for the word "zeethe", but I want to find "price" too.

    I have been busy googling and trying to do this for about a day now, please help me.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    I can explain why you can't do what you want to do. No matter what you pass in to readByString, the most that count will amount to is 1, so a comparison will always fail. Check out what I did for you. A debugger, especially one as nice as Visual Studio has, could have also pointed this out for you:
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::endl;
    
    void readByString(std::string f1, std::string f2, std::string f3){
        int count = 0;
        cout << "count before comparison: " << count << endl;
        if(f1 == f2)
            count++;
        if(count > 1)
            cout << f1 << " " << f2 << endl << f3 << endl;
        else
            cout << "count after comparison: " << count << endl;
    }
    
    int main()
    {
        readByString("zeethe", "zeethe", "the");
        readByString("price", "price", "price");
        readByString("is", "right", "a");
        return 0;
    }
    count before comparison: 0
    count after comparison: 1
    count before comparison: 0
    count after comparison: 1
    count before comparison: 0
    count after comparison: 0

    C++ code - 24 lines - codepad

    To fix it: Just do what readByString() does in main(). There are better functions you could write. Start by removing all of the duplicate code.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Thank you, this code works;

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <fstream>
    #include <vector>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    using namespace std;
    
    int main()
    {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.txt");
        std::ifstream fin2("file_in_2.txt");
        std::ofstream fout2("file_out_2.txt");
        if (!fin || !fout ||!fin2 || !fout2) { std::cerr<<"file error\n"; std::exit(1); }
    
        vector<string> text_file3;
        string temp3;
    
        if(fin2.is_open()){
            while( getline( fin2, temp3 ) ){
                float c_;
                bool word = false;
                char letters[] = "abcdefghijklmnopqrstuvwxyz";
                char *this_letter;
    
                while ((c_ = fin.get()) != EOF) {
                    if (c_ == '\'') continue; // skip apostrophe
                    if (std::isalpha(c_)) {
                        if (word) fout<<"\n";
                        if ((this_letter = strchr(letters, tolower(c_))) != NULL){
                            fout << this_letter[0] << endl;
                            }
                            word = true;
                            }
                            else if (word) {
                                fout<<'\n';
                                word = false;
                                }
                                }
                                fout.close();
                                fin.close();
                float c_2;
                bool word2 = false;
                char letters2[] = "abcdefghijklmnopqrstuvwxyz";
                char *this_letter2;
    
                while ((c_2 = fin2.get()) != EOF) {
                    if (c_2 == '\'') continue; // skip apostrophe
                    if (std::isalpha(c_2)) {
                        if (word2) fout2<<"\n";
                        if ((this_letter2 = strchr(letters2, tolower(c_2))) != NULL){
                            fout2 << this_letter2[0] << endl;
                            }
                            word2 = true;
                            }
                            else if (word2) {
                                fout2<<'\n';
                                word2 = false;
                                }
                                }
                                fout2.close();
                                fin2.close();
    
                ifstream ifs ("file_out.txt");
                ifstream ifs2 ("file_out_2.txt");
                if (!ifs || !ifs2) { std::cerr<<"file error\n"; std::exit(1); }
    
                vector<string> text_file;
                string temp;
                vector<string> text_file2;
                string temp2;
                int count = 0;
                if(ifs.is_open()){
                    while( getline( ifs, temp ), getline( ifs2, temp2 ) ){
    
                        if(temp == temp2)
                            count++;
    
                        if(count > 1)
                        cout << temp << " " << temp2 << endl;
    
                        text_file.push_back( temp );
                        text_file2.push_back( temp2 );
                    }
                        ifs.close();
                        ifs2.close();
                }
                    text_file3.push_back( temp3 );
            }
        }
    }

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Now another question, once I get count >4, how would I print out the sentence ifs2 is using from fin2 in the console window?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Here is a cleaned up version of the SW;

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <fstream>
    #include <vector>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    using namespace std;
    
    int main()
    {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.txt");
        std::ifstream fin2("file_in_2.txt");
        std::ofstream fout2("file_out_2.txt");
        if (!fin || !fout ||!fin2 || !fout2) { std::cerr<<"file error\n"; std::exit(1); }
    
        string temp3;
    
        while( getline( fin2, temp3 ) ){
            char c_;
            bool word = false;
            char letters[] = "abcdefghijklmnopqrstuvwxyz";
            char *this_letter, *this_letter2;;
    
            while ((c_ = fin.get()) != EOF) {
                if (c_ == '\'') continue; // skip apostrophe
                if (std::isalpha(c_)) {
                    if (word) fout<<"\n";
                    if ((this_letter = strchr(letters, tolower(c_))) != NULL){
                        fout << this_letter[0] << endl;
                    }
                    word = true;
                }
                else if (word) {
                    fout<<'\n';
                    word = false;
                }
            }
            fout.close();
            fin.close();
    
            while ((c_ = fin2.get()) != EOF) {
                if (c_ == '\'') continue; // skip apostrophe
                if (std::isalpha(c_)) {
                    if (word) fout2<<"\n";
                    if ((this_letter2 = strchr(letters, tolower(c_))) != NULL){
                        fout2 << this_letter2[0] << endl;
                    }
                    word = true;
                }
                else if (word) {
                    fout2<<'\n';
                    word = false;
                }
            }
            fout2.close();
    
    
            ifstream ifs ("file_out.txt"), ifs2 ("file_out_2.txt");
    
            if (!ifs || !ifs2) { std::cerr<<"file error\n"; std::exit(1); }
    
            string ifs_char[100];
            string ifs_char2[100];
            int count = 0;
    
            for (int a=0;a != '\n';a++){
                for (int b=0;b != '\n';b++){
                    ifs >> ifs_char[a];
                    ifs2 >> ifs_char2[b];
                    if(ifs_char[a] == ifs_char2[b])
                        count++;
                    if(count > 4){
                        cout << ifs_char[a] << " " << ifs_char2[b] << endl;
                        break;
                    }
                }
            }
    
            ifs.close();
            ifs2.close();
        }
        fin2.close();
    }
    Here is the results in the console window;

    ______________
    h h
    e e
    p p
    r r
    i i
    c c
    e e
    i i
    s s
    r a

    Process returned 0 (0x0) execution time : 0.016 s
    Press any key to continue.

    __________________

    This is the text in the fin file; zeethe price is right

    This is the text in the fin2 file;

    a blue budgie
    zeethe price is alright
    __________________

    You can see the ifs2 file is from the fin2 file,
    and when the count condition is true I want to display the sentence from fin2 that ifs2 is using,
    below the list of words I just showed.

    So my results are missing a few letters "zeethe" is shown as "he" letter.
    Probably because the cout starts to print after 4 matches, and that is at letter 'h'.

    I want to print the sentence from fin2 when the count condition is true.

    Thanks for showing me where I was going wrong.
    Last edited by jeremy duncan; 05-22-2012 at 03:30 AM.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Well I got it going, here is the code;

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <fstream>
    #include <vector>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    using namespace std;
    
    int main()
    {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.txt");
        std::ifstream fin2("file_in_2.txt");
        std::ofstream fout2("file_out_2.txt");
        if (!fin || !fout ||!fin2 || !fout2) { std::cerr<<"file error\n"; std::exit(1); }
    
        string temp3;
    
        while( getline( fin2, temp3 ) ){
            char c_;
            bool word = false;
            char letters[] = "abcdefghijklmnopqrstuvwxyz";
            char *this_letter, *this_letter2;;
    
            while ((c_ = fin.get()) != EOF) {
                if (c_ == '\'') continue; // skip apostrophe
                if (std::isalpha(c_)) {
                    if (word) fout<<"\n";
                    if ((this_letter = strchr(letters, tolower(c_))) != NULL){
                        fout << this_letter[0] << endl;
                    }
                    word = true;
                }
                else if (word) {
                    fout<<'\n';
                    word = false;
                }
            }
            fout.close();
            fin.close();
    
            while ((c_ = fin2.get()) != EOF) {
                if (c_ == '\'') continue; // skip apostrophe
                if (std::isalpha(c_)) {
                    if (word) fout2<<"\n";
                    if ((this_letter2 = strchr(letters, tolower(c_))) != NULL){
                        fout2 << this_letter2[0] << endl;
                    }
                    word = true;
                }
                else if (word) {
                    fout2<<'\n';
                    word = false;
                }
            }
            fout2.close();
            fin2.close();
    
            ifstream ifs ("file_out.txt"), ifs2 ("file_out_2.txt"), fin2("file_in_2.txt");
    
            if (!ifs || !ifs2 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
    
            string ifs_char[100];
            string ifs_char2[100];
            string temp;
            string temp2;
            int count = 0;
            int count2 = 0;
            while(getline(fin2, temp2)){
                while(getline(fin2, temp)){
                    for (int a=0;a != '\n';a++){
                        for (int b=0;b != '\n';b++){
                            ifs >> ifs_char[a];
                            fin2 >> ifs_char2[b];
                            if(ifs_char[a] == ifs_char2[b])
                                count++;
                            if(count > 4 && count2 == 0){
                                cout << temp << endl;
                                count2++;
                            }
                        }
                    }
                }
            }
    
            ifs.close();
            ifs2.close();
            fin2.close();
        }
    
    }
    ________________________
    This is the text in the fin file; zeethe price is right

    This is the text in the fin2 file;

    a blue budgie
    zeethe price is alright
    ________________________

    And the results;

    zeethe price is alright

    Process returned 0 (0x0) execution time : 0.000 s
    Press any key to continue.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    To explain what I was trying to do; I was comparing two text files, and finding the best matching string between the two text files.

    This way I can write a string to the fin file, and the fin2 file holds a lot of lines and I look through fin2 for the best match to the sentence in fin.

    Like searching through a database.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    char c_;
    bool word = false;
    char letters[] = "abcdefghijklmnopqrstuvwxyz";
    char *this_letter, *this_letter2;;
     
    while ((c_ = fin.get()) != EOF) {
    
    ...
    
    while ((c_ = fin2.get()) != EOF) {
    The get member function returns an int. This is important when comparing against EOF.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I have this code going, which loops through a string and then displays it in the console.

    Here is the code;

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // exit()
    
    using namespace std;
    
    int main()
    {
        std::ifstream fin2("file_in_2.txt");
        if (!fin2) { std::cerr<<"file error\n"; std::exit(1); }
    
        string temp3;
    
        while( getline( fin2, temp3 ) ){
    
            for (int a=0;a < temp3.length();a++){
                if (temp3[a] == '\0')
                          {
                                 break;
                          }
                          else if ( temp3[a] == '.' )
                          {
                              temp3[a] = '\n';
                              cout << temp3;
                          }
            }
        }
    
        fin2.close();
    }
    What I want to do is have a string from one file, and then compare it with multiple strings from another file, and then based on how much chars match between the two files strings I want to display the string.

    So now that I can loop through a string I want to add another string from a different file and compare it with the strings in the file I am showing right now. May I ask you how I should do this?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Quote Originally Posted by jeremy duncan View Post
    What I want to do is have a string from one file, and then compare it with multiple strings from another file, and then based on how much chars match between the two files strings I want to display the string.
    Well about that, you shouldn't need to compare any characters from temp3 with the '\0' character. You are already controlling the loop with the length() function.

    Quote Originally Posted by jeremy duncan View Post
    What I want to do is have a string from one file, and then compare it with multiple strings from another file, and then based on how much chars match between the two files strings I want to display the string.

    So now that I can loop through a string I want to add another string from a different file and compare it with the strings in the file I am showing right now. May I ask you how I should do this?
    Well I think it is a mistake to compare all of the strings all at once. The logic is just too complicated to understand that way.
    Code:
    while there are more strings from file1:
        get a string1
        while there are more strings from file2:
            get a string2
            compare string1 and string2
            if characters match enough:
                display string
        end while
        rewind file2 to beginning
    end while
    I hope you can turn something like that into real code. I think it is more like what you want to do. You are welcome to comment if you think something like this is totally off.

    If there are more than 2 files, I still wouldn't change the algorithm two much. You can make this process a function and repeat it as many times as you like, by calling the function, and referring to different files.
    Last edited by whiteflags; 05-23-2012 at 05:38 AM.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    OK I got the code made and it seems to work.

    Here is file_in_1 contents;
    grapes
    apples
    oranges

    Here is file_in_2 contents;
    olive
    oranges
    apples


    Here is the code;

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // exit()
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        std::ifstream fin1("file_in_1.txt"), fin2("file_in_2.txt");
        if (!fin1 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
    
        string temp1;
        string temp2;
        int count = 0;
        int count2 = 0;
        vector<string> text_file2;
    
        while( getline( fin1, temp1 ) ){
            while( getline( fin2, temp2 ) ){
    
                for (unsigned int a=0;a < temp1.length();a++){
                    if ( temp1[a] == temp2[a] )
                        count++;
                    if(count == 4 && count2 == 0){
                        cout << temp2;
                        count2++;
                    }
                }
    
                text_file2.push_back( temp2 );
            }
        }
    
        fin1.close();
        fin2.close();
    }
    And here is the console results;

    apples
    Process returned 0 (0x0) execution time : 0.016 s
    Press any key to continue.
    ___________________________

    Thank you very much.
    Last edited by jeremy duncan; 05-23-2012 at 01:27 PM. Reason: edited code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No result out for this CP
    By leevenchoon in forum C Programming
    Replies: 12
    Last Post: 07-10-2011, 11:36 AM
  2. Help with result only...
    By Ervilha in forum C Programming
    Replies: 5
    Last Post: 08-19-2008, 11:29 AM
  3. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  4. What would be the result...
    By sreeramu in forum C Programming
    Replies: 8
    Last Post: 03-25-2008, 04:43 AM
  5. No Result when run *.exe
    By hednast in forum C Programming
    Replies: 6
    Last Post: 08-23-2005, 12:50 AM

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