回答編集履歴
4
修正したコードを追加
answer
CHANGED
@@ -54,4 +54,25 @@
|
|
54
54
|
}
|
55
55
|
}
|
56
56
|
```
|
57
|
-
`address [Tokyo 1丁目 111-222], japan: 1` だけが読めました。
|
57
|
+
`address [Tokyo 1丁目 111-222], japan: 1` だけが読めました。
|
58
|
+
|
59
|
+
**追記2**
|
60
|
+
```C++
|
61
|
+
#include <fstream>
|
62
|
+
#include <iostream>
|
63
|
+
#include <string>
|
64
|
+
using namespace std;
|
65
|
+
|
66
|
+
int main()
|
67
|
+
{
|
68
|
+
string address, tmp;
|
69
|
+
bool japan;
|
70
|
+
ifstream file;
|
71
|
+
file.open("sample.txt");
|
72
|
+
|
73
|
+
while (getline(file, address) && getline(file, tmp)) {
|
74
|
+
japan = tmp == "1";
|
75
|
+
cout << "address [" << address << "], japan: " << japan << endl;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
```
|
3
コードを追加
answer
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
file.eof() が false だからといって、getline や file >> が成功するとは限りません。
|
6
6
|
次のように getline や file >> の返した値をチェックしないといけないでしょう。
|
7
7
|
```C++
|
8
|
-
while (getline(file, address) && (file >> country) {
|
8
|
+
while (getline(file, address) && (file >> country)) {
|
9
9
|
...
|
10
10
|
}
|
11
11
|
```
|
@@ -23,4 +23,35 @@
|
|
23
23
|
file >> country; を実行した後は、'\n' は入力に残ったままなので、
|
24
24
|
次の getline で、address は "" になるでしょう。
|
25
25
|
|
26
|
-
sample.txt の内容も質問に追加してください。
|
26
|
+
sample.txt の内容も質問に追加してください。
|
27
|
+
|
28
|
+
**追記**
|
29
|
+
修正された質問のコードに、
|
30
|
+
`cout << "address [" << address << "], japan: " << japan << endl;`
|
31
|
+
を追加して実行すると
|
32
|
+
```text
|
33
|
+
address [Tokyo 1丁目 111-222], japan: 1
|
34
|
+
address [], japan: 1
|
35
|
+
```
|
36
|
+
無限ループにはなっていません。
|
37
|
+
|
38
|
+
コードを次のようにすると、
|
39
|
+
```C++
|
40
|
+
#include <fstream>
|
41
|
+
#include <iostream>
|
42
|
+
#include <string>
|
43
|
+
using namespace std;
|
44
|
+
|
45
|
+
int main()
|
46
|
+
{
|
47
|
+
string address;
|
48
|
+
bool japan;
|
49
|
+
ifstream file;
|
50
|
+
file.open("sample.txt");
|
51
|
+
|
52
|
+
while (getline(file, address) && (file >> japan)) {
|
53
|
+
cout << "address [" << address << "], japan: " << japan << endl;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
```
|
57
|
+
`address [Tokyo 1丁目 111-222], japan: 1` だけが読めました。
|
2
adddress を address に修正
answer
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
ifstream の file を使っているのに、cin の ignore や clear は無意味です。
|
4
4
|
|
5
5
|
file.eof() が false だからといって、getline や file >> が成功するとは限りません。
|
6
|
-
|
7
6
|
次のように getline や file >> の返した値をチェックしないといけないでしょう。
|
8
7
|
```C++
|
9
8
|
while (getline(file, address) && (file >> country) {
|
@@ -13,7 +12,7 @@
|
|
13
12
|
あるいは、
|
14
13
|
```C++
|
15
14
|
while (true) {
|
16
|
-
getline(file,
|
15
|
+
getline(file, address);
|
17
16
|
if (file.eof()) break;
|
18
17
|
file >> country;
|
19
18
|
if (file.eof()) break;
|
1
> を >> に修正
answer
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
> アドバイスをいただけないでしょうか?
|
2
2
|
|
3
3
|
ifstream の file を使っているのに、cin の ignore や clear は無意味です。
|
4
|
+
|
4
5
|
file.eof() が false だからといって、getline や file >> が成功するとは限りません。
|
5
6
|
|
6
|
-
次のように getline や file > の返した値をチェックしないといけないでしょう。
|
7
|
+
次のように getline や file >> の返した値をチェックしないといけないでしょう。
|
7
8
|
```C++
|
8
9
|
while (getline(file, address) && (file >> country) {
|
9
10
|
...
|