while((getchar ()) != '\n')理解
生活随笔
收集整理的這篇文章主要介紹了
while((getchar ()) != '\n')理解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 程序清單8.5.cpp : 定義控制臺應用程序的入口點。
//帶有一個較大的I/O問題的程序
//問題出現在緩沖區
#include "stdafx.h"
void dispaly (char cr ,int lines ,int width);
int _tmain(int argc, _TCHAR* argv[])
{
int ch;
int rows ,cols;
printf ("Enter a character and two integers:\n");
while ((ch = getchar ()) != '\n')
{
scanf ("%d %d",&rows,&cols);
dispaly (ch,rows,cols);
while ((getchar ()) != '\n')
/*這里為什么不能用while (ch != '\n');因為getchar ();只讀取stdin流最前面的那個字符在賦值給ch,所以ch的值不會是回車(只要是最開始沒有輸入換行符),while ((getchar ()) != '\n')這句話是意思是getchar()每一次讀取stdin流中字符和換行符比較(當程序調用getchar時.程序就等著用戶按鍵.用戶輸入的字符被存放在鍵盤緩沖區中.直到用戶按回車為止(回車字符也放在緩沖區中).當用戶鍵入回車之后,getchar才開始從stdin流中每次讀入一個字符,這也是為什么沒有結束循環增量或減量也可讓循環結束), 如果不是換行符就跳回while語句,如果是換行符結束循環*/
continue ;
printf ("Eenter another character and two integers :\n");
printf ("Enter a newline to quit .\n");
}
printf ("Bye .\n");
getchar ();
getchar ();
return 0;
}
void dispaly (char cr, int lines, int width)
{
int row ,col;
for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++ )
putchar (cr);
putchar ('\n');
}
}
?
轉載于:https://blog.51cto.com/515223773/1213111
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的while((getchar ()) != '\n')理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为OracleLinux配置FTP yu
- 下一篇: 【NOIP模拟题】Incr(dp)