質問編集履歴

2

情報の追加依頼に基づき編集しました

2018/09/11 12:13

投稿

Removed_Past
Removed_Past

スコア14

test CHANGED
@@ -1 +1 @@
1
- out_of_range どこから起きているのか分からないです
1
+ データ正しく出力されません
test CHANGED
@@ -4,174 +4,164 @@
4
4
 
5
5
  C++を用いて自動採点のシステムを開発しているのですが、
6
6
 
7
- out_of_rangeの例外がどこから発生して、どう処理したらいいのかが分かりません。
7
+ ~~out_of_rangeの例外がどこから発生して、どう処理したらいいのかが分かりません。~~
8
+
9
+ 今度はデータが正しく出力されない問題が発生しました…
8
10
 
9
11
 
10
12
 
11
13
  ### 発生している問題・エラーメッセージ
12
14
 
13
-
15
+ ~~terminate called after throwing an instance of 'std::out_of_range'what(): basic_string::substr: __pos (which is 4) > this->size() (which is 0)This application has requested the Runtime to terminate it in an unusual way.
16
+
17
+ Please contact the application's support team for more information.
18
+
19
+ ~~
20
+
21
+ ### 該当のソースコード
22
+
23
+
24
+
25
+ ```C++
26
+
27
+ #include <iostream>
28
+
29
+ #include <fstream>
30
+
31
+ #include <string>
32
+
33
+ #include <vector>
34
+
35
+ #include <utility>
36
+
37
+ #include "AnswerCheck.h"
38
+
39
+
40
+
41
+ typedef vector<string> Type;
42
+
43
+
44
+
45
+ Type answer;
46
+
47
+
48
+
49
+ Type putdata(string s){
50
+
51
+ int cnt = 0, pnt = 0, cha = 0;
52
+
53
+ Type buf;
54
+
55
+
56
+
57
+ while(1){
58
+
59
+ if(buf.size() == 13) break;
60
+
61
+ if(s[cha] != ','){
62
+
63
+ cnt++;
64
+
65
+ }
66
+
67
+ else{
68
+
69
+ ** try{
70
+
71
+ buf.push_back(s.substr(pnt + 1, cnt - 2));
72
+
73
+ }
74
+
75
+ catch(...){
76
+
77
+ buf.push_back("");
78
+
79
+ }
80
+
81
+ pnt += ++cnt;
82
+
83
+ cnt = 0;
84
+
85
+ }
86
+
87
+ cha++;
88
+
89
+ }
90
+
91
+
92
+
93
+ return buf;
94
+
95
+ }
96
+
97
+
98
+
99
+ int main(void){
100
+
101
+ ifstream ifs("data.csv");
102
+
103
+ ofstream log("log.csv", ios::trunc);
104
+
105
+ int cnt = 0;
106
+
107
+
108
+
109
+ log << "Class,Number,A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,B6,A Result,Sum Result" << endl;
110
+
111
+
112
+
113
+ string s;
114
+
115
+ while(getline(ifs, s)){
116
+
117
+ int a_result = 0, result = 0;
118
+
119
+ if(cnt != 0){
120
+
121
+ Type buf = putdata(s);
122
+
123
+ string id = buf[1];
124
+
125
+ pair<string, string> user;
126
+
127
+ user.first = id.substr(4, 1);
128
+
129
+ user.second = id.substr(5, 2);
130
+
131
+ cout << "Class : " << user.first << endl
132
+
133
+ << "Number : " << user.second << endl;
134
+
135
+ log << user.first << ","
136
+
137
+ << user.second << ",";
138
+
139
+ for(int a = 2; a < buf.size(); a++){
140
+
141
+ int score = check(buf[a], a - 2);
142
+
143
+ result += score;
144
+
145
+ if(a < 7) a_result += score;
146
+
147
+ log << score << ",";
148
+
149
+ }
150
+
151
+ log << a_result << "," << result << flush << endl;
152
+
153
+ }
154
+
155
+
156
+
157
+ cnt++;
158
+
159
+ }
160
+
161
+ }
14
162
 
15
163
  ```
16
164
 
