943,901 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3628
  • C++ RSS
Jan 16th, 2009
0

break statement

Expand Post »
Hi,
I have this sample program where the compiler gives me error for the break statement.

The error is " misplaced break in afunction"

Could anyone help me understanding why the break statement gives an error? i understand that u can use break in a for , while loop etc., and exit early when required.

thanks for the help
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2.  
  3. void afunction()
  4. {
  5. if(1)
  6. {
  7. cout<<"Err";
  8. break;
  9. }
  10. }
  11.  
  12. int main ()
  13. {
  14.  
  15. afunction();
  16. }
Last edited by Ancient Dragon; Jan 16th, 2009 at 6:23 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aiyer is offline Offline
1 posts
since Jan 2009
Jan 16th, 2009
0

Re: break statement

break statements can be used in looping constructs, and in switch/case blocks. They cannot be used to break out of an "if" block, or as an alternative means of returning from a function.

Also, <iostream.h> is non-standard. #include <iostream> instead.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 16th, 2009
0

Re: break statement

i thing break is for switch case statement only..
Reputation Points: 10
Solved Threads: 2
Light Poster
FrancisC07 is offline Offline
30 posts
since Feb 2008
Jan 16th, 2009
0

Re: break statement

They're for abruptly "breaking" loops.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 16th, 2009
0

Re: break statement

Click to Expand / Collapse  Quote originally posted by aiyer ...
#include <iostream.h>

void afunction()
{
if(1)
{
cout<<"Err";
break;
}
}

int main ()
{

afunction();
}

and exit early when required.
I believe that by placing a break statement you were trying to return back to the main() function.
Placing a
C++ Syntax (Toggle Plain Text)
  1. return;//Instead of break might be what you are looking for
The above program just gives out an Err,
Probably because the above program is a 'test' program(or something of that sort)
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 16th, 2009
0

Re: break statement

yeah, try using return instead...
*mumbles something about multiple exit points*
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array question
Next Thread in C++ Forum Timeline: get key input without pausing the program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC