Switch not execute commands(&& crosses initialization)

Hi!
I meet problems with Switch statement... Because when I try to execute some code in main it's run correctly, but when I put it in a case of switch statement it won't work, can you explain me why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  //In main it run correctly
  string note;
  cout<<"Note: ";
  char cnote[100];
  cin.getline(cnote,101); /*type "Hey how are you?"*/
  note=cnote;
  cout<<note;/*output on screen "Hey how are you?"*/
  system("pause");

  /*In switch this not works*/
string c;/*If I declare string there, the crosses initializated error disappears, but the statements still not work*/
switch(choose)
{  
  /*string note; If I declare string there it said "crosses initializated string note;
  Same thing if I try to declare int a=-1;
*/
  cout<<"Note: ";
  char cnote[100];
  cin.getline(cnote,101); //The program doesn't stop there for the input
  note=cnote;
  cout<<note; //nothing as I have input nothing
  system("pause");//this works
}


Can someone explain me where am I wrong?(Or what don't I know? :< )

Ask me more details if you need it!
The problem appears to be that you're trying to declare a variable inside a case statement that doesn't include braces to create a block. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
...
   switch(choose)
   {
      case 'A'
         int test; // Error.
         break;
      case 'B'
      {
         int test1;  // Works, but remember test1 is only valid inside this block.
         break;
      }
   }
...


You may actually want to declare your variables before your switch statement instead of trying to create them inside the case statements.

Okè mmh, now I declare all variables out of the statement(I prefer do in this way at least for now as the program doesn't work yet)... In this way could function right?...
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
int* tel=new int;
        string note;
        char cnote[100];
        string* n=new string;
        string* c=new string;
        string* i=new string;
        int g=-1,m=-1,a=-1,numax=0;
        switch(opzione)
        {
            case 1:
                cout<<"Nome: "; /*work*/
                cin>>*n; /*work*/
                cout<<"Cognome: "; /*work*/
                cin>>*c; /*work*/
                cout<<"Indirizzo: "; /*work*/
                cin>>*i; /*work*/
                do /*work*/
                {
                    if(cin.fail())
                    {
                        cerr<<"Il tipo di dato e' sbagliato!";
                        cin.clear();
                        cin.ignore(255,'\n');
                    }
                    else
                    {
                        cout<<"Data nascita: "; /*work*/
                        cin>>g>>m>>a; /*work*/
                        cout<<g<<" "<<m<<" "<<a<<endl; /*work*/
                    }
                }while(!am->controlloData(g,m,a) || cin.fail()); /*work*/
                cout<<"Digitare 0 se si vuole omettere il campo\nLimite di 3 numeri di telefono\n"; /*work*/
                do /*work*/
                {
                    cout<<"Numero di telefono: "; /*work*/
                    cin>>*tel; /*work*/
                    numax++; /*work*/
                }while(am->isTelMax()!=true && *tel!=0 && numax<3);  /*work*/
                if(cin.fail()) cout<<"Error!"; /*work*/
                else cout<<"No error!"; /*Here the output is "No error!"*/ /*work*/
                cout<<"Note: "; /*work*/
/*But in this point is as the program pass to "system("pause");" directly*/
                cin.getline(cnote,101); /*not work*/
                note=cnote;
                cout<<note;
                system("pause");
                am->mettiNote(note);
                system("pause");
                am->aggiunta(*n,*c,*i,*tel);
                delete tel;
                delete n;
                delete c;
                delete i;
                break;
            case 2:
                break;
            case 3:
                am->stampa(cout);
                break;
            case 4:
                break;
            case 5:
                cout<<"Fine!";
                system("pause >nul");
                system("cls");
                break;
            default:
                break;
        }
Last edited on
What's with all those pointers and the dynamic memory. Why not just use "standard" instances of those variables? And also use meaningful variable names instead of all those horrible single letter variable names.

Lastly I recommend you always use braces with your control statements, even when not technically required, until you are much more familiar with the language.


Okay but even if I use "standard" instances, the comand cin.getline(note,100) not work in switch case, even if this code works in main, can you explain me why?
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
76
string tel;
        string note;
        char cnote[101];
        string n;
        string c;
        string i;
        int g=-1,m=-1,a=-1,numax=0;
        switch(opzione)
        {
            case 1:
                {
                cout<<"Nome: ";
                cin>>n;
                cout<<"Cognome: ";
                cin>>c;
                cout<<"Indirizzo: ";
                cin>>i;
                do
                {
                    if(cin.fail())
                    {
                        cerr<<"Il tipo di dato e' sbagliato!";
                        cin.clear();
                        cin.ignore(255,'\n');
                    }
                    else
                    {
                        cout<<"Data nascita: ";
                        cin>>g>>m>>a;
                        cout<<g<<" "<<m<<" "<<a<<endl;
                    }
                }while(!am->controlloData(g,m,a) || cin.fail());
                cout<<"Digitare 0 se si vuole omettere il campo\nLimite di 3 numeri di telefono\n";
                do
                {
                    cout<<"Numero di telefono: ";
                    cin>>tel;
                    numax++;
                }while(am->isTelMax()!=true && tel!="0" && numax<3);
                if(cin.fail()) cout<<"Error!";
                else cout<<"No error!";
                cout<<n<<c;
                cout<<"Note: ";
/*Why the program jump cin.getline in this point?*/
                cin.getline(cnote,101);
                note=cnote;
                cout<<note;
                system("pause");
                am->mettiNote(note);
                system("pause");
                am->aggiunta(n,c,i,tel);
                break;
                }
            case 2:
                {
                    break;
                }
            case 3:
                {
                    am->stampa(cout);
                    break;
                }
            case 4:
                {
                    break;
                }
            case 5:
                {
                   cout<<"Fine!";
                    system("pause >nul");
                    system("cls");
                    break;
                }
        }
    }while(opzione!=5);
    delete am;


p.s. The exercise ask me to use pointers.
The exercise ask me to use pointers.

Why, and if this is true you probably shouldn't be using C++ strings.

Your problem is being caused because you're switching from the extraction operator>> to getline(). The extraction operator leaves the end of line character in the input buffer which causes the next getline() take as the complete entry. You need to ignore() the end of line character before you switch to getline().

You really really need to start using meaningful variable names and consistently use a decent indentation style if you want others to help you troubleshoot your code. As presented your code is confusing and difficult to read because of these issues.

Edit: You also don't actually need those "extra" braces in your case statements.



Last edited on
1)Why my code don't have "a decente identation style"? O.o I leave backspaces and use {} p.s. I not use {} in the cases of switch because I don't know that I can use it, so thanks for make me know it! And I go '\n' when usually o.O

2)Thanks for suggest me to use ignore(), actually I don't know that the operator >> leaves the end of line character in the stream input.(Now I try to use it and hope that this was the mistake)

Edit: The issue is solved!
Last edited on
Topic archived. No new replies allowed.