C++ C++;比较两个阵列的问题?

C++ C++;比较两个阵列的问题?,c++,arrays,C++,Arrays,我正在从文本文件中读取字符串列表,并将其输入到数组中。然后,我要求用户将另一个字符串列表放入一个新数组中,我想比较这两个字符串并输出相同的字符串(因此是两个数组的交集)。我在0x0F6508AB处得到一个未处理的异常,它在底部写着“_Count | Value 4 |键入unsigned int”“_First2”知道我的问题是什么吗?注:忽略bad4dogs,这应该是另一个比较,一旦我有了好的工作 const int ARRAY_SIZE = 28; const int ARRAY_SIZE2

我正在从文本文件中读取字符串列表,并将其输入到数组中。然后,我要求用户将另一个字符串列表放入一个新数组中,我想比较这两个字符串并输出相同的字符串(因此是两个数组的交集)。我在0x0F6508AB处得到一个未处理的异常,它在底部写着“_Count | Value 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); 
const int数组_SIZE=28;
常数int数组_SIZE2=20;
常数=6;
int i=0;
int j=0;
int-quit=1;
字符串good4dogs[数组大小];
字符串bad4dogs[ARRAY_SIZE2];
字符串userFoodNames[sixFoods];
整数计数=0;
int count2=0;
ifstream输入文件;
ifstream输入文件2;
open(“GoodForDogs.txt”);
inputFile2.open(“toxinfordogs.txt”);
如果(输入文件)
{
cout goods[计数])
计数++;
而(count2>bad4dogs[count2])
count2++;
inputFile.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(icout您的最后一个循环是while do{}while:-)它可能是问题的根源,因为只有当quit!=1时,它才会停止

另一个问题是数组不排序,这会使最后一个循环失败。无论如何,在C++中,使用SETIONIFON算法可能是更好的解决方案:

全局,您比C++代码更能编写C。您应该更喜欢使用STD::vector,而不是C样式数组。您也应该更喜欢使用算法而不是循环。 比如:

vector<string> good4dogs;
ifstream inputFile("Good4Dogs.txt");
copy(istream_iterator<string>(inputFile), {}, back_inserter(good4dogs));
vectorGood4Dogs;
ifstream输入文件(“Good4Dogs.txt”);
复制(istream_迭代器(inputFile),{},back_插入器(good4dogs));
允许您读取向量中的文件内容,而不存在向量硬编码限制、需要处理较小文件等问题

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

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

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


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

while(quit==1);是一个有趣的条件。你确定你真的把
ARRAY_SIZE
行读入
good4dogs
中了吗?如果这很早就停止了,你会得到有趣的结果。在第一次while循环之后打印
count
。学会爱上调试器。@SergeyA我不明白为什么在这个时代,课程不从显示调试器,从简单的“Hello World”开始。还有什么比实际看到变量改变值、循环如何流动等更能成为学习工具的呢?这对我来说毫无意义。无可否认,这是很久以前的事了,但说服我从计算机科学转为工程学的教授告诉全班同学,“如果你写的代码是正确的,就不会有bug。”在回答“调试怎么样?”
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();
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);