mercredi 28 mars 2018

Why do I get stuck in an infinite loop after calling a function?

I'm supposed to make a function that receives two arguments: a sentence(std::string) and bunch of phrases(std::vector>). Now for all words in a sentence that are contained in the vector, I need to make their palindrome, and stick them together, e.g. compile -> compileelipmoc. I also need to make sure that input is available up until two ENTER's are pressed. The problem occurs after calling the function, where I seem to get stuck in an infinite loop. Why am I getting this infinite loop?

#include <iostream>
#include <vector>
#include <string>
typedef std::vector<std::string> Vektor; 
typedef std::string String;

void ReverseString(String &s1)
{
char temp(0);
for(int i(0); i < s1.size()/2; i++) {
    temp = s1.at(i);
    s1.at(i) = s1.at(s1.length()-1-i);
    s1.at(s1.length()-1-i) = temp;
}
}

void CreatePalindrome(String s, Vektor v)
{
bool white_space(true);
bool go_on(false);
String compare;
for(int i(0); i < s.size(); i++) {
    for(;;) {
        if(s.at(i) == '\n' || i == s.size()-1) {
            go_on == true;
            break;
        }
        compare+=s.at(i);
    }
    if(go_on) {
        for(int j(0); j < v.size(); j++) {
            if(compare == v.at(j)) {
                ReverseString(v.at(j));
                if(i != s.size()-1) v.at(j)+=' ';
                s.insert(i, v.at(j));
            }
        }
    }
    compare.clear();
}
}

int main ()
{
String sentence, phrase;
Vektor v1;
char character(0);
std::cout << "Enter your sentence: ";
std::getline(std::cin, sentence);
std::cout << "Enter phrases: ";
for(;;) {
    character = std::cin.get();
    if(character == '\n') break;
    for(;;) {
        phrase.push_back(character);
        character = std::cin.get();
        if(character == '\n') break;
    }
    v1.push_back(phrase);
    phrase.clear();
}
CreatePalindrome(sentence, v1);
std::cout << "After the transformation, the sentence is: " << sentence;
return 0;
}

Aucun commentaire:

Enregistrer un commentaire