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
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:
- How to echo error from foreach loop?Best solution by youtube.com
- How to do a line break for a UITextField?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- Any spring break places within 3 hours from Philly?Best solution by Yahoo! Answers
- How safe is the Orange Line CTA to Midway and to the Loop?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.