[問題] 程式出現了預期之外的輸出
問題(Question):
用cin.eof()時遇到錯誤,輸入時遇到EOF應該終止程式而不繼續運行,但程式卻多輸出一次,想知道為什麼會有錯誤?
餵入的資料(Input):
0 0 5 10 0 8
0 0 5 10 10 5
預期的正確結果(Expected Output):
Quick in
Nearly
錯誤結果(Wrong Output):
Quick in
Nearly
Nearly
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
double x1 , x2 , y1 , y2 , r1 , r2 , temp ;
while (!cin.eof())
{
cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2 ;
temp = sqrt (pow (x1 - x2 , 2) + pow (y1 - y2 , 2));
if ((r1 + r2)>=temp)
{
cout << "Quick in" << endl ;
}
else if ((r1 + r2) < temp)
{
cout << "Nearly" << endl ;
}
}
}
http://codepad.org/tnRLqQJK
補充說明(Supplement):
這個程式是在寫下面這題
https://toj.tfcis.org/oj/pro/19/
-----
Sent from JPTT on my Samsung SM-G955F.
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.11.34.83 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1584016842.A.55D.html
→
03/12 21:03,
4年前
, 1F
03/12 21:03, 1F
看起來是沒有?
→
03/12 21:04,
4年前
, 2F
03/12 21:04, 2F
所以我加了一個!讓它能夠遇到EOF才break
→
03/12 21:04,
4年前
, 3F
03/12 21:04, 3F
上次讀取是指什麼呢?
index嗎?
※ 編輯: jack41402 (101.11.34.83 臺灣), 03/12/2020 21:05:05
→
03/12 21:06,
4年前
, 4F
03/12 21:06, 4F
→
03/12 21:06,
4年前
, 5F
03/12 21:06, 5F
※ 編輯: jack41402 (101.11.34.83 臺灣), 03/12/2020 21:17:27
※ 編輯: jack41402 (101.11.34.83 臺灣), 03/12/2020 21:18:49
→
03/12 21:20,
4年前
, 6F
03/12 21:20, 6F
→
03/12 21:30,
4年前
, 7F
03/12 21:30, 7F
→
03/12 21:30,
4年前
, 8F
03/12 21:30, 8F
了解了,謝謝你
可是我以前有用過cin.fail()判斷eof,但為什麼只有這次有這個問題
※ 編輯: jack41402 (101.11.34.83 臺灣), 03/12/2020 22:55:44
推
03/13 00:24,
4年前
, 9F
03/13 00:24, 9F
→
03/13 00:25,
4年前
, 10F
03/13 00:25, 10F
→
03/13 00:25,
4年前
, 11F
03/13 00:25, 11F
→
03/13 00:26,
4年前
, 12F
03/13 00:26, 12F
→
03/13 00:26,
4年前
, 13F
03/13 00:26, 13F
了解了,謝謝你
※ 編輯: jack41402 (49.219.145.234 臺灣), 03/13/2020 17:52:37