我對編程有點新,所以我的問題的答案並沒有變得明顯,儘管我試圖完成任務有很多方法。我繼續從另一個數組中修改數組中的垃圾值,在函數中
問題是,我試圖採取一個單詞的數組,從數組中刪除任何標點符號,並將新單詞放入一個單獨的數組。我試圖做到這一點,但當我輸出新數組時,我不斷收到垃圾數值。
代碼讀取:
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;
}
你如何聲明你的原始數組,你傳入'norm'?是否有任何理由你正在使用數組而不是'std :: vector',因爲我認爲這個字符串列表可能是可變的? – birryree 2011-04-09 13:29:56
我認爲你想爲外循環的每一次迭代使用newstring =「」。 – jfs 2011-04-09 13:33:15
我沒有使用矢量,僅僅是因爲我還沒有知道如何實現它:P – Sam 2011-04-09 13:42:36