我继续在函数中从另一个数组修改的数组中获取垃圾值

I continue to get garbage values in an array that have been modified from another array, in a function

本文关键字:数组 获取 修改 另一个 继续 函数      更新时间:2023-10-16

我对编程有点陌生,所以我的问题的答案还不是很明显,尽管我已经尝试了很多方法来完成任务。

问题是我试图取一个单词数组,从数组中删除任何标点符号,并将新单词放入单独的数组中。我尝试这样做,但当我输出新数组时,我一直得到垃圾值。

代码如下:

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;
        }
 
}

完整的main函数是:

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;
}

你的代码应该可以工作,但是可能在main函数中有一个问题,比如你的数组和你必须使用两个数组的事实,所以我看到这种行为的一个原因可能是你的数组大小不匹配,存储原始字符串的那个比你复制到的那个大。

#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的连接问题,但后来没有清除它。

你可以更简洁地解决这个问题,通过使用标准<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';
    }
}

不确定垃圾输出是什么意思?

如果我用这个(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

这不是你想要的吗?

如果不需要连接,则需要重置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

数组是固定大小的数组。如果你需要从"数组"中添加和删除元素,你应该使用列表或向量,它们是可变大小的序列。