【问题标题】:When I use ctime my loop runs half the amount of times as it should当我使用 ctime 时,我的循环运行次数是它应该运行的一半
【发布时间】:2021-10-07 04:33:52
【问题描述】:

大小定义为 15,但这个程序由于某种原因只运行了 8 次,我不知道为什么。 这部分是唯一的问题。一旦我删除它并用不使用 ctime 的东西替换它,它就会运行 15 次。

    for(int count = 0; count < size; count++) 
    {
        printf("Plane ID :         %d\n", planes[count].planeid);
        printf("Destination :      %s\n", planes[count].destination); 
        char * time_str;
        time_str = ctime(&planes[count].time);
        printf("Depart Time/Date : %s \n", time_str);
        count++; 
    }

【问题讨论】:

  • 结构数组中.time 成员的值是多少?如果其中一个无效或年份值 >= 10000,ctime 函数可能会失败。
  • @AdrianMole 我把它作为一个最大为 9999 的随机值。我刚刚用初始化时间的函数更新了代码。
  • 什么是planes?如何定义?请发布一个minimal reproducible example - 一些#includes,一个简短的main(),一个完整的程序。

标签: c time.h ctime


【解决方案1】:

你每次循环增加两次计数:

for (int count = 0; count < size; count++) 
 //                               ^^^^^^^ HERE
{
    ..
    count++;   // HERE
}

删除函数体末尾的第二个count++;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2021-06-04
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多