我对编程有点新,所以我的问题的答案并没有变得明显,尽管我试图完成任务有很多方法。我继续从另一个数组中修改数组中的垃圾值,在函数中
问题是,我试图采取一个单词的数组,从数组中删除任何标点符号,并将新单词放入一个单独的数组。我试图做到这一点,但当我输出新数组时,我不断收到垃圾数值。
代码读取:
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