C ++比较2个阵列问题?

时间:2016-02-19 22:11:56

标签: c++ arrays

我正在从文本文件中读取字符串列表并将其输入到数组中。然后我要求用户输入另一个字符串列表放入一个新数组,我想比较两个字符串并输出相同的字符串(所以2个数组的交集)我得到一个未处理的异常0x0F6508AB,它在底部" _Count |中说价值4 |输入unsigned int" " _First2"知道我的问题是什么吗?注意:一旦我有了良好的工作,忽略那些应该是另一种比较的bad4dogs

const int ARRAY_SIZE = 28;
const int ARRAY_SIZE2 = 20;
const int sixFoods = 6;
int i = 0;
int j = 0;
int quit = 1;
string good4dogs[ARRAY_SIZE];
string bad4dogs[ARRAY_SIZE2];
string userFoodNames[sixFoods];
int count = 0;
int count2 = 0;

ifstream inputFile;
ifstream inputFile2;


inputFile.open("GoodForDogs.txt");
inputFile2.open("PoisonForDogs.txt");

if (inputFile)
{
    cout << "it worked";
}
else
{
    cout << "error";
}

while (count < ARRAY_SIZE && inputFile >> good4dogs[count])
    count++;

while (count2 < ARRAY_SIZE2 && inputFile2 >> bad4dogs[count2])
    count2++;

inputFile.close();


cout << "Please enter up to six foods." << endl << endl;

for (int k = 0; k < sixFoods; k++)
{
  cout << "Food " << (k + 1) << ": ";
  getline(cin, userFoodNames[k]);
}

cout << "Foods that YOU have that also match food GOOD for dogs.\n";
cout << "-------------------------------------------------------\n";
while (i < ARRAY_SIZE && j < sixFoods) do
{
    if (good4dogs[i] == userFoodNames[j])
    {
        cout << good4dogs[i] << endl;
        i++;
        j++;
    }
    else if (good4dogs[i] > userFoodNames[j])
        j++;
    else
        i++;
} while ( quit == 1); 

3 个答案:

答案 0 :(得分:1)

更好地编写问题代码,你得到

while (i < ARRAY_SIZE && j < sixFoods) 
{
    do
    {
        if (good4dogs[i] == userFoodNames[j])
        {
            cout << good4dogs[i] << endl;
            i++;
            j++;
        }
        else if (good4dogs[i] > userFoodNames[j])
            j++;
        else
            i++;
    } while ( quit == 1); 
}

这表明这里有两个循环。

外部循环将防止ij超越其各自的缓冲区,但在内部循环完成之前不会进行任何边界测试。

内部循环永远不会完成,因为quit永远不会被设置为除了1之外的任何东西。这会导致无限循环而无法阻止ij超出界限。

答案 1 :(得分:0)

你的最后一个循环是一段时间做{}同时:-)它可能是你问题的根源,因为它只会在你退出时停止!= 1。

你遇到的另一个问题是你的数组没有排序,这会使你的最后一个循环失败。无论如何,在C ++中,要计算交集,使用set_intersection算法可能是更好的解决方案:http://www.cplusplus.com/reference/algorithm/set_intersection/

在全球范围内,您更多地编写C而不是C ++代码。您应该更喜欢使用std :: vector而不是C风格的数组。您还应该更喜欢使用算法而不是循环。

类似的东西:

vector<string> good4dogs;
ifstream inputFile("Good4Dogs.txt");
copy(istream_iterator<string>(inputFile), {}, back_inserter(good4dogs));

允许您在向量中读取文件的内容,而不会出现由向量上的硬编码限制组成的问题,需要处理较小的文件等...

请注意,您正在检查和关闭inputFile的有效性,而不是inputFile2

答案 2 :(得分:0)

inputFile.open("GoodForDogs.txt");
inputFile2.open("PoisonForDogs.txt");

if (inputFile)
{
    cout << "it worked";
}
else
{
    cout << "error";
}

首先,您没有检查第二个文件,如果出现“错误”,您的程序将只读取文件,就像没有“错误”一样。除了“它工作”的信息是如何矛盾的,因为它尚未读取文件。

要解决此问题,假设您在main内工作,则将其替换为此类内容。

if (!inputFile || !inputFile2)
{
    cout << "error: could not open files.";
    return 0;
}
while (count < ARRAY_SIZE && inputFile >> good4dogs[count])
    count++;

while (count2 < ARRAY_SIZE2 && inputFile2 >> bad4dogs[count2])
    count2++;

inputFile.close();

在这里,您需要为第二个文件添加close

while (i < ARRAY_SIZE && j < sixFoods) do
{
    if (good4dogs[i] == userFoodNames[j])
    {
        cout << good4dogs[i] << endl;
        i++;
        j++;
    }
    else if (good4dogs[i] > userFoodNames[j])
        j++;
    else
        i++;
}
while (quit == 1);

这里while (quit == 1)是一个无限循环,因为quit被初始化为1并且在循环内没有更新。