2011-04-09 54 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

相关问题