dimanche 19 avril 2020

Why am I getting a segment fault for my for one of my loops in this code? And where is it?

This program is supposed to change the most commonly occurring one letter word in a text with the most commonly occurring one letter word in the English Language. The rest of the items aren't that important for the purposes of this question. The most recently changes for loops are the one just mentioned and one having to do with common occurrence of two letter words. Any ideas? I also understand that the code is sloppy, so please forgive that.

#include <iostream>
#include <vector>

using namespace std;

vector<string> split(const string& s)
{
    vector<string> ret;
    typedef string::size_type string_size;
    string_size i = 0;

    // invariant: we have processed characters [original value of i, i) 
    while (i != s.size())
    {
        // ignore leading blanks
        // invariant: characters in range [original i, current i) are all spaces
     while (i != s.size() && isspace(s[i]))
         ++i;

     // find end of next word
     string_size j = i;
     // invariant: none of the characters in range [original j, current j)is a space
     while (j != s.size() && !isspace(s[j]))
         j++;
         // if we found some nonwhitespace characters 
         if (i != j) {
             // copy from s starting at i and taking j - i chars
             ret.push_back(s.substr(i, j - i));
             i = j;
         }
    }
    return ret;
}

int main() {
    string s;
    vector<string> allOneLetter{"I","A"};
    vector<string> twoLetterInput;
    vector<string> oneLetterInput;


    vector<string> allTwoLetter{"AM","AN","AR","AT",
                                    "AX","AY","BE","BY","DO","EW","EX","GO","HA","HE",
                                    "HI","ID","IF","IN","IS","IT","MA","ME","MY","NO",
                                    "OD","OF","OH","OK","ON","OR","OP","OS","OW","OX",
                                    "OY","PE","PI","PO","QI","RE","SH","SI","SO","TA",
                                    "TE","TO","UH","UM","UN","UP","US","UT","WE","WO",
                                    "XI","YA","YE","YO","IQ"};

    vector<string> commonTwoLetter{"AM","AN","AR","AT",
                                "AX","AY","BE","BY","DO","GO","HE",
                                "HI","ID","IF","IN","IS","IT","ME","MY","NO",
                                "OD","OF","OH","OK","ON","OR","OW",
                                "PI","PO","SO","TO","UN","UP","US","WE","WO",
                                "XI","YA","YE","YO","IQ"};


    // read and split each line of input to put in vector
    while (getline(cin, s)) {
        vector<string> v = split(s);

        //find all words with one letters and set them to I and A respectively for trial
        for (vector<string>::size_type i = 0; i != v.size(); ++i){
            if(v[i].size() == 1){
                oneLetterInput.push_back(v[i]);
            }
            for(unsigned int j = 0; j < v.size(); j++){
                if(oneLetterInput[i] == v[j]){
                    v[i] = oneLetterInput[j];
                }
            }

        }
        //find most common occurrence within one letters found in text input
        for(unsigned int i = 0; i < oneLetterInput.size(); i++){
            int max = 0;
            int newMax = 0;
            int count = 0;
            for(unsigned int j=1; j < oneLetterInput.size()-1; j++){
                if(oneLetterInput[i] == oneLetterInput[j]){
                    count ++;
                    if(count > max){
                        newMax = count;
                        // int indexNewMax = j;
                    }
                }

                for(unsigned int k = 0; k < v.size(); k++){
                    if(v[k] == oneLetterInput[j]){
                        v[k] = oneLetterInput[j];
                    }
                }
            }
            cout << "Count: " << count << ", FOR: " << twoLetterInput[i][0] << " IN: " << twoLetterInput[i] << endl;
        }


        //find all words with two letters and put in vector twoLetterInput
        for (vector<string>::size_type i = 0; i != v.size(); ++i){
            if(v[i].size() == 2){
                twoLetterInput.push_back(v[i]);
            }

        }
        for(unsigned int j= 1; j < twoLetterInput.size(); j++){
            cout << twoLetterInput[j] << endl;
        }

        //find most common occurrence within two letters found in text input
        for(unsigned int i = 0; i < twoLetterInput.size(); i++){
            int max = 0;
            int newMax = 0;
            int count = 0;
            for(unsigned int j=1; j < twoLetterInput.size()-1; j++){
                if(twoLetterInput[i][0] == twoLetterInput[j][0] || twoLetterInput[0][j] == twoLetterInput[j][1] ){
                    count ++;
                    if(count > max){
                        newMax = count;
                    }
                }
            }
            cout << "Count: " << count << ", FOR: " << twoLetterInput[i][0] << " IN: " << twoLetterInput[i] << endl;
        }
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire