如何使用文件填充数组并将其与用户输入 c++ 进行比较

Posted

技术标签:

【中文标题】如何使用文件填充数组并将其与用户输入 c++ 进行比较【英文标题】:how do I use a file to populate the array and compare it with user input c++ 【发布时间】: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。特别是,消除示例代码中的输入。 【参考方案1】:

当你使用

inputFile >> contacts[count]
    前导空白字符被丢弃。 非空白字符被读入contants[count]。 遇到空白字符时停止读取。

这解释了你的输出。

您需要改用istream::get

while (count < arraySize && inputFile.get(contacts[count], length) )
    count++;

回应OP的评论

以上应该是文件的所有行最多arraySize 行数。

您可以添加一些调试输出来解决问题。

while (count < arraySize && inputFile.get(contacts[count], length) )

    std::cout << "Read " << count+1 << "-th line.\n" << "\t" << contants[count] << "\n";
    count++;

【讨论】:

解决了这个问题!!现在如果我输入名字,它会显示全名和号码,但是,如果我输入姓氏,有没有办法显示联系人?例如,如果联系人是:“Holly Gaddis,555-8878”“Warren Gaddis,555-9037”我可以输入姓氏 Gaddis 并显示两个联系人吗? @fluy619,当然可以。它应该适用于您发布的代码。 我尝试输入 Gaddis 并显示涉及姓名的两个联系人,但输出为“抱歉,未找到匹配项!” 看起来数组只存储第一个名字,这意味着如果用户将他们的名字放在输入中,其他名字将不会显示 @fluy619,既然您接受了答案,那么假设您的问题/疑问已得到解答是否公平?

以上是关于如何使用文件填充数组并将其与用户输入 c++ 进行比较的主要内容,如果未能解决你的问题,请参考以下文章

如何编码照片并将其与网络摄像头输入进行比较?

从文件中删除单词每次用户输入要删除的单词并将新数组写入新文件。 C++

如何使用标头中的用户 JWT 令牌转发授权并将其与 cookie 中的 JWT 令牌进行比较以验证用户?

如何在数组中搜索先前已输入的 int 值

如何生成随机数并将其与令牌输入进行比较?

phpmyadmin 与 java。有没有办法从 phpmyadmin 获取字符串并将其与键盘输入进行比较?