Using fstream with switch-case statement
Apr 9, 2009 at 7:34am UTC
When I am using the ofstream to declare a variable in one case, there is error and the output says that
"error C2360: initialization of 'in' is skipped by 'case' label"
code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
case 1: ofstream in(filename, ios::app);
Log.actionup();
in << Log.getaction() << "\t" << bucket.getbk1() << "\t" << bucket.getbk2() << "\t" << bucket.getbk3() << endl;
in.close();
cout << "Action\tBucket 1\tBucket 2\tBucket 3\n" ;
cout << Log.getaction() << "\t" << bucket.getbk1() << "\t" << bucket.getbk2() << "\t" << bucket.getbk3() << endl;
cout << "Please choose the bucket to empty: " ;
cin >> from;
cout << endl << "Please choose the bucket to store the water: " ;
cin >> to;
cout << endl;
finish = bucket.process(from, to);
break ;
case -1: cout << "Action\tBucket 1\tBucket 2\tBucket 3\n" ;
cout << Log.getaction() << "\t" << bucket.getbk1() << "\t" << bucket.getbk2() << "\t" << bucket.getbk3() << endl;
cout << "Please choose the bucket to empty: " ;
cin >> from;
cout << endl << "Please choose the bucket to store the water: " ;
cin >> to;
cout << endl;
finish = bucket.process(from, to);
break ;
what can I do to solve it?
Apr 9, 2009 at 8:35am UTC
add braces to keep 'in' in a smaller scope:
1 2 3 4 5 6
case 1:
{
ofstream in(filename, ios::app);
//...
}
break ;
Apr 9, 2009 at 8:59am UTC
Thanks a lot
Topic archived. No new replies allowed.