C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 17th, 2005, 09:00 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
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

Reply With Quote
  #2  
Old October 17th, 2005, 10:07 PM
jaf1211 jaf1211 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 21 jaf1211 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 15 sec
Reputation Power: 0
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...

Reply With Quote
  #3  
Old October 18th, 2005, 03:34 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 739 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 29 m 29 sec
Reputation Power: 4
Post your code please. We think in code

ps. jaf1211, that is not c++..?

Reply With Quote
  #4  
Old October 20th, 2005, 12:04 AM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
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.

Reply With Quote
  #5  
Old October 20th, 2005, 04:23 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 739 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 29 m 29 sec
Reputation Power: 4
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
Comments on this post
MichaelSoft agrees!

Reply With Quote
  #6  
Old October 20th, 2005, 06:16 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
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...

Reply With Quote
  #7  
Old October 21st, 2005, 07:11 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
Quote:
Originally Posted by Icon
Ok, i know what is going on. If you look at the compiler error there is extra information:


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)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Random questions of programming in C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT