【发布时间】:2018-04-25 19:28:05
【问题描述】:
我写了一个代码来从一个文件中填充一个数组 然后使用该数组将其与用户输入进行比较 程序应要求用户输入名称或部分名称以在 大批 这是代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
bool found = false;
const int arraySize = 35;
const int length = 100;
char contacts[arraySize][length];
int count = 0; // Loop counter variable
ifstream inputFile; // Input file stream object
inputFile.open("input.txt"); // Open the file.
// Read the numbers from the file into the array.
// After this loop executes, the count variable will hold
// the number of values that were stored in the array.
while (count < arraySize && inputFile >> contacts[count])
count++;
// Close the file.
inputFile.close();
char search[length];
char *fileContact = nullptr;
int index;
cout << "To search for your contact's number \nplease enter a name or partial name of the person.\n";
cin.getline(search, length);
for (index = 0; index < arraySize; index++)
{
fileContact = strstr(contacts[index], search);
if (fileContact != nullptr)
{
cout << contacts[index] << endl;
found = true;
}
}
if (!found) cout << "Sorry, No matches were found!";
return 0;
}
文件中的名字是
“亚历杭德拉克鲁兹,555-1223”
“乔鲁尼,555-0097”
“杰里·帕尔默,555-8787”
“李晨,555-1212”
“霍莉·加迪斯,555-8878”
“山姆·威金斯,555-0998”
“鲍勃·凯恩,555-8712”
“蒂姆·海恩斯,555-7676”
“沃伦·加迪斯,555-9037”
“让·詹姆斯,555-4939”
“罗恩·帕尔默,555-2783”
所以代码可以工作,但问题是 例如,当我写亚历杭德拉时 输出是:“亚历杭德拉 输出应该显示全名和数字: “亚历杭德拉克鲁兹,555-1223”
有谁知道如何解决这个问题? 谢谢!!
【问题讨论】:
-
你有没有做任何事情来看看你的阵列中实际放入了什么?为什么不呢?
-
请提取minimal reproducible example。特别是,消除示例代码中的输入。