所以我有這樣的scanf掃描字符串時,如果用戶輸入超過30個字符,該如何禁止輸入?
scanf ("%30[^\n]%c", &i
我如何添加其中,如果用戶輸入超過30個字符,它會提示一個消息,說有太多的人物,並讓他們重新進入的條件?
這裏是我的整個循環不斷重新進入,直到輸入正確的,但我沒有合適的條件設定
int keeptrying = 1, rc;
char after, i;
do
{
rc = scanf ("%30[^\n]%c", &i, &after);
if (rc == 0)
{
printf (" **Invalid input try again: ");
clear();
}
else if (after != '\n')
{
printf (" **Trailing characters try again: ");
clear();
}
else if() // THIS IS WHERE I WOULD PUT THE CONDITION
{
printf (" **Title is longer than 30 characters: ");
}
else
{
keeptrying = 0;
}
} while (keeptrying == 1);
return i;
['strlen的()'](http://pubs.opengroup.org/onlinepubs/009695399/functions/strlen.html) – 2012-12-02 18:50:13
存儲最多30個字符加結尾空成'i'是*糟糕的主意™*。你需要'我'是至少31個字符的數組。 –