用scanf扫描未知的行数,直到EOF [关闭]

问题描述 投票:-1回答:1

我正在尝试编写一个扫描未知数字的函数最多10行到一个数组中,最后返回数字的行数。这是代码本身-

for (int i = 0; i < n; i++) {
    for (int j = 0; j < DEST; j++)
    {
        if (scanf("%lf", &flightsPrices[i][j]) != EOF) {
            if (flightsPrices[i][j] <= 0) {
                errorPrice(i);
                exit(-i);
            }
        }
        else return i; 
    }
}
return 1;

问题是我似乎只获得返回值1和0。我不知道为什么,不胜感激一些指导。谢谢大家!

c arrays scanf
1个回答
0
投票

我将更改条件和返回的值,类似这样

int ask_prices(size_t n, size_t DEST, double flightsPrices[n][DEST])
{
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < DEST; j++)
        {
            if ( scanf("%lf", &flightsPrices[i][j]) == 1)
            { //                                    ^^^^ If a double is read 
                if (flightsPrices[i][j] <= 0)
                {
                    errorPrice(i);  
                    return i;      // <-- This price is wrong, but not the others.
                }
            }
            else
                return i; 
        }
    }
    return n;  // All the prices were correctly read
}
© www.soinside.com 2019 - 2024. All rights reserved.