2011-04-09 18 views
1

我對編程有點新,所以我的問題的答案並沒有變得明顯,儘管我試圖完成任務有很多方法。我繼續從另一個數組中修改數組中的垃圾值,在函數中

問題是,我試圖採取一個單詞的數組,從數組中刪除任何標點符號,並將新單詞放入一個單獨的數組。我試圖做到這一點,但當我輸出新數組時,我不斷收到垃圾數值。

代碼讀取:

norm(sepwords1,sepwords2,numwords);  <- where I called it in main 

void norm(string words[], string wordz[],int count)  
{ 

     int i; 
     int x; 

     string newstring=""; 
     char current; 


    for(i=0; i<count; i++) 
     { 
     for(x=0; x<words[i].length();x++) 
     {   
      current= words[i].at(x); 
       if(ispunct(current)==0) 
       { 
       newstring += current; 
       } 

     }   
       wordz[i]= newstring; 
     } 

} 

完整的主要功能是:

int main (int argc, char* argv[]) 
{ 



int count = argc; 
int i; 
string filename[count]; 
ifstream infile; 
string fromfile[1000]; 
int numdata; 
int pass; 
char current; 
int sum; 
string masterstring=""; 
int x; 
string sepwords[2000]; 
int sum1; 
string temp=""; 
int start; 
int fin; 
string newstring=""; 
string newfile[1000]; 
int place; 
int numwords; 
string sepwords1[2000]; 
string newmaster=""; 
int j=0; 
string currentz; 
string highmark; 
int index[2000]; 
string sepwords2[2000]; 
int counta=0; 

for(i=0; i < count-1; i++) 
{ 
filename[i] = argv[i+1]; 
} 

for(i=0; i < count-1; i++) 
    { 
    infile.open(filename[i].c_str()); 

    numdata=0; 

    while(!infile.eof()) 
    { 

    getline(infile, fromfile[numdata], '\n'); 
    numdata++; 

    } 





    for(i=0; i<numdata; i++) 
    { 
    cout<<fromfile[i]<<endl; 
    masterstring += fromfile[i] + " ";          //NUMBER ONE 
    } 



    numwords = split(masterstring, sepwords); 
    cout<<numwords<<endl;              //NUMBER TWO 


    } 

    for(i=0;i<numwords;i++) 
    { 
     newstring = toupper(sepwords[i].at(0));   
     newstring += sepwords[i].substr(1); 
     sepwords1[i] = newstring; 
     newstring=""; 
    } 

    for(i=0;i<numwords;i++) 
    { 


    newmaster += sepwords1[i] + " "; 
     j++; 
      if(j > 10) 
      { 
      newmaster+= '\n'; 
      j=0; 
      } 

    } 
    cout<<newmaster<<endl;            //NUMBER THREE 



    norm(sepwords1,sepwords2,numwords); 

     for(i=0;i<numwords;i++) 
    { 
    cout<<sepwords2<<endl; 
    } 

return 0; 
} 
+0

你如何聲明你的原始數組,你傳入'norm'?是否有任何理由你正在使用數組而不是'std :: vector',因爲我認爲這個字符串列表可能是可變的? – birryree 2011-04-09 13:29:56

+0

我認爲你想爲外循環的每一次迭代使用newstring =「」。 – jfs 2011-04-09 13:33:15

+0

我沒有使用矢量,僅僅是因爲我還沒有知道如何實現它:P – Sam 2011-04-09 13:42:36

回答

0

不確定您輸入的行李是什麼意思?

如果我打電話給你的功能與此(G ++ 4.4.5)

#include <string> 
#include <iostream> 
using namespace std; 
int 
main (int ac, char **av) 
{ 
    int numwords = 3; 
    string sepwords1[] = {"one,", "two", "three"}; 
    string sepwords2[numwords]; 
    norm(sepwords1,sepwords2,numwords); 
    for(size_t i=0;i<numwords;++i){ 
    std::cout<<"sepwords2["<<i<<"] = "<<sepwords2[i]<<std::endl; 
    } 

} 

然後我得到的輸出

sepwords2[0] = one 
sepwords2[1] = onetwo 
sepwords2[2] = onetwothree 

這是不是你想要的?

如果你不想concatination,那麼你需要重新設置newword變量,

wordz[i]= newstring; //this is in your norm function 
    newstring="";   //this is the line I added. 

然後輸出

