簡體   English   中英

“scanf” 作為 while 循環中的條件

[英]“scanf” as a condition in while loops

我需要使用“scanf”function 來確定輸入的大小(可能是無限的)。 現在我有限制,所以我不能使用字符串,arrays,或者基本上不是 function 的任何東西。

char input;
int counter = 0;
while (scanf(" %c",&input) == 1) counter++;

我面臨的問題是循環是無限的,我認為我的結束條件可能是錯誤的。 我已經嘗試了所有這些。

while (scanf(" %c",&input) > 0)
while (scanf(" %c",&input) != -1)
while (scanf(" %c",&input) != EOF)

我也嘗試過“do-while”。

感謝幫助者。

//Assuming that input will be space seperated and will terminate when user presses "Enter".
#include<stdio.h>
int main()
{
        char input;
        while(1)
        {
                if(scanf("%c", &input))
               {
                        /*
                        * do someething
                         */
                        if(input=='\n')
                    {
                            break;
                    }
                    else
                    {
                            printf("%c\n", input);
                    }
            }

    }
    return 0;
}

我面臨着類似的問題。 當我使用 "while(scanf("%s", dictionary[i++]));" 時,循環無法結束。即使我使用 Ctrl + D。當我使用 Debug 時,我發現循環確實在運行,但是循環不能結束。 謝謝你的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM