So I’m trying a new way of getting input from files while using c++. I tried it out on the shell problem in the bronze practice.
1 #include <bits/stdc++.h>
2 int N, ord[3], cnt[3];
3
4 using namespace std;
5 int main()
6 {
7 ofstream fout ("shell.out");
8 ifstream fin ("shell.in");
9 fin >> N;
10 for(int i = 0; i < 3; ++i){
11 ord[i] = i;
12 }
13 int a,b,g;
14 for(int i = 0; i < N; ++i){
15 fin >> a;
16 fin >> b;
17 fin >> g;
18 --a, --b, --g;
19 swap(ord[a], ord[b]);
20 ++cnt[ord[g]];
21 }
22 fout << max({cnt[0], cnt[1], cnt[2]});
23 cout << max({cnt[0], cnt[1], cnt[2]});
24 return 0;
25 }
That was my code. (Sorry for it taking up so much space). Whenever I try to do
fin >> a,b,g;
instead of
fin >> a;
fin >> b;
fin >> g;
I get an segmentation error. Can somebody please explain to me why?