sepwords2[0] = one 
sepwords2[1] = two 
sepwords2[2] = three 
+0

有點像這樣。我只需從每個單詞中刪除標點符號(如果有標點符號)並將其存儲在輔助數組中。使用我提供的代碼,當我從sepwords2 []中獲得cout時,我得到了每個元素的「0xffbf5828」。我錯過了什麼嗎?此外,我不明白你做了什麼,以消除逗號 – Sam 2011-04-09 13:40:06

+0

@sam因爲你有「if(ispunct(current)== 0)」檢查之前你連接到字符串 - 我沒有改變你的功能除了我指出的新增行外。 – Tom 2011-04-09 13:43:39

+0

哦,對,我知道ispunct函數做了什麼,我認爲你可能以不同的方式做了。但我確實添加了你的建議,我明白了爲什麼它是有道理的,但我始終得到值0xffbf5828。它可能是代碼中的其他地方嗎?一切工作到這一點,我宣佈他們在主要作爲:「sepwords1 [2000]」和「sepwords2 [2000]」 – Sam 2011-04-09 13:47:54

0

陣列是固定的大小的數組。如果您需要添加和刪除「數組」中的元素,則應使用列表或向量,它們是可變大小的序列。

3

您的代碼應該工作,但可能有一個問題主要功能,比如你的數組和你必須使用兩個的事實,所以我看到這種行爲的一個原因可能是你的數組大小彼此不匹配,並且存儲原始字符串的大小大於一個你正在複製到。

#include <string> 
#include <iostream> 

int main() { 
    const int SIZE = 5; 
    string oldArray[SIZE] = {"He,llo", "Wor,ld", "H,ow", "Ar,e.", "Y,O,U"}; 
    string newArray[SIZE]; 

    for (int i = 0; i < 5; ++i) { 
     // Moved this into the loop for ease, otherwise your 
     // original code would have kept appending to this 
     // newString variable unless you cleared it later 
     std::string newString = ""; 
     for (int x = 0; x < oldArray[i].length(); ++x) { 
      char current = oldArray[i].at(x); 
      if (ispunct(current) == 0) 
      { 
       newString += current; 
      } 
     } 
     newArray[i] = newString; 
    } 

    for (int i = 0; i < 5; ++i) { 
     std::cout << newArray[i] << '\n'; 
    } 
} 

這主要是你的代碼,有一些調整,以解決保持newString周圍,但沒有後來清除它的串聯問題。

您可以通過使用STD <algorithm>東西更簡潔地做到這一點的問題,並通過使用<vector>將處理增長和調整你。

#include <iostream> 
#include <string> 
#include <algorithm> 
#include <vector> 

int main() { 
    std::vector<std::string> stringsToCopy; 
    stringsToCopy.push_back("Hel,lo,"); 
    stringsToCopy.push_back("th,ere."); 

    // Make a copy of the other vector, since it seems like you want to keep 
    // the original data. This will copy all the elements from the stringsToCopy 
    // vector. 
    std::vector<std::string> newStrings = stringsToCopy; 

    // simplicity, but you could use an iterator as well, which would be 
    // more verbose 
    for (int i = 0; i < newStrings.size(); ++i) { 
     // get a reference to the current string in the 
     // vector for convenience, so we can use a shorter 
     // name for it 
     std::string& s = newStrings[i]; 

     // because remove_if doesn't actually delete things from a 
     // container, we should also call the string's erase method 
     s.erase(std::remove_if(s.begin(), s.end(), ispunct), s.end()); 
    } 


    for (int i = 0; i < newStrings.size(); ++i) { 
     std::cout << newStrings[i] << '\n'; 
    } 
} 
+0

感謝您的迴應。我會嘗試學習如何更加熟練地使用載體,因爲我現在還沒有確切的線索。我想我會加上,雖然這兩個數組被聲明在2000年的大小,所以我不知道這是否是確切的錯誤。 (或許我誤解了你說的話) – Sam 2011-04-09 13:54:18

+0

@Sam - 2000元素很好 - 你傳入'norm'的'count'是什麼?它是2000還是「單詞」包含的字符串的實際數量? – birryree 2011-04-09 13:56:10

+0

我貼滿了主要函數,count應該是原數組中元素的個數。哦,我忘了補充說,還有另一個函數用於確定數組中的單詞數量,但這一直工作到目前爲止,所以我懷疑這是問題 – Sam 2011-04-09 14:03:02

相關問題