17
- terminate called after throwing an instance of 'std::out_of_range'
18
-
19
- what(): basic_string::substr: __pos (which is 4) > this->size() (which is 0)
20
-
21
-
22
-
23
- This application has requested the Runtime to terminate it in an unusual way.
24
-
25
- Please contact the application's support team for more information.
26
-
27
- ```
28
-
29
-
30
-
31
- ### 該当のソースコード
32
-
33
-
34
-
35
- ```C++
36
-
37
- #include <iostream>
38
-
39
- #include <fstream>
40
-
41
- #include <string>
42
-
43
- #include <vector>
44
-
45
- #include <utility>
46
-
47
- #include "AnswerCheck.h"
48
-
49
-
50
-
51
- typedef vector<string> Type;
52
-
53
-
54
-
55
- Type answer;
56
-
57
-
58
-
59
- Type putdata(string s){
60
-
61
- int cnt = 0, pnt = 0, cha = 0;
62
-
63
- Type buf;
64
-
65
-
66
-
67
- while(1){
68
-
69
- if(buf.size() == 13) break;
70
-
71
- if(s[cha] != ','){
72
-
73
- cnt++;
74
-
75
- }
76
-
77
- else{
78
-
79
- ** try{
80
-
81
- buf.push_back(s.substr(pnt + 1, cnt - 2));
82
-
83
- }
84
-
85
- catch(...){
86
-
87
- buf.push_back("");
88
-
89
- }
90
-
91
- pnt += ++cnt;
92
-
93
- cnt = 0;
94
-
95
- }
96
-
97
- cha++;
98
-
99
- }
100
-
101
-
102
-
103
- return buf;
104
-
105
- }
106
-
107
-
108
-
109
- int main(void){
110
-
111
- ifstream ifs("data.csv");
112
-
113
- ofstream log("log.csv", ios::trunc);
114
-
115
- int cnt = 0;
116
-
117
-
118
-
119
- log << "Class,Number,A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,B6,A Result,Sum Result" << endl;
120
-
121
-
122
-
123
- string s;
124
-
125
- while(getline(ifs, s)){
126
-
127
- int a_result = 0, result = 0;
128
-
129
- if(cnt != 0){
130
-
131
- Type buf = putdata(s);
132
-
133
- string id = buf[1];
134
-
135
- pair<string, string> user;
136
-
137
- user.first = id.substr(4, 1);
138
-
139
- user.second = id.substr(5, 2);
140
-
141
- cout << "Class : " << user.first << endl
142
-
143
- << "Number : " << user.second << endl;
144
-
145
- log << user.first << ","
146
-
147
- << user.second << ",";
148
-
149
- for(int a = 2; a < buf.size(); a++){
150
-
151
- int score = check(buf[a], a - 2);
152
-
153
- result += score;
154
-
155
- if(a < 7) a_result += score;
156
-
157
- log << score << ",";
158
-
159
- }
160
-
161
- log << a_result << "," << result << flush << endl;
162
-
163
- }
164
-
165
-
166
-
167
- cnt++;
168
-
169
- }
170
-
171
- }
172
-
173
- ```
174
-
175
165
 
176
166
 
177
167
  ### 試したこと
@@ -199,3 +189,19 @@
199
189
  「data.csv」のサンプルです。
200
190
 
201
191
  [data.csv](https://drive.google.com/file/d/1IewVqd_p9o7AjEgH_-45knRZEZ2iMDR8/view?usp=sharing)
192
+
193
+
194
+
195
+ 開発環境はMinGW6.3.0です。
196
+
197
+ エディタはTeraPadを使用しています。
198
+
199
+
200
+
201
+ 回答者のご指摘からmain関数にtry,catchを入れたところ、ひとまずエラーが解消されました。
202
+
203
+ ですが、入力したデータは4つ連続しているのですが、出力した「log.csv」には以下のように無駄に空白が入ってしまいました…
204
+
205
+ 良ければ原因は何処なのか教えてほしいです…
206
+
207
+ ![log.csv](66c6b6bf171fe380090678ccc9a3b0c9.png)

1

情報の追加依頼に基づき編集しました

2018/09/11 12:13

投稿

Removed_Past
Removed_Past

スコア14

test CHANGED
File without changes
test CHANGED
@@ -189,3 +189,13 @@
189
189
  「#include "AnswerCheck.h"」は問題採点用のヘッダで、こちらにエラーが無い事は確認済みです。
190
190
 
191
191
  また、読み込みファイルの「data.csv」は読み込むデータが1つのみ(Googleフォームの結果を落としているので2行)の場合は問題なく動きます。
192
+
193
+
194
+
195
+ ### 追加
196
+
197
+
198
+
199
+ 「data.csv」のサンプルです。
200
+
201
+ [data.csv](https://drive.google.com/file/d/1IewVqd_p9o7AjEgH_-45knRZEZ2iMDR8/view?usp=sharing)