c++ - 查找文件中的重复数字 (C++)

标签 c++

我环顾四周,但我仍然坚持一些逻辑。

我有一个包含生日列表的文件。我想找到最常见的生日(月/日)。

到目前为止我的逻辑:

    int maxValue = 0;

    int maxBday;

    while (Bday >> month >> b >> day >> b >> year) // take in values a line at a time from the file

    {
        int a = totalDays(month, day); //function finds number of days into the year 
        //(i.e. Feb. 2 = 33, not considering leap years in this problem). 

        for (int i = 0; i < 365; i++)
        {
            if (i = a) //going through all 365 days until the number of days matches
            {
                bdays[i]++; //add one to the array at position i

                if (bdays[i] > maxValue) //finding what the maximum value in the array is
                {
                    maxBday = i; //max number of days
                    maxValue = bdays[i]; //Setting new max for next line
                }
            }
        }
     }
     cout << "The most common birthday: " << maxBday; 
}

稍后我将创建一个函数,将总天数转换为年份作为稍后的实际日期。

我的文件在 1 月 1 日有一个重复的日期,因此输出应该是 1 表示一年中的 1 天,但我没有得到任何输出。我已经输入了 cout 语句,函数的每个部分都被访问了,但循环永远不会结束。我真的不知道我的逻辑错误可能在哪里。

最佳答案

尝试

if(i == a)

否则程序会将 i 设置为 a 的值。它可能不是完整的解决方案。

关于c++ - 查找文件中的重复数字 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42917557/

相关文章:

C++:在两个程序中使用宽字符串时无法通过管道获取数据

c++ - 类型错误引用的无效初始化

c++ - 在 std::move() 之后 unique_ptr 会发生什么?

C++ 自定义 vector 实现 - 指向不完整类型的指针的下标

c++ - 是否有可能检测到线程在 Linux [暂停] 中进行了上下文切换?

c++ - 二维 vector 的运行时错误

c++ - 背景音乐和声音

c# - 将 C++ 文件读取/写入转换为 C# 文件读取/写入

C++ 编译时宏来检测 Windows 操作系统

c++ - 打印字节的正确类型转换