Help with Switch..
Apr 12, 2013 at 7:17pm UTC
when i want to add case 3 to the switch it give an error ?!?
and i can't figure it out...
it says: transfer of control by passes initialization of:
the code for case 3 is
This is the program down there
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
cout<<"1.Triangle" <<endl;
cout<<"2.mark" <<endl;
cout<<"3.exist" <<endl;
int choice;
cout<<"enter your choice" <<endl;
cin>>choice;
while (choice!=3) {
switch (choice)
{
case 1:
int a,b,c;
cout<<"enter length" <<endl;
cin>>a>>b>>c;
if (a==b && a==c && c==b)
cout<<"its quilateral" <<endl;
else
if (a==b || a==c || c==b)
cout<<"its isocelces" <<endl;
else
cout<<"its scaelene" <<endl;
break ;
case 2:
int m1,m2,m3,m4;
double sum=0; int i=1;
double avg; string result;
ifstream infile;
infile.open("output.txt" );
if (!infile)
{
cout<<"not exist" <<endl;
return 1;
}
infile>>m1>>m2>>m3>>m4;
cout<<" Calculus" <<" Physics" <<" English" <<" Arabic" <<" Sum" <<" Avg" <<" Result" <<endl;
cout<<" -------------------------------------------------------" <<endl;
while (infile)
{
sum=sum+m1+m2+m3+m4;
avg=sum/4.0;
if (avg>=60)
result="Pass" ;
else
result="Fail" ;
cout<<"Student#" <<i<<" \t" <<m1<<" \t" <<m2<<" \t" <<m3<<" \t" <<m4<<" \t" <<sum<<" \t" <<avg<<" \t" <<result<<endl;
i++;
sum=0;
infile>>m1>>m2>>m3>>m4;
}
infile.close();
break ;
}
cout<<"enter your choice" <<endl;
cin>>choice;
}
return 0;
}
Apr 12, 2013 at 7:32pm UTC
Ok that is because you are initializing some variables in case 2. Try doing this or at least declaring all the variables before the switch block.
Aceix.
Apr 12, 2013 at 7:39pm UTC
Thanks Man.
i appreciate that.
Topic archived. No new replies allowed.