unreachable code at beginning of switch statement|

Jan 16, 2011 at 7:50pm
hey , plz help me
here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
while((c=Menu())!='6')
            {
 switch (c)
           {
         case '1' : cout<<"\nentrer le nombre d'etudiant de la cycle EEA :  ";
                    cin>>i;
                    etudiant EEA[i];      // etudiant is a class

                           break;
         case '2' : break;


                }


it keep giving me : " unreachable code at beginning of switch statement"
what's the problem ??
Jan 16, 2011 at 7:57pm
You probably need to post more of the code if you can, but my first shot is that you want something more like this:

1
2
3
4
5
c = Menu();
while(c != 6) //Leave out those single quotes
{ 
switch(c)
{//...Also drop the single quotes from here on as well 


You cannot use switch to test for characters like this, which is what declaring those conditions with quotes does. By leaving out the single quotes you tell it to test for numbers instead.
Last edited on Jan 16, 2011 at 7:57pm
Jan 16, 2011 at 8:03pm
here the code , srry some phrases are in french (we use french language in Morocco :) )
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
class etudiant : 
            {
                public :
                
                double moyenne;
                etudiant()
                {
                cout<<"\nentrer le moyenne d'etudiant : ";
                cin>>moyenne;
                cout<<"\n------------------";
               
                }
            };
          

char Menu()
{
 char c;
 cout<<"______________________________________Menu_____________________________\n\n";
 cout<<"     - 1 : Saisir les infos                                      \n";
 cout<<"     - 2 : Ajouter ou supprimer des etudiants                    \n";
 cout<<"     - 3 : Ajouter ou supprimer des enseignants                  \n";
 cout<<"     - 4 : Afficher la liste des etudiants                       \n";
 cout<<"     - 5 : Afficher la liste des enseignants                     \n";
 cout<<"     - 6 :            quitter                                    \n";
 cin>>c;
 return c;

}


int main()
      {
          char c;
          int i;
         

          while((c=Menu())!='6')
            {
                switch (c)
                {
                case '1' :   cout<<"\nentrer le nombre d'etudiant de la cycle EEA :  ";
                                 cin>>i;
                                 etudiant EEA[i];
                           break;
                case '2' : break;

                }

            }
            return 0;

      }


i did what yu told me , but doesn't work :(
Jan 16, 2011 at 8:10pm
It looks okay to me, except for line 44. You can't declare a dynamically sized array like that; you'd have to use new[].
Jan 16, 2011 at 8:16pm
First off it seems I was wrong in regards to my statement about the switch statement.

I'm looking into the problem now. Thanks for posting your code.

EDIT: I think Zhuge has it, except that I spotted you were trying to call the constructor you declared in line 6 in which case you need to tell the declaration to expect an integer as input and you need to change line 44 to: etudiant EEA(i); with the curvey brackets instead of the square ones.
Last edited on Jan 16, 2011 at 8:19pm
Jan 16, 2011 at 8:19pm
Ty so much , it worked . im stupid :)
Topic archived. No new replies allowed.