Is using the break statement bad practice?

Line 32: error: break statement not within loop or switch C++?

  • If user input is 0, the loop ends,what is wrong? #include <iostream> using namespace std; int main() { int balance=0, withdrawal=0, deposit=0; int a; int total=0; { cout << "Welcome user. Please deposit an opening balance"<< endl; cin >> balance; for (int i=0; i < 5; i++) cout << "Withdraw: Enter a negative number. Deposit: Enter a positive number. Enter 0 to exit." << endl; cin >> a; if (a > 0) { withdrawal++; total+=a; } else if (a < 0) { deposit++;total+=a; } else { cout << "Opening balance: "<< balance << endl; cout << "Number of Withdrawals: " << withdrawal << endl; cout << "Number of Deposits: " << deposit << endl; cout << "Closing Balance: " << total << endl; break; } } }

  • Answer:

    Its by design that the loop should end when a = 0. That's what the break there does. If you want to continue running the loop no matter what, remove the break statement. Maybe you pasted it wrong here, but the second { from the top is in a weird place.

TripLine at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

You don't use break statements in if-else conditionals, only in switch blocks and as a loop terminator if a certain condition is met while the loop is executing.

SteveO

Your break statement is inside an else block, but not a loop. It looks like you intended for it and the surrounding if else blocks to be inside the for loop. However, without adding braces to the for loop, it will only run for the very next line after the for statement itself, which in this case is an output call. Here, I'll mark the code to make it easier to see: for (int i=0; i < 5; i++) /*This line is the loop statement.*/ cout << "Withdraw: Enter a negative number. Deposit: Enter a positive number. Enter 0 to exit." << endl; /*This line is inside the loop body.*/ cin >> a; /*This line, and all the lines following it, are NOT inside the loop body. They will only be run once.*/ if (a > 0) { //etc break; } I think what you intended to do was this: for (int i=0; i < 5; i++) /*This line is the loop statement.*/ { cout << "Withdraw: Enter a negative number. Deposit: Enter a positive number. Enter 0 to exit." << endl; /*This line is inside the loop body.*/ cin >> a; /*Now this line is inside the loop body too.*/ if (a > 0) { //etc break; } /*This is the last line inside the loop body.*/ } /*This is the closing brace for the loop.*/

green meklar

You can this page to see the use of break in C++:

Sovandara

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.