Thread: substr in a for loop isn't working correctly

  1. #16
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    This function is obviously beyond my level of comprehension at the moment, so if its alright with you I'll just walk away from it and say it was too hard for me to do right. Maybe another day I will have the skill to do this function, but that day is not today. Thank you all for your help and patience.

  2. #17
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I realized I could find the matching chars OK.
    But the problem was when I got a result,
    the matching string from the database was returned,
    with other strings from the database that didn't match.

    So I thought about this and realized I didn't have the skill to do this.
    But, I did have the skill to open a database file, get a string, then write the single string to a file,
    then open this file and look at it for a matching char (and I know how to do this, laserlight showed me how.)
    then if the matching chars are there,
    I display the string, and since there is only one string in the file, only one string is displayed.
    Heh, so I made the code and it works fine now.
    Here it is.

    Basically I here to ask if there is any bugs in my program, because it has worked in my tests.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib> // exit()
    
    using namespace std;
    
    int main()
    {
        std::ifstream fin1("holdSentence.txt", fstream::app), fin2("holdDatabase.txt", fstream::app);
        if (!fin1 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
        int count = 0;
        int countOne = 0;
        string holdD;
        string holdS;
        getline( fin1, holdS );
    
        while( getline( fin2, holdD ) ){
            std::ofstream fout("tempData.txt");
            if (!fout) { std::cerr<<"file error\n"; std::exit(1); }
            fout << holdD;
            fout.close();
    
            std::ifstream fin3("tempData.txt", fstream::app);
            if (!fin3) { std::cerr<<"file error\n"; std::exit(1); }
            string tempD;
            getline(fin3, tempD);
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if ((holdS[a] == tempD[b]) && (holdS[a] != ' ')) {
                        count++;
                        if (tempD[b] == '1') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            fin3.close();
            count = 0;
            countOne = 0;
        }
        fin1.close();
        fin2.close();
    
        return 0;
    }
    This is another try, I probably should just quit, but I had this idea and it worked, so here I am again...

  3. #18
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Can you explain the difference between your code

    Code:
            std::ofstream fout("tempData.txt");
            if (!fout) { std::cerr<<"file error\n"; std::exit(1); }
            fout << holdD;
            fout.close();
     
            std::ifstream fin3("tempData.txt", fstream::app);
            if (!fin3) { std::cerr<<"file error\n"; std::exit(1); }
            string tempD;
            getline(fin3, tempD);
    and this line:

    Code:
    string tempD = holdD;
    ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #19
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    H nvoigt,

    Thanks for that, it works fine with your suggestion. I have made another tweak to use the code laserlight gave me.

    Here is the new program;

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib> // exit()
    
    using namespace std;
    
    int main()
    {
        std::ifstream fin1("holdSentence.txt", fstream::app), fin2("holdDatabase.txt", fstream::app);
        if (!fin1 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
        int count = 0;
        int countOne = 0;
        string holdD;
        string tempD;;
        string holdS;
        getline( fin1, holdS );
    
        while( getline( fin2, holdD ) ){
            tempD = holdD;
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if (tempD.find(holdS[a]) != string::npos && holdS[a] != ' ') {
                        count++;
                        if (tempD[b] == '1') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            count = 0;
            countOne = 0;
        }
        fin1.close();
        fin2.close();
    
        std::ifstream fin3("holdSentence.txt", fstream::app), fin4("holdDatabase.txt", fstream::app);
        if (!fin3 || !fin4) { std::cerr<<"file error\n"; std::exit(1); }
        count = 0;
        countOne = 0;
        getline( fin3, holdS );
    
        while( getline( fin4, holdD ) ){
            tempD = holdD;
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if (tempD.find(holdS[a]) != string::npos && holdS[a] != ' ') {
                        count++;
                        if (tempD[b] == '2') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            count = 0;
            countOne = 0;
        }
        fin3.close();
        fin4.close();
    
        std::ifstream fin5("holdSentence.txt", fstream::app), fin6("holdDatabase.txt", fstream::app);
        if (!fin5 || !fin6) { std::cerr<<"file error\n"; std::exit(1); }
        count = 0;
        countOne = 0;
        getline( fin5, holdS );
    
        while( getline( fin6, holdD ) ){
            tempD = holdD;
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if (tempD.find(holdS[a]) != string::npos && holdS[a] != ' ') {
                        count++;
                        if (tempD[b] == '3') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            count = 0;
            countOne = 0;
        }
        fin5.close();
        fin6.close();
    
        std::ifstream fin7("holdSentence.txt", fstream::app), fin8("holdDatabase.txt", fstream::app);
        if (!fin7 || !fin8) { std::cerr<<"file error\n"; std::exit(1); }
        count = 0;
        countOne = 0;
        getline( fin7, holdS );
    
        while( getline( fin8, holdD ) ){
            tempD = holdD;
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if (tempD.find(holdS[a]) != string::npos && holdS[a] != ' ') {
                        count++;
                        if (tempD[b] == '4') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            count = 0;
            countOne = 0;
        }
        fin7.close();
        fin8.close();
    
        std::ifstream fin9("holdSentence.txt", fstream::app), fin10("holdDatabase.txt", fstream::app);
        if (!fin9 || !fin10) { std::cerr<<"file error\n"; std::exit(1); }
        count = 0;
        countOne = 0;
        getline( fin9, holdS );
    
        while( getline( fin10, holdD ) ){
            tempD = holdD;
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if (tempD.find(holdS[a]) != string::npos && holdS[a] != ' ') {
                        count++;
                        if (tempD[b] == '5') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            count = 0;
            countOne = 0;
        }
        fin9.close();
        fin10.close();
    
        std::ifstream fin11("holdSentence.txt", fstream::app), fin12("holdDatabase.txt", fstream::app);
        if (!fin11 || !fin12) { std::cerr<<"file error\n"; std::exit(1); }
        count = 0;
        countOne = 0;
        getline( fin11, holdS );
    
        while( getline( fin12, holdD ) ){
            tempD = holdD;
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if (tempD.find(holdS[a]) != string::npos && holdS[a] != ' ') {
                        count++;
                        if (tempD[b] == '6') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            count = 0;
            countOne = 0;
        }
        fin11.close();
        fin12.close();
    
        std::ifstream fin13("holdSentence.txt", fstream::app), fin14("holdDatabase.txt", fstream::app);
        if (!fin13 || !fin14) { std::cerr<<"file error\n"; std::exit(1); }
        count = 0;
        countOne = 0;
        getline( fin13, holdS );
    
        while( getline( fin14, holdD ) ){
            tempD = holdD;
    
            for (string::size_type a = 0; a < holdS.length(); ++a) {
                for (string::size_type b = 0; b < tempD.length(); ++b) {
                    if (tempD.find(holdS[a]) != string::npos && holdS[a] != ' ') {
                        count++;
                        if (tempD[b] == '7') {
                            countOne++;
                        }
                    }
                }
            }
            if(count > 2 && countOne > 0){
                cout << endl << tempD << endl;
                break;
            }
            count = 0;
            countOne = 0;
        }
        fin13.close();
        fin14.close();
    
        return 0;
    }
    Here is the contents of the holdDatabase.txt;

    Code:
     aaaa 1
     aaaa 2
     aaaa 3
     aaaa 4
     aaaa 5
     aaaa 6
     aaaa 7
    And the contents of holdSentence.txt;

    Code:
     badger 1
    And the results;

    Code:
     aaaa 1
    
     aaaa 2
    
     aaaa 3
    
     aaaa 4
    
     aaaa 5
    
     aaaa 6
    
     aaaa 7
    
    Process returned 0 (0x0)   execution time : 0.016 s
    Press any key to continue.
    So everything looks fine to me. But is there any bugs or something else you can point out? Thanks to you and laserlight I have this thing fixed it looks like!

  5. #20
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    After watching this thread for a few hours I decided the function was OK to start my renaming and cleaning up of my program, but after trying the function out I found it wasn't working as it should, so here is the new version;

    Code:
    void findMatch(){
      
        int countOne = 0;
    	int countTwo = 0;
    	int countThree = 0;
    	int countFour = 0;
    	int countFive = 0;
    	int countSix = 0;
    	int countSeven = 0;
    	int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count5 = 0;
        int count6 = 0;
        int count7 = 0;
        int foundOne = 0;
    	int foundTwo = 0;
    	int foundThree = 0;
    	int foundFour = 0;
    	int foundFive = 0;
    	int foundSix = 0;
    	int foundSeven = 0;
        string holdD;
        string tempD;
    	string tempD2;
        string holdS;
    	string dataBaseSentence1;
        string dataBaseSentence2;
        string dataBaseSentence3;
        string dataBaseSentence4;
        string dataBaseSentence5;
        string dataBaseSentence6;
        string dataBaseSentence7;
    
    	std::ifstream fin1("holdSentence.txt", fstream::app), fin2("holdDatabase.txt", fstream::app);
        if (!fin1 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
    	
        getline( fin1, holdS );
    
        while( getline( fin2, holdD ) ){
            tempD = holdD;
    		tempD2 = holdD;
    		countOne = 0;
    		foundOne = 0;
    
            for (string::size_type a = 0; a < tempD2.length(); ++a) {
    			if (tempD2[a] == '1') {
    				foundOne++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < holdS.length(); ++b) {
    			if (tempD.find(holdS[b]) != string::npos && holdS[b] != ' ' && holdS[b] != '  ') {
    				countOne++;
    			}
    		}
    
            if(countOne > 2 && foundOne > 0){
                dataBaseSentence1 = tempD;
    			count1 = countOne;
                break;
            }
            countOne = 0;
            foundOne = 0;
        }
        fin1.close();
        fin2.close();
    
        std::ifstream fin3("holdSentence.txt", fstream::app), fin4("holdDatabase.txt", fstream::app);
        if (!fin3 || !fin4) { std::cerr<<"file error\n"; std::exit(1); }
        
        getline( fin3, holdS );
    
        while( getline( fin4, holdD ) ){
            tempD = holdD;
    		tempD2 = holdD;
    		countTwo = 0;
    		foundTwo = 0;
    
            for (string::size_type a = 0; a < tempD2.length(); ++a) {
    			if (tempD2[a] == '2') {
    				foundTwo++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < holdS.length(); ++b) {
    			if (tempD.find(holdS[b]) != string::npos && holdS[b] != ' ' && holdS[b] != '  ') {
    				countTwo++;
    			}
    		}
    
            if(countTwo > 2 && foundTwo > 0){
                dataBaseSentence2 = tempD;
    			count2 = countTwo;
                break;
            }
            countTwo = 0;
            foundTwo = 0;
        }
        fin3.close();
        fin4.close();
    
        std::ifstream fin5("holdSentence.txt", fstream::app), fin6("holdDatabase.txt", fstream::app);
        if (!fin5 || !fin6) { std::cerr<<"file error\n"; std::exit(1); }
        
        getline( fin5, holdS );
    
        while( getline( fin6, holdD ) ){
            tempD = holdD;
    		tempD2 = holdD;
    		countThree = 0;
    		foundThree = 0;
    
            for (string::size_type a = 0; a < tempD2.length(); ++a) {
    			if (tempD2[a] == '3') {
    				foundThree++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < holdS.length(); ++b) {
    			if (tempD.find(holdS[b]) != string::npos && holdS[b] != ' ' && holdS[b] != '  ') {
    				countThree++;
    			}
    		}
    
            if(countThree > 2 && foundThree > 0){
                dataBaseSentence3 = tempD;
    			count3 = countThree;
                break;
            }
            countThree = 0;
            foundThree = 0;
        }
        fin5.close();
        fin6.close();
    		//////////////////////
        std::ifstream fin7("holdSentence.txt", fstream::app), fin8("holdDatabase.txt", fstream::app);
        if (!fin7 || !fin8) { std::cerr<<"file error\n"; std::exit(1); }
        
        getline( fin7, holdS );
    
        while( getline( fin8, holdD ) ){
            tempD = holdD;
    		tempD2 = holdD;
    		countFour = 0;
    		foundFour = 0;
    
            for (string::size_type a = 0; a < tempD2.length(); ++a) {
    			if (tempD2[a] == '4') {
    				foundFour++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < holdS.length(); ++b) {
    			if (tempD.find(holdS[b]) != string::npos && holdS[b] != ' ' && holdS[b] != '  ') {
    				countFour++;
    			}
    		}
    
            if(countFour > 2 && foundFour > 0){
                dataBaseSentence4 = tempD;
    			count4 = countFour;
                break;
            }
            countFour = 0;
            foundFour = 0;
        }
        fin7.close();
        fin8.close();
    
        std::ifstream fin9("holdSentence.txt", fstream::app), fin10("holdDatabase.txt", fstream::app);
        if (!fin9 || !fin10) { std::cerr<<"file error\n"; std::exit(1); }
        
        getline( fin9, holdS );
    
        while( getline( fin10, holdD ) ){
            tempD = holdD;
    		tempD2 = holdD;
    		countFive = 0;
    		foundFive = 0;
    
            for (string::size_type a = 0; a < tempD2.length(); ++a) {
    			if (tempD2[a] == '5') {
    				foundFive++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < holdS.length(); ++b) {
    			if (tempD.find(holdS[b]) != string::npos && holdS[b] != ' ' && holdS[b] != '  ') {
    				countFive++;
    			}
    		}
    
            if(countFive > 2 && foundFive > 0){
                dataBaseSentence5 = tempD;
    			count5 = countFive;
                break;
            }
            countFive = 0;
            foundFive = 0;
        }
        fin9.close();
        fin10.close();
    			//////////////////////
        std::ifstream fin11("holdSentence.txt", fstream::app), fin12("holdDatabase.txt", fstream::app);
        if (!fin11 || !fin12) { std::cerr<<"file error\n"; std::exit(1); }
        
        getline( fin11, holdS );
    
        while( getline( fin12, holdD ) ){
            tempD = holdD;
    		tempD2 = holdD;
    		countSix = 0;
    		foundSix = 0;
    
            for (string::size_type a = 0; a < tempD2.length(); ++a) {
    			if (tempD2[a] == '6') {
    				foundSix++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < holdS.length(); ++b) {
    			if (tempD.find(holdS[b]) != string::npos && holdS[b] != ' ' && holdS[b] != '  ') {
    				countSix++;
    			}
    		}
    
            if(countSix > 2 && foundSix > 0){
                dataBaseSentence6 = tempD;
    			count6 = countSix;
                break;
            }
            countSix = 0;
            foundSix = 0;
        }
        fin11.close();
        fin12.close();
    			//////////////////////
        std::ifstream fin13("holdSentence.txt", fstream::app), fin14("holdDatabase.txt", fstream::app);
        if (!fin13 || !fin14) { std::cerr<<"file error\n"; std::exit(1); }
        
        getline( fin13, holdS );
    
        while( getline( fin14, holdD ) ){
            tempD = holdD;
    		tempD2 = holdD;
    		countSeven = 0;
    		foundSeven = 0;
    
            for (string::size_type a = 0; a < tempD2.length(); ++a) {
    			if (tempD2[a] == '7') {
    				foundSeven++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < holdS.length(); ++b) {
    			if (tempD.find(holdS[b]) != string::npos && holdS[b] != ' ' && holdS[b] != '  ') {
    				countSeven++;
    			}
    		}
    
            if(countSeven > 2 && foundSeven > 0){
                dataBaseSentence7 = tempD;
    			count7 = countSeven;
                break;
            }
            countSeven = 0;
            foundSeven = 0;
        }
        fin13.close();
        fin14.close();
    
    	//if(count1 > count2 && (count1 > count3) && (count1 > count4) && (count1 > count5) && (count1 > count6) && (count1 > count7))
            cout << endl << endl << "Matching chars: " << count1 << ", Program response; " << endl << dataBaseSentence1;
        //else if(count2 > count1 && (count2 > count3) && (count2 > count4) && (count2 > count5) && (count2 > count6) && (count2 > count7))
            cout << endl << endl << "Matching chars: " << count2 <<  ", Program response; " << endl << dataBaseSentence2;
        //else if(count3 > count2 && (count3 > count1) && (count3 > count4) && (count3 > count5) && (count3 > count6) && (count3 > count7))
            cout << endl << endl << "Matching chars: " << count3 <<  ", Program response; " << endl << dataBaseSentence3;
        //else if(count4 > count2 && (count4 > count3) && (count4 > count1) && (count4 > count5) && (count4 > count6) && (count4 > count7))
            cout << endl << endl << "Matching chars: " << count4 <<  ", Program response; " << endl << dataBaseSentence4;
        //else if(count5 > count2 && (count5 > count3) && (count5 > count4) && (count5 > count1) && (count5 > count6) && (count5 > count7))
            cout << endl << endl << "Matching chars: " << count5 <<  ", Program response; " << endl << dataBaseSentence5;
       // else if(count6 > count2 && (count6 > count3) && (count6 > count4) && (count6 > count5) && (count6 > count1) && (count6 > count7))
            cout << endl << endl << "Matching chars: " << count6 <<  ", Program response; " << endl << dataBaseSentence6;
        //else if(count7 > count2 && (count7 > count3) && (count7 > count4) && (count7 > count5) && (count7 > count6) && (count7 > count1))
            cout << endl << endl << "Matching chars: " << count7 <<  ", Program response; " << endl << dataBaseSentence7;
    }
    What it does is it takes the input string from hold sentence and compares it to the strings in holddatabase, and then returns strings from holddatabase that have a matching char with the holdsentence string.

  6. #21
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Your keyboard will probably need a new pair of ctrl-c/ctrl-v keys when this program is finished. People already told you, you won't listen. I will not repeat their mistakes
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #22
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Or you could write another program to do the copy pasting and call it your "AITM loop unroller".
    I think people actually did that before templates were invented.

  8. #23
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    If I build a loop it will loop for all seven numbers from 1-7, and my database only has three because the database it grown as I input sentences, it starts from nothing in the database and builds up as I input sentences.

    I would have made the loop but because of this condition I don't know how. So because you have shown me you know how with your john wayne attitude I hope you can show me how to loop through and not need every number to be in the database;

    here is the new function code;

    Code:
    void findMatch(){
    
        int matching_char_one = 0;
    	int matching_char_two = 0;
    	int matching_char_three = 0;
    	int matching_char_four = 0;
    	int matching_char_five = 0;
    	int matching_char_six = 0;
    	int matching_char_seven = 0;
    	int char_one_total = 0;
        int char_two_total = 0;
        int char_three_total = 0;
        int char_four_total = 0;
        int char_five_total = 0;
        int char_six_total = 0;
        int char_seven_total = 0;
        int char_one = 0;
    	int char_two = 0;
    	int char_three = 0;
    	int char_four = 0;
    	int char_five = 0;
    	int char_six = 0;
    	int char_seven = 0;
        string database;
        string inputSentence;
    	string dataBaseSentence1;
        string dataBaseSentence2;
        string dataBaseSentence3;
        string dataBaseSentence4;
        string dataBaseSentence5;
        string dataBaseSentence6;
        string dataBaseSentence7;
    
    	std::ifstream fin1("inputSentenceentence.txt"), fin2("holdDatabase.txt");
        if (!fin1 || !fin2) { std::cerr<<"file error\n"; std::exit(1); }
    
        getline( fin1, inputSentence );
    
        while( getline( fin2, database ) ){
    		matching_char_one = 0;
    		char_one = 0;
    
            for (string::size_type a = 0; a < database.length(); ++a) {
    			if (database[a] == '1') {
    				char_one++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < inputSentence.length(); ++b) {
    			if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
    				matching_char_one++;
    			}
    		}
    
            if(matching_char_one > 2 && char_one > 0){
                dataBaseSentence1 = database;
    			char_one_total = matching_char_one;
                break;
            }
            matching_char_one = 0;
            char_one = 0;
        }
        fin1.close();
        fin2.close();
    
        std::ifstream fin3("holdDatabase.txt");
        if (!fin3) { std::cerr<<"file error\n"; std::exit(1); }
    
        while( getline( fin3, database ) ){
    		matching_char_two = 0;
    		char_two = 0;
    
            for (string::size_type a = 0; a < database.length(); ++a) {
    			if (database[a] == '2') {
    				char_two++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < inputSentence.length(); ++b) {
    			if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
    				matching_char_two++;
    			}
    		}
    
            if(matching_char_two > 2 && char_two > 0){
                dataBaseSentence2 = database;
    			char_two_total = matching_char_two;
                break;
            }
            matching_char_two = 0;
            char_two = 0;
        }
        fin3.close();
    
        std::ifstream fin4("holdDatabase.txt");
        if (!fin4) { std::cerr<<"file error\n"; std::exit(1); }
    
        while( getline( fin4, database ) ){
    		matching_char_three = 0;
    		char_three = 0;
    
            for (string::size_type a = 0; a < database.length(); ++a) {
    			if (database[a] == '3') {
    				char_three++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < inputSentence.length(); ++b) {
    			if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
    				matching_char_three++;
    			}
    		}
    
            if(matching_char_three > 2 && char_three > 0){
                dataBaseSentence3 = database;
    			char_three_total = matching_char_three;
                break;
            }
            matching_char_three = 0;
            char_three = 0;
        }
        fin4.close();
    
        std::ifstream fin5("holdDatabase.txt");
        if (!fin5) { std::cerr<<"file error\n"; std::exit(1); }
    
        while( getline( fin5, database ) ){
    		matching_char_four = 0;
    		char_four = 0;
    
            for (string::size_type a = 0; a < database.length(); ++a) {
    			if (database[a] == '4') {
    				char_four++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < inputSentence.length(); ++b) {
    			if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
    				matching_char_four++;
    			}
    		}
    
            if(matching_char_four > 2 && char_four > 0){
                dataBaseSentence4 = database;
    			char_four_total = matching_char_four;
                break;
            }
            matching_char_four = 0;
            char_four = 0;
        }
        fin5.close();
    
        std::ifstream fin6("holdDatabase.txt");
        if (!fin6) { std::cerr<<"file error\n"; std::exit(1); }
    
        while( getline( fin6, database ) ){
    		matching_char_five = 0;
    		char_five = 0;
    
            for (string::size_type a = 0; a < database.length(); ++a) {
    			if (database[a] == '5') {
    				char_five++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < inputSentence.length(); ++b) {
    			if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
    				matching_char_five++;
    			}
    		}
    
            if(matching_char_five > 2 && char_five > 0){
                dataBaseSentence5 = database;
    			char_five_total = matching_char_five;
                break;
            }
            matching_char_five = 0;
            char_five = 0;
        }
        fin6.close();
    			//////////////////////
        std::ifstream fin7("holdDatabase.txt");
        if (!fin7) { std::cerr<<"file error\n"; std::exit(1); }
    
        while( getline( fin7, database ) ){
    		matching_char_six = 0;
    		char_six = 0;
    
            for (string::size_type a = 0; a < database.length(); ++a) {
    			if (database[a] == '6') {
    				char_six++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < inputSentence.length(); ++b) {
    			if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
    				matching_char_six++;
    			}
    		}
    
            if(matching_char_six > 2 && char_six > 0){
                dataBaseSentence6 = database;
    			char_six_total = matching_char_six;
                break;
            }
            matching_char_six = 0;
            char_six = 0;
        }
        fin7.close();
    			//////////////////////
        std::ifstream fin8("holdDatabase.txt");
        if (!fin8) { std::cerr<<"file error\n"; std::exit(1); }
    
        while( getline( fin8, database ) ){
    		matching_char_seven = 0;
    		char_seven = 0;
    
            for (string::size_type a = 0; a < database.length(); ++a) {
    			if (database[a] == '7') {
    				char_seven++;
    				break;
    			}
            }
    
    		for (string::size_type b = 0; b < inputSentence.length(); ++b) {
    			if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
    				matching_char_seven++;
    			}
    		}
    
            if(matching_char_seven > 2 && char_seven > 0){
                dataBaseSentence7 = database;
    			char_seven_total = matching_char_seven;
                break;
            }
            matching_char_seven = 0;
            char_seven = 0;
        }
        fin8.close();
    
    	//if(char_one_total > char_two_total && (char_one_total > char_three_total) && (char_one_total > char_four_total)
          //&& (char_one_total > char_five_total)&& (char_one_total > char_six_total) && (char_one_total > char_seven_total))
    
            cout << endl << endl << "Matching chars: " << char_one_total << ", Program response; " << endl
            << dataBaseSentence1;
    /*_________________________________________________________/*______________________________________________________________________________________________*/
        //else if(char_two_total > char_one_total && (char_two_total > char_three_total) && (char_two_total > char_four_total)
        // && (char_two_total > char_five_total)&& (char_two_total > char_six_total) && (char_two_total > char_seven_total))
    
            cout << endl << endl << "Matching chars: " << char_two_total <<  ", Program response; " << endl
            << dataBaseSentence2;
    /*_________________________________________________________/*______________________________________________________________________________________________*/
        //else if(char_three_total > char_two_total && (char_three_total > char_one_total) && (char_three_total > char_four_total)
        // && (char_three_total > char_five_total)&& (char_three_total > char_six_total) && (char_three_total > char_seven_total))
    
            cout << endl << endl << "Matching chars: " << char_three_total <<  ", Program response; " << endl
            << dataBaseSentence3;
    /*_________________________________________________________/*______________________________________________________________________________________________*/
        //else if(char_four_total > char_two_total && (char_four_total > char_three_total) && (char_four_total > char_one_total)
        // && (char_four_total > char_five_total)&& (char_four_total > char_six_total) && (char_four_total > char_seven_total))
    
            cout << endl << endl << "Matching chars: " << char_four_total <<  ", Program response; " << endl
            << dataBaseSentence4;
    /*_________________________________________________________/*______________________________________________________________________________________________*/
        //else if(char_five_total > char_two_total && (char_five_total > char_three_total) && (char_five_total > char_four_total)
        // && (char_five_total > char_one_total)&& (char_five_total > char_six_total) && (char_five_total > char_seven_total))
    
            cout << endl << endl << "Matching chars: " << char_five_total <<  ", Program response; " << endl
            << dataBaseSentence5;
    /*_________________________________________________________/*______________________________________________________________________________________________*/
        //else if(char_six_total > char_two_total && (char_six_total > char_three_total) && (char_six_total > char_four_total)
        // && (char_six_total > char_five_total)&& (char_six_total > char_one_total) && (char_six_total > char_seven_total))
    
            cout << endl << endl << "Matching chars: " << char_six_total <<  ", Program response; " << endl
            << dataBaseSentence6;
    /*_________________________________________________________/*______________________________________________________________________________________________*/
        //else if(char_seven_total > char_two_total && (char_seven_total > char_three_total) && (char_seven_total > char_four_total)
        // && (char_seven_total > char_five_total)&& (char_seven_total > char_six_total) && (char_seven_total > char_one_total))
    
            cout << endl << endl << "Matching chars: " << char_seven_total <<  ", Program response; " << endl
            << dataBaseSentence7;
    }
    Sorry if I spoke harshly a bit there, but sheesh, I'm trying here you know, it's not easy for me.

  9. #24
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    it's not easy for me
    It would be a lot easier if you listened to the people trying to help you. There are loops in your program. You know how they work. Use them. Break your problem down in very (!) small steps and solve one at a time. Right now I don't even know how I could help you because after 4 pages I don't have the slightest clue what you are trying to achieve with this mess of code.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Listen, you need to THINK when you copy and paste.

    > while( getline( fin2, database ) )
    OK, you've got a loop to do what you want.

    > while( getline( fin3, database ) )
    NOW PAY ATTENTION.
    Instead of blindly editing 'one' into 'two' and put numbers on the ends of variables, and so on, think!!!!!!!

    Here, in about 50 lines is your epic.
    Code:
    void findMatch(){
      std::ifstream fin1("inputSentenceentence.txt");
      if (!fin1) { std::cerr<<"file error\n"; std::exit(1); }
      string inputSentence;
      getline( fin1, inputSentence );
      fin1.close();
    
      std::ifstream finx("holdDatabase.txt");
      if (!finx) { std::cerr<<"file error\n"; std::exit(1); }
    
      const int maxMatches = 7;
      char matchList[] = "1234567";
      string dataBaseSentences[maxMatches];
      int charTotals[maxMatches] = { 0 };
    
      for ( int nMatch = 0 ; nMatch < maxMatches ; nMatch++ ) {
        finx.seekg(0, ios::beg);      // Start each scan at the start of the file
        finx.clear();                 // clear any errors such as EOF
    
        string database;
        while( getline( finx, database ) ){
          int matching_char_one = 0;
          int char_one = 0;
    
          for (string::size_type a = 0; a < database.length(); ++a) {
            if (database[a] == matchList[nMatch]) {
              char_one++;
              break;
            }
          }
    
          for (string::size_type b = 0; b < inputSentence.length(); ++b) {
            if (database.find(inputSentence[b]) != string::npos && inputSentence[b] != ' ') {
              matching_char_one++;
            }
          }
    
          if(matching_char_one > 2 && char_one > 0){
            dataBaseSentences[nMatch] = database;
            charTotals[nMatch] = matching_char_one;
            break;
          }
        }
      }
      finx.close();
    
      for ( int nMatch = 0 ; nMatch < maxMatches ; nMatch++ ) {
          cout << endl << endl
                << "Matching chars: " << charTotals[nMatch]
                << ", Program response; " << endl
                << dataBaseSentences[nMatch];
      }
    }

    Try learning more of the language, rather than the absolute minimal subset you seem to be clinging to.
    Because the code you're writing could just as well be written in another minimalist language, and it would be just as annoying as the code you're posting at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #26
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Hi Salem,

    Thanks for the code, the line;
    Code:
    finx.seekg(0, ios::beg);
    Made the while loop only read three strings, I counted with cout.

    And this is what your function returned with no alterations to your function;
    Code:
    Enter your sentence, end it with a period: zzzy.
    
    Input sentence to program, from input.txt;
     zzzy
    
    Matching chars: 5, Program response;
     zzyy 1
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Press any key to continue . . .
    And the result if I removed the line;
    Code:
    finx.seekg(0, ios::beg);
    Code:
    Enter your sentence, end it with a period: zzzy.
    
    Input sentence to program, from input.txt;
     zzzy
    
    Matching chars: 5, Program response;
     zzyy 1
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Press any key to continue . . .
    So I changed your function a bit and here is the new function;

    Code:
    void findMatch(){
      std::ifstream fin1("inputSentenceentence.txt");
      if (!fin1) { std::cerr<<"file error\n"; std::exit(1); }
      string inputSentence;
      getline( fin1, inputSentence );
      fin1.close();
    
      std::ifstream finx("holdDatabase.txt");
      if (!finx) { std::cerr<<"file error\n"; std::exit(1); }
    
      const int maxMatches = 7;
      char matchList[] = "1234567";
      string dataBaseSentences[maxMatches];
      int charTotals[maxMatches] = { 0 };
      int one = 0;
      int two = 0;
      int three = 0;
      int four = 0;
      int five = 0;
      int six = 0;
      int seven = 0;
    
      for ( int nMatch = 0 ; nMatch < maxMatches ; nMatch++ ) {
        finx.clear();                 // clear any errors such as EOF
    
        string database;
        while( getline( finx, database ) ){
          int matching_char_one = 0;
          int matching_char_two = 0;
          int matching_char_three = 0;
          int matching_char_four = 0;
          int matching_char_five = 0;
          int matching_char_six = 0;
          int matching_char_seven = 0;
          int char_one = 0;
          int char_two = 0;
          int char_three = 0;
          int char_four = 0;
          int char_five = 0;
          int char_six = 0;
          int char_seven = 0;
    
          for (string::size_type a = 0; a < database.length(); ++a) {
            if ((database[a] == matchList[0]) && (one == 0)) {
              char_one++;
              break;
            }
            if ((database[a] == matchList[1]) && (two == 0)) {
              char_two++;
              break;
            }
            if ((database[a] == matchList[2]) && (three == 0)) {
              char_three++;
              break;
            }
            if ((database[a] == matchList[3]) && (four == 0)) {
              char_four++;
              break;
            }
            if ((database[a] == matchList[4]) && (five == 0)) {
              char_five++;
              break;
            }
            if ((database[a] == matchList[5]) && (six == 0)) {
              char_six++;
              break;
            }
            if ((database[a] == matchList[6]) && (seven == 0)) {
              char_seven++;
              break;
            }
          }
    
          for (string::size_type b = 0; b < inputSentence.length(); ++b) {
            if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_one == 1) && (one == 0)) {
              matching_char_one++;
            }
            if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_two == 1) && (two == 0)) {
              matching_char_two++;
            }
            if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_three == 1) && (three == 0)) {
              matching_char_three++;
            }
            if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_four == 1) && (four == 0)) {
              matching_char_four++;
            }
            if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_five == 1) && (five == 0)) {
              matching_char_five++;
            }
            if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_six == 1) && (six == 0)) {
              matching_char_six++;
            }
            if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_seven == 1) && (seven == 0)) {
              matching_char_seven++;
            }
          }
          if((matching_char_one > 2) && (char_one == 1) && (one == 0)){
            dataBaseSentences[0] = database;
            charTotals[0] = matching_char_one;
            one++;
            break;
          }
          if((matching_char_two > 2) && (char_two == 1) && (two == 0)){
            dataBaseSentences[1] = database;
            charTotals[1] = matching_char_two;
            two++;
            break;
          }
          if((matching_char_three > 2) && (char_three == 1) && (three == 0)){
            dataBaseSentences[2] = database;
            charTotals[2] = matching_char_three;
            three++;
            break;
          }
          if((matching_char_four > 2) && (char_four == 1) && (four == 0)){
            dataBaseSentences[3] = database;
            charTotals[3] = matching_char_four;
            four++;
            break;
          }
          if((matching_char_five > 2) && (char_five == 1) && (five == 0)){
            dataBaseSentences[4] = database;
            charTotals[4] = matching_char_five;
            five++;
            break;
          }
          if((matching_char_six > 2) && (char_six == 1) && (six == 0)){
            dataBaseSentences[5] = database;
            charTotals[5] = matching_char_six;
            six++;
            break;
          }
          if((matching_char_seven > 2) && (char_seven == 1) && (seven == 0)){
            dataBaseSentences[6] = database;
            charTotals[6] = matching_char_seven;
            seven++;
            break;
          }
        }
      }
      finx.close();
    
      for ( int nMatch = 0 ; nMatch < maxMatches ; nMatch++ ) {
          cout << endl << endl
            << "Matching chars: " << charTotals[nMatch]
            << ", Program response; " << endl
            << dataBaseSentences[nMatch];
      }
    }
    And the results the new function returns;
    Code:
    Enter your sentence, end it with a period: zzzy.
    
    Input sentence to program, from input.txt;
     zzzy
    
    Matching chars: 5, Program response;
     zzyy 1
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 4, Program response;
     yyyzz 4
    
    Matching chars: 3, Program response;
     zzzzz 5
    
    Matching chars: 0, Program response;
    
    
    Matching chars: 0, Program response;
    
    
    Press any key to continue . . .
    These results match the function I previously posted before your reply.

    So I hope my alterations to the function is OK and now I can worry about outputting only the string with the highest number of matching chars? And tips on how I would do that? And thank you very much for your contribution to my program, thats a genuine statement, thank you.

  12. #27
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Salem! How dare you try and take his pointless verbosity away!

    Good on you for adding redundant content instead of just fixing the tiny bug! Show 'em how it's done!

    Soma

  13. #28
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I just started learning c++ from thenewboston about two months ago, I watched up to lesson 40 once and up to lessons 1 to 14 or so a couple of times since then, and I watched some of xoax.net's c++ videos.
    I have been mostly learning c and c#, but a person at AI.com said to learn c++ so I started to and made this AI program a bit. Then I come here and I get into a rough part of the internet for me.

    I fixed the code Salem gave me, sorry if it wasn't up to your elite standard, but in my two odd months of C++ its pretty cool!

  14. #29
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    a person at AI.com said to learn c++ so I started to and made this
    Maybe you should go back to AI.com because obviously you listen to people there. An internet message board is not worth posting if you don't take the advice given.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop not working Correctly
    By patneel in forum C Programming
    Replies: 4
    Last Post: 03-15-2012, 07:25 AM
  2. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  3. Pointers are not working correctly
    By adrian_fpd in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 07:55 PM
  4. function not working correctly
    By mackieinva in forum C Programming
    Replies: 0
    Last Post: 09-29-2007, 08:22 PM
  5. File I/O not working correctly
    By gL_nEwB in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2006, 10:29 PM