| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Random questions of programming in C++
Okay so I won't make new threads I decided to make this one so I can write the problems.
This question may be easy for you guys but I have tried to take this problem for already 2 days and I just can't find the solution. So in my program(which is not complete) I used the switch function( or I think that is it's name) this is what I mean with the switch function (just if it has a word that is known for instead of the switch function) Code:
switch(choice)
{
case 1:
blah blah blah
break;
case 2:
blah blah blah
break;
case 3:
blah blah blah
break;
}
e.t.c. so you guys get the point. Okay here is my story I compiled the little I had of the code and then I get errors off this switch function So heres my questions. Question 1) Okay I get the error: jump to case label And it gives my the line case 2: for that error. Oh yeah and all other case I have except for the first one I get the same error. So my question is why do I get this error and how can I fix this error? Question 2)I know about the default: thing and for now I am not using it so will that be a problem. Will not using default in the switch affect the program and if so how? ---- So yeah that is the problems I can't just find the solution plz help me. P.S. new to this switch function, also did not post the code cause I do not think its a thing that matters cause I think I may have used the function wrong-as always lol |
|
#2
|
|||
|
|||
|
Try somethign like this..
Code:
case "$variable" in "$condition1" ) command... ;; "$condition2" ) command... ;; esac edit sorry I didn't realize you were using a switch...Is it nessasry that you use it? I could be completly wrong tho... |
|
#3
|
||||
|
||||
|
Post your code please. We think in code
ps. jaf1211, that is not c++..? |
|
#4
|
|||
|
|||
|
Okay Icon to make you happy here is the part of the code
![]() But first read whats after the code then look at the code Code:
switch (choice)
{
case 1:
cout << "You picked minute.\n"
<< "There are " << min << " seconds in 1" << num <<" minute." << endl
<<" Now you can see how many seconds are there in the"
<<" desired minutes." << endl;
// state how many seconds are there in a minute and put it
// as one integer named min
int min = ( sec * 60);
cout << "Enter the number of minutes you desire to know how" <<" many seconds are there. (2 - 59):";
cin >> num;
if ( num < 2 || num > 59)
{
break;
}
else
{
cout << "There are " << ( min * num) << " seconds in " << num << " minutes.";
cout << " Type function: ";
cin >> func;
}
break;
case 2:
cout << "You picked hour.\n"
<< "There are " << hour << " seconds in 1"
<< " hour." << endl
<< " Now you can see how many seconds are there in the"
<< " desired hours."<< endl;
// state how many seconds are there in a hour and put it
// as one integer name hour
int hour = (min * 60);
cout << "Enter the number of hours you desire to know how"
<< " many seconds are there. (2 - 23):";
cin >> num;
if ( num < 2 || num > 23)
{
break;
}
else
{
cout << "There are " << ( hour * num) << " seconds in " << num << " hours.";
cout << " Type function: ";
cin >> func;
}
case 3:
cout << "You picked day.\n"
<< "There are " << day << " seconds in 1"
<< " day." << endl
<< " Now you can see how many seconds are there in the"
<< " desired days." << endl;
// state how many seconds are there in a day and put it
// as one integer name day
int day = ( hour * 24);
cout << "Enter the number of days you desire to know how"
<< " many seconds are there. (2 - 6):";
cin >> num;
if ( num < 2 || num > 6)
{
break;
}
else
{
cout << "There are " << ( day * num) << " seconds in " << num << " days.";
cout << "Type function: ";
cin >> func;
}
case 4:
cout << "You picked week.\n"
<< "There are " << week << " seconds in 1"
<< " week." << endl
<< " Now you can see how many seconds are there in the"
<< " desired weeks." << endl;
//state how many seconds are there in a day and put it
// as one integer name week
int week = ( day * 7);
cout << "Enter the number of weeks you desire to know how"
<< " many seconds are there. (2 - 3):";
cin >> num;
if ( num < 2 || num > 3)
{
break;
}
else
{
cout << "There are " << ( week * num) << " seconds in " <<
num << " weeks.";
cout << "Type function: ";
cin >> func;
};
Okay so you see the program is simply it tell how many seconds are there in whatever you pick. Okay also remember for now my only complain right now is the case problem here are the questions Question 1) Okay I get the error: jump to case label And it gives my the line case 2: for that error. Oh yeah and all other case I have except for the first one I get the same error. So my question is why do I get this error and how can I fix this error? Question 2)I know about the default: thing and for now I am not using it so will that be a problem. Will not using default in the switch affect the program and if so how? and I know you guys see the func and know nothing about it but remember of my complains also I know you people know how to do the program but plz give me tips and tell me whats wrong not the code. I hope this helps Icon cause I need help. |
|
#5
|
||||
|
||||
|
Ok, i know what is going on. If you look at the compiler error there is extra information:
sw.cpp:42: error: jump to case label sw.cpp:19: error: crosses initialization of `int min' For some reason you can declare local variables inside a case statement if it is not enclosed in a block. Putting evert case between { and } solves the problem (better solutions exist though) Using a default case is very good practice! You should always try to implement a default case more info: http://www.cs.uwm.edu/classes/cs252/faq/compiler-errors.html |
|
#6
|
|||
|
|||
|
Yep, it is the 'local initialization' of the variables.
The switch-statement can give strange errormessages, and (like Icon indicates) you must always look at the following errors if you don't get the first one. (Just remember the errors you get when only one ; or bracket is missing somewhere )Using if() and else if() in your code will also take care of the errors. In my opinion that would be a better solution then using extra brackets. A switch-statement should be short, basically fit on one page max. The best solution would be using functions in the switch statement... |
|
#7
|
|||
|
|||
|
Quote:
yeah I saw that maybe I forgot about saying about that error well thanks for the help Icon and also thanks for the link it helps also thank you MichaelSoft but with you I have no idea what you saw I understand using the if() and else if () but the other I am clueless. Just to put you guys in a little secret to explain anything to me you need to break whatever you are talking about in little pieces cause I do not understand things that well but thanks for your help both of you guys and mostly Icon ( not to say MichaelSoft did not help or anything mean but come on me understanding this problem came from Icon so thank you) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Random questions of programming in C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|