2016-04-05 23 views
0
#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

vector <string> words; 

void splitSent (string sent); 



int main() 
{ 
string sent; 

cout << "Enter your sentence: " << endl; 
getline (cin, sent); 
splitSent (sent);  


string finalSent; 
for (unsigned int i = 0; i < words.size(); i++) 
{ 
    if (words[i] == "i") 
    { 
     finalSent += "I "; 
     i++; 
    } 

    if (words[i] == "instructor") 
    { 
     finalSent += "name of prof "; 
     i++; 
    } 

    finalSent += words[i]; 
    finalSent += " "; 
} 

cout << "Final sentence is: " << finalSent << "." << endl; 


return 0; 
} 


void splitSent (string sent) 
{ 
int Pos = 0; // Position 
string word; 

while (Pos < sent.length()) 
{ 
    while ((Pos < sent.length()) && (sent[Pos] != ' ')) 
    { 
     word += sent[Pos]; 
     Pos++; 
     if (sent[Pos] == '.') 
     { 
      break; 
     } 
    }; 
words.push_back(word); 
word = ""; 
Pos++; 
} 
} 

Bu, programım şu ana kadar "i" ile "I" değiştirmeye çalışıyorum ve "eğitmen" i, profemin adıyla değiştirmeye çalışıyorum. Ancak, her zaman bir cümlede ikiden fazla "i" var, bir hata mesajı alıyorum ve neden emin değilim. CümleHata, dönüş değeri 3221225477

+0

Bu kod derlenmez; En az çalışan bir örnek vermeniz gerekiyor. Örneğin, “splitSent” veya “checkSent” in ne yaptığını bilmiyoruz ve kelimelerin nereden geldiğini bilmiyoruz. Ayrıca, aldığınız hataları açıklamanız gerekir. – Mike

+0

Yea, yeni tamir ettim, bir bakabilir misin? –

+2

[çocuk süreci 3221225477 durumundan çıkıldı] (http://stackoverflow.com/questions/10306272/apache-crashing-parent-child-process-exited-with-status-3221225477-restarti) bunun bir segmentasyon hatası olacağını söylüyor - tahsis edilmeyen belleğe erişim. Onun için bahse girerim çünkü sen artik artar ve daha sonra ona erişirsin. – Obicere

cevap

1

'da "instructor" kelimesi varsa, aynı hata iletisini de i el ile artırmaya gerek yoktur. Bu arada for döngüsünün işi budur. i artırarak siz Obicere yorumlarda belirtildiği gibi manuel i artan ve daha sonra words[i] istiyorlar çünkü muhtemelen bir segment hataya alıyoruz, tanımsız hafızayı

string finalSent; 
    for (unsigned int i = 0; i < words.size(); i++) 
    { 
     if (words[i] == "i") 
     { 
      finalSent += "I "; 
      continue; 
      //i++; 
     } 

     if (words[i] == "instructor") 
     { 
      finalSent += "name of prof "; 
      continue; 
      //i++; 
     } 

     finalSent += words[i]; 
     finalSent += " "; 
    } 
0

erişen açıkçası Vektörün büyüklüğü aşılması ve edilmektedir. i değişkeninin olması gerektiği kadar yüksek olduğunda, "i" veya "eğitmen" in, cümlenin sonuna geldiğinde muhtemelen sadece segfault aldınız. Ama sonra i++ yaptınız, bu yüzden i olması gerekenden daha büyüktü ve words[i], sonunu geçecek olan words öğesinden birini istiyor. Segmentler böyle olur.

if ifadelerinize daha dikkatli olmalı ve onlardan sonra ne olacak? Bu arada, global değişkenleri kullanmak kötü bir fikirdir. Bu durumda, dosyanızın en üstünde words tanımlamamalısınız, ancak referans olarak iletmelisiniz. Ayrıca, iyi bir editör (emacs veya vim) kodunuzu düzenli tutabilir.

Ne istediğinizi tam olarak emin değilim, bu yüzden akış denetimini karıştırmış olabilirim, ancak bu kod istediğiniz şeye çok daha yakın olmalıdır.

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 

void splitSent (string sent, vector<string>& words); 

int main() { 
    string sent; 
    vector<string> words; 

    cout << "Enter your sentence: " << endl; 
    getline(cin, sent); 
    splitSent(sent, words); 

    string finalSent; 
    for (unsigned int i = 0; i<words.size(); i++) { 
    if (words[i] == "i") { 
     finalSent += "I"; 
    } else if (words[i] == "instructor") { 
     finalSent += "name of prof"; 
    } else { 
     finalSent += words[i]; 
    } 
    if (i<words.size()-1) { // Don't put a space before the period 
     finalSent += " "; 
    } 
    } 
    finalSent += "."; 

    cout << "Final sentence is: " << finalSent << endl; 

    return 0; 
} 

void splitSent (string sent, vector<string>& words) { 
    int Pos = 0; // Position 
    string word; 

    while (Pos < sent.length()) { 
    while ((Pos < sent.length()) && (sent[Pos] != ' ')) { 
     word += sent[Pos]; 
     Pos++; 
     if (sent[Pos] == '.') { 
     break; 
     } 
    } 
    words.push_back(word); 
    word = ""; 
    Pos++; 
    } 
}