Scanf跳过一条线

时间:2017-02-12 00:09:41

标签: c arrays loops printf scanf

我正在编写一个代码,提示用户输入他们选择的天数(3-10之间)的高点和低点。到目前为止,一切都是正确的,除了scanf函数进入高温和低温出现比它们应该低一行。例如,将输出第1天高和低,但用户的输入从第1天低开始,因此高输入接近低,低输入接下来是空行。

我认为问题可能是printf正在完成它在scanf生效之前输出高和低两者的功能。有没有办法让scanf出现,以便可以将高值和低值相互输入?这是我的代码:

#include <stdio.h>

int main (void)
{
    int i;
    int limit;
    int day[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int high[10],low[10];

    printf("---===IPC Temperature Analyzer V2.0===---\n");

    printf("Please enter the number of days between 3 and 10, inclusive: ");
    scanf("%d", &limit);
    while (limit <= 2 || limit >= 11) {
            printf("Invalid");
            scanf("%d", &limit);
    }

    for(i = 0;i < limit; i++) {
            printf("Day %d - High:\n Day %d - Low: ",day[i],day[i]);
            scanf("%d%d", &high[i], &low[i]);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:2)

你在找这样的东西吗?

url,    title,  visit_count,    typed_count,    datetime((urls.last_visit_time/1000000)-11644473600, 'unixepoch', 'local time'),    hidden, favicon_id, datetime((visits.visit_time/1000000)-11644473600, 'unixepoch', 'local time'),   from_visit, visit_duration, transition, source
https://www.sqlite.org/cli.html,    Command Line Shell For SQLite,  2,  0,  ,   0,  0,  ,   136309, 0,  1610612736  ,

这导致:

for(i = 0;i < limit; i++) {
    printf("Day %d - High: ", day[i]);
    scanf("%d", &high[i]);
    printf("Day %d - Low: ", day[i]);
    scanf("%d", &low[i]);
}