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?
//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? :< )
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?...
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?
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.
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)