2011-10-13 115 views
0

平臺若干發呆:Linux的2.6.18 x86_64的約帕斯卡輸出

編譯器:Free Pascal編譯版本2.2.2 [2008年11月5日]爲x86_64的

的源代碼是:

Program main; 
var invalue:string; 
Begin 
     (*Until the EOF, this loop continue to work*) 
     while not eof do 
     begin 
       Write('Please input the express: '); 
       Flush(StdOut); 
       Readln(invalue); 
       Writeln('The expression is: ',invalue); 
     end; 
     Writeln(''); 
     Writeln('Exit the program. Bye!'); 
End. 

我編譯並運行它。但輸出是這樣的:

123 
Please input the express: The expression is: 123 
234 
Please input the express: The expression is: 234 
345 
Please input the express: The expression is: 345 

Exit the program. Bye! 

這些數字是我的輸入。我搜索了它,並認爲這是因爲緩衝區。所以我試圖在寫入之後添加flush(stdout)或flush(輸出)。但它仍然不能很好地工作。

我希望它的工作原理爲:

Please input the express: 123 
The expression is: 123 
Please input the express: 234 
The expression is: 234 
Please input the express: 345 
The expression is: 345 

Exit the program. Bye! 

謝謝!很遺憾我的傻瓜〜


增加: 我試圖爲(謝謝你,Aloush!)

Program main; 
var invalue:string; 
Begin 
     (*Until the EOF, this loop continue to work*) 
     while not eof do 
     begin 
       Write('Please input the express: '); 
       Flush(StdOut); 
       Readln(invalue); 
       Writeln('') 
       Writeln('The expression is: ',invalue); 
     end; 
     Writeln(''); 
     Writeln('Exit the program. Bye!'); 
End. 

結果還是沒有得到很好的爲:

1 
Please input the express: 
The expression is: 1 
2 
Please input the express: 
The expression is: 2 
3 
Please input the express: 
The expression is: 3 
4 
Please input the express: 
The expression is: 4 
5 
Please input the express: 
The expression is: 5 

Exit the program. Bye! 

的Readln在第一次寫入之前仍然執行。

順便說一句,我也嘗試:

Program main; 
var invalue:string; 
Begin 
     (*Until the EOF, this loop continue to work 
     while not eof do 
     begin*) 
     Repeat 
       Write('Please input the express: '); 
       Flush(StdOut); 
       Readln(invalue); 
       Writeln('The expression is: ',invalue); 
     Until eof; 
     Writeln(''); 
     Writeln('Exit the program. Bye!'); 
End. 

在這種情況下,第一個循環是好的,但其他人仍然是錯誤的。

Please input the express: 123 
The expression is: 123 
234 
Please input the express: The expression is: 234 
345 
Please input the express: The expression is: 345 

Exit the program. Bye! 

謝謝!


最終解決方案: http://www.amath.unc.edu/sysadmin/DOC4.0/pascal/lang_ref/ref_io.doc.html#592 這是因爲,在EOF實際上相當於一個隱含的讀取。 當前的代碼應該是:

Program main; 
var invalue:string; 
Begin 
     (*Until the EOF, this loop continue to work*) 
     Write('Please input the express: '); 
     while not Eof do 
     begin 
       Readln(invalue); 
       Writeln('The expression is: ',invalue); 
       Write('Please input the express: '); 
     end; 
     Writeln(''); 
     Writeln('Exit the program. Bye!'); 
End. 

最後,在這裏謝謝你,Aloush和keiy上CSDN!

+0

請在答案左側的箭頭滑動以將其標記爲「已接受」。這比編輯標題「解決」要好。 – spraff

回答

0

你可以試試以下嗎?

Program main; 
var invalue:string; 
Begin 
     (*Until the EOF, this loop continue to work*) 
     while not eof do 
     begin 
       Write('Please input the express: '); 
       Flush(StdOut); 
       Readln(invalue); 
       Writeln('') 
       Writeln('The expression is: ',invalue); 
     end; 
     Writeln(''); 
     Writeln('Exit the program. Bye!'); 
End.