简体   繁体   English

为什么EOF在我的scanf while循环期间没有终止?

[英]Why does EOF not terminate during my scanf while loop?

I just started learning C++ and am trying to learn how to use scanf and printf for faster input and output. 我刚刚开始学习C ++,并且正在尝试学习如何使用scanf和printf以获得更快的输入和输出。 Here is the code I'm currently working on: 这是我目前正在处理的代码:

#include <stdio.h>
using namespace std;

int main() {
    int time, record;
    double down, loan;
    while (scanf("%d %lf %lf %d", &time, &down, &loan, &record) != EOF) {
        double value = down + loan;
        double owed = loan;
        double payment = owed/time;

        // current simulated month and depreciation
        int rday, c = 0;
        double temp, dep;
        bool found = false;

        // finds value and owed after records
        while (!found && record > 0) {
            scanf("%d %lf", &rday, &temp);
            // adjusts value and owed to day before listed on record
            while (!found && c <= rday) {
                if (c == rday) {
                    dep = temp;
                }
                value *= 1 - dep;
                if (c > 0) {
                    owed -= payment;
                }
                c++;

                // determines if found date
                if  (owed < value) {
                    found = true;
                }
            }
            record--;
        }

        // finds point where owed < value
        while (!found && value < owed) {
            value *= 1 - dep;
            owed -= payment;
            c++;
        }

        if (c - 1 == 1) {
            printf("%d month\n", c - 1);
        }
        else {
            printf("%d months\n", c - 1);
        }
    }
    return 0;
}

When I run this on Code::Blocks, it prints the correct answers, but the outermost while loop doesn't terminate even when I enter CTRL+Z (I am using Windows). 当我在Code :: Blocks上运行此命令时,它会打印正确的答案,但是即使输入CTRL+Z (我使用Windows),最外面的while循环也不会终止。 Here is my input: 这是我的输入:

30 500.0 15000.0 3
0 .10
1 .03
3 .002
12 500.0 9999.99 2
0 .05
2 .1
60 2400.0 30000.0 3
0 .2
1 .05
12 .025
-99 0 17000 1

Here is an image of the what happens: 这是发生的情况的图像:

Error 错误

I've tried changing the loop condition to scanf("%d %lf %lf %d", &time, &down, &loan, &record) == 4 , but the same problem happens. 我尝试将循环条件更改为scanf("%d %lf %lf %d", &time, &down, &loan, &record) == 4 ,但是发生相同的问题。 Could someone please explain what the issue with my code is? 有人可以解释一下我的代码的问题是什么吗?

In the line 在行中

while (scanf("%d %lf %lf %d", &time, &down, &loan, &record) != EOF)

you expect 4 variables to be read into when the read is successful. 您希望读取成功时将读取4个变量。 When scanf is able to successfully extract the data from stdin for all the arguments, it will return 4. Change the check to use that number. scanf能够成功地从stdin提取所有参数的数据时,它将返回4。将检查更改为使用该数字。

while (scanf("%d %lf %lf %d", &time, &down, &loan, &record) == 4)

这是因为scanf永远不会返回EOF ,所以永远不会满足终止条件。

Thanks for the suggestions and answers, everyone! 谢谢大家的建议和答案! I figured out the bug. 我发现了这个错误。 Kind of embarrassing, but it turns out the issue was with the last line of input. 有点尴尬,但事实证明问题出在最后一行输入上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM