【问题标题】:How to match data in the same file using C++如何使用 C++ 匹配同一文件中的数据
【发布时间】:2022-10-17 15:28:22
【问题描述】:

该程序以列格式打印报告,该列的格式与示例输出中显示的完全相同。

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    char user_gender, user_smoker;
    string user_eyecolor;
    int user_minAge, user_maxAge, user_minHeight, user_maxHeight;

    cout << "What is the gender of your ideal match(M, F, N) ? ";
    cin >> user_gender;

    cout << "What is the minimum age? ";
    cin >> user_minAge;

    cout << "What is the maximum age? ";
    cin >> user_maxAge;

    cout << "What is the minimum height (in inches)? ";
    cin >> user_minHeight;

    cout << "What is the maximum height (in inches)? ";
    cin >> user_maxHeight;

    cout << "Smoker (Y/N)? ";
    cin >> user_smoker;

    cout << "What is the eyecolor (Blue, Green, Grey, Brown)? ";
    cin >> user_eyecolor;

    cout << endl << endl;
    //Variables to check against the conditions
    int countGender = 0;
    int partialMatch = 0;
    int fullMatch = 0;

    cout << endl << left << setw(1) << "  Name" << fixed << right << setw(22) << "Age" << fixed << right << setw(12) << "Height" << fixed << right << setw(12) << "Smoker" << fixed << right << setw(15) << "Eye Color" << fixed << right << setw(22) << "Phone" << endl;
    cout << "-----------------------------------------------------------------------------------------------------------------" << endl;


    //Now read the file data.
    ifstream fin("matches.txt");

    if (fin.is_open())
    {
        while (!fin.eof())
        {
            string firstname, lastname, eyecolor, phoneno;
            char gender, smoker;
            int age, height;
            fin >> firstname >> lastname >> gender >> age >> height >> smoker >> eyecolor >> phoneno;

            if (gender == user_gender)
            {
                countGender++;

                //Now check to see if the age and height are between the maximum and minum preferences. 
                if ((age >= user_minAge && age <= user_maxAge) && (height >= user_minHeight && height <= user_maxHeight))
                {

                    //Then check to see if the smoking preference and eye color are also a match. 
                    if (user_smoker == smoker && user_eyecolor == eyecolor)
                    {
                        fullMatch++;

                        cout << "* " << firstname << "  " << lastname  << setw(25) << age << setw(11) << height << setw(11) << smoker << setw(11) << eyecolor << setw(11) << phoneno << endl;
                    }

                    else if (eyecolor == user_eyecolor)
                    {
                        partialMatch++;

                        cout << "  " << firstname << "  " << lastname << setw(24) << age << setw(11) << height << setw(11) << smoker << setw(11) << eyecolor<< setw(11) << phoneno << endl;
                    }
                }
            }
        }
        cout << "-----------------------------------------------------------------------------" << endl;
        cout << "There were " << fullMatch << " matches and " << partialMatch << " partial matches out of " << countGender << " records." << endl;
        cout << "-----------------------------------------------------------------------------" << endl;

        fin.close();
    }
    else {
        cout << "File did not open";
    }


    return 0;
}

****该程序运行得非常好,但我没有将输出打印在上面示例输出中所示格式的列中。 **** 编写一个程序,打开文件并逐条读取记录。该程序将跳过性别偏好不匹配的任何记录。在符合性别偏好的那些记录中,检查年龄和身高是否在最大和最小偏好之间。然后检查吸烟偏好和眼睛颜色是否也匹配。如果至少 3 个剩余字段匹配,则认为记录部分匹配,并将其打印在报告中。如果所有 4 个剩余字段都匹配,则该记录是完全匹配的,并将其打印在报告中,并在其旁边带有一个星号。在程序结束时,关闭文件并报告指定性别的总记录数,部分匹配的记录数,完美匹配的记录数。

Charlie Bradbury    F   42  65  N   Green   555-867-5309
Bobby Singer        M   70  69  Y   Brown   555-867-5309
Dean Winchester     M   43  72  N   Brown   555-867-5309
Sam Winchester      M   39  75  N   Brown   555-867-5309
Bela Talbot         F   39  69  Y   Blue    555-867-5309
James Novak         M   46  71  Y   Blue    555-867-5309

【问题讨论】:

  • 发布您的输入文件的几行(作为文本,上面一行带有```none,下面一行带有```,以格式化为固定文本)查看您的输入将使这里的每个人都可以确定您的代码是否正在执行您的操作希望是。确保数据文件不是多字节字符编码(例如,带有 BOM 的 UTF-16 是 Windows 记事本的默认值 - 会导致问题)

标签: c++


【解决方案1】:
  1. 使用具有调试功能的适当 IDE(例如Visual Studio Community Edition)。

  2. 在有问题的方法的第一行设置断点,例如在你的情况下第 9 行

  3. 逐行遍历您的代码,看看它做了什么。

  4. 将鼠标悬停在变量上或使用局部变量或观察窗口查看变量的值

  5. 想想你是什么预计会看到并将其与您的内容进行比较实际看到.在 99% 的情况下,计算机比你更正确,这是对你的误解。

【讨论】:

  • 谢谢托马斯的回答。我会修好它。
  • 我做了一些更改,但仍然无法正常工作。
  • @JEANELIENYANGUILE:随时发布一个新问题。也许将您的代码减少到minimal reproducible example,即创建您的项目的副本并删除尽可能多的代码,但问题仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
  • 2014-05-25
  • 2015-03-23
相关资源
最近更新 更多