when i add default to the end of my switch there is an error that pops up that says "initialization of "pos" is skipped by default label" what does that mean? the thing is when i delete the default from my code, my code, executes, but obviously i cant use it if i dont have the default i am going to include the case where i get the error.
case 2:
cout << " please enter a word: ";
cin >> word;
cout << "please enter a part of the word:";
cin >> letters;
int pos = word.find(letters);
if (pos>0)
{
cout << letters << " is found in the " << pos;
}
else{
cout << letters << " is not found in " << word;
}
break;
default:
cout << "*****Invalid Selection. Please enter 1,2,3.";
}
}while (ans !="y");
return 0;
}
the damn error keeps showing when i add the default could somebody please explain to me what exactly is going on?
switch ( <variable> ) {
casethis-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value
break;
...
default:
Code to execute if <variable> does not equal the value following any of the cases
break; // <-- this one after the default
}
According to paragraph #3 of section "6.7 Declaration statement" of the C++ Standard
3 It is possible to transfer into a block, but not in a way that bypasses declarations with initialization.
In your code snip passing the control from the switch statement to the label default bypasses the declaration of variable pos with initialization
1 2 3
int pos = word.find(letters);
...
default:
To escape the error you could write the following way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
case 2:
{
cout << " please enter a word: ";
cin >> word;
cout << "please enter a part of the word:";
cin >> letters;
int pos = word.find(letters);
if (pos>0)
{
cout << letters << " is found in the " << pos;
}
else{
cout << letters << " is not found in " << word;
}
break;
}
By the way if word has type std::string then the condition
if (pos>0)
is invalid because letters can be in word in position 0
- Sir, you don't have a switch statement at all in your OP....Unless that's partial code, and if that's the case it would help us help you if you posted your entire code (using code brackets, the "<>" button, or the bbcode "[code.]your code goes here[/code.]" without the periods.
thank you so much that was it. I didnt think you could put brackets in a case structure unless you had if/else statements, but now i know i was wrong. you can tell im a beginner hehehe. thanks again. i also fixed (pos>=0).
Sir, the error message is described in the original post.
- Him stating that he got an error, does not imply that only that error message was shown when he ran the program. You would know all of the error messages that were shown if he had put his entire code in his post, but here we are.
vlad from moscow wrote:
I do not know what is the reason that prevents you to read the original post carefully.:)
- Why would i waste my time reading his post if he won't even show his entire code, so that i can see exactly what's going on? The answer: I wouldn't.
- Him stating that he got an error, does not imply that only that error message was shown when he ran the program. You would know all of the error messages that were shown if he had put his entire code in his post, but here we are.
He bothered about this error and asked the question about this error though I accept that his full code can contain other errors. But it is his problem.
We need not do the full code review of the whole program each time when we are asked about some single error.:)
He bothered about this error and asked the question about this error though I accept that his full code can contain other errors. But it is his problem.
We need not do the full code review of the whole program each time when we are asked about some single error.:)
- True, but giving us the full code instead of partial code does not guarantee that we will be able to provide the best assistance as possible, while giving us the full code would.
- It's also true that we aren't necessarily responsible for things that he fails to mention, but not providing others with the best chance to help you is highly illogical as far as i'm concerned, and it makes little sense. However, that's just my opinion and these posters can do whatever they want.
@thejman250
but giving us the full code instead of partial code does not guarantee that we will be able to provide the best assistance as possible, while giving us the full code would.
I agree that maybe there was little information for some programmers but there was enough information for me to provide the best assistance.:)
LMAO.............i logged in today and saw all these messages posted on my thread so i had to reply. Sorry guys i just found it funny the way you entered a discussion and kept it so cordial, but to clear things up .....vlad answered my question and helped me solve my problem because that was the only error i was having, but jman you have a point i didnt show the whole code even though i was getting that error. i do realize that the error could have been anywhere else in the code but luckily it wasnt, but if i would have continued to have problems with the code i would have then posted it completely. Anyways i appreciate all the help. keep on truckin. my code is working fine. thumbs up! :D