1

Newb here working there way toward being a hobbyist.

My current project uses methods, classes, list to display books and comments and to allow users to enter their own comments in a console view. I've built my classes and they are working at this time so I've expunged them for the moment to expose my current problem as clearly as possible. get at my problem which is that I felt my program was getting kind of big quickly so it seemed like a good idea to move some of the code into a method which i called 'Select'. Before i moved the code into the select method, it worked fine/as expected. But now i'm getting error when i test: no enclosing loop out of which to break

The specific error happens on the else if (command == "e"){break;} line

I've tried swapping the keyword 'break' for 'continue', but that did not work. I've looked around the net and stackoverflow, but not found anything that i can understand well enough with my level of understanding to resolve (I'm still a newb).

Code:

 class Program { public void Play(){ Announcer(" \n\nProgram Name Goes Here \n\n"); while (true) { /* * Allow user to display director of books (Three) * allow user to select specific book with any comments it might have (2-4 comments) * Allow user to enter a specific comment * display book with new new added comment * Allow user to exit book * */ Select(); Console.Read(); } } public void Announcer(String strTxt){Console.Write(strTxt);} public String GetString(String strData){ Console.WriteLine(strData); return Console.ReadLine();//traffic control => back to program } public void Select(){ String command = GetString(" \n\n(V)eiw, (S)elect, (C)omment, (R)emove, (E)xit").ToLower(); if (command == "v") { Announcer(" \n\nEnter listing: "); //ViewDirectory();//call view directory here } else if (command == "c") { Announcer(" \n\nEnter comment: "); } else if (command == "s") { //we want to do a selectString method that returns length of selects here String select = GetString(" \n\n(1)st Selection, (2)nd Selection, (3)rd book, (E)xit").ToLower(); if (select == "1") { Announcer(" \n\nDisplay book info + allow for user comment entering"); } else if (select == "2") { Announcer(" \n\nDisplay book info + allow for user comment entering"); } else if (select == "3") { Announcer(" \n\nDisplay book info + allow for user comment entering"); } } else if (command == "e") { break; } else { Console.WriteLine("\n\nOopsy, I don't know that command! \n\n"); } } } //public void ViewDirectory(){ // Console.WriteLine("stuff"); //} 
3
  • Return a bool from Select and base your loop on that bool. Commented Mar 1, 2014 at 18:35
  • you can't break from if block as it is not enclosed in any loop. Commented Mar 1, 2014 at 18:36
  • Thanks I thought that i was still in the loop as the method was placed in the loop. It's really interesting how things change based on where you place something in your code. I will try the bool thingie zimdanen suggests Commented Mar 1, 2014 at 18:43

7 Answers 7

5

Well you're not in a loop, so to terminate the execution of the method, the keyword is 'return'

Sign up to request clarification or add additional context in comments.

Comments

2

After you refactored your code, it's no longer aware that it's inside of a while loop, so the break keyword is unnecessary.

You were using break to terminate the program, so you'll need to do something else to exit the while loop. There are multiple ways you could refactor, but you could try:

while (true) { var command = GetString(" \n\n(V)eiw, (S)elect, (C)omment, (R)emove, (E)xit").ToLower(); if (command == "e") break; Select(command); Console.Read(); } 

Pass command to Select(), so most of your logic is still in the other method... just don't test for command == "e" in Select().

1 Comment

WOW. I thought that this was super elegant and smart (My newbness was able to understand it completely anyway). I ammended my code, i commented out the else if (command == 'e') with the select method() and then ran it and it worked. I'm learning a lot here. Thank you Grant.
1

If you have a switch statement do not put anything outside the case statements, otherwise you will get that error.

switch (val) { //this line should not be here dosomething1(); //==>error case "low": dosomething2(); break; case "high": dosomething3(); break; } 

Comments

1

Just omit the break; statement as there is no need for it, it is only required when you want to quit executing a loop.

Comments

1

break keywords work with loop or switch block. In Select method there is no such statements. you can return true/false from Select method and inside play method you can call break base on return value;

if(Select()) { break; } 

Comments

1

I would update your Select method to return a boolean value indicating that the input was valid. Then, you can set the while condition as follows:

while (Select()) { /* * Allow user to display director of books (Three) * allow user to select specific book with any comments it might have (2-4 comments) * Allow user to enter a specific comment * display book with new new added comment * Allow user to exit book * */ Console.Read(); } 

I would also discourage the use of many else if's like you have, and update them to use switch/case instead. For case s, you can also make that into a separate method.

public bool Select() { bool isValid = true; String command = GetString(" \n\n(V)eiw, (S)elect, (C)omment, (R)emove, (E)xit").ToLower(); switch (command) { case "v": Announcer(" \n\nEnter listing: "); //ViewDirectory();//call view directory here break; case "c": Announcer(" \n\nEnter comment: "); break; case "s": { //we want to do a selectString method that returns length of selects here String select = GetString(" \n\n(1)st Selection, (2)nd Selection, (3)rd book, (E)xit").ToLower() switch (select) { case "1": Announcer(" \n\nDisplay book info + allow for user comment entering"); break; case "2": Announcer(" \n\nDisplay book info + allow for user comment entering"); break; case "3": Announcer(" \n\nDisplay book info + allow for user comment entering"); break; } break; } case "e": isValid = false; break; default: Console.WriteLine("\n\nOopsy, I don't know that command! \n\n"); break; } return isValid; } 

3 Comments

I thought this was really awesome too, as were many of the suggestions which have my brain melting in awesome ways of realization - thank you everyone. I had no idea a bool could be used to send text to the console. I must dig into this.
No problem, glad I could help. What do you mean by bool sending text to console? The bool is just a flag that indicates whether the correct command was used.
We have a bool, the method sends text to the string and i was very hung up on a bool returning true or false, the idea that i could have other things in the method with it that go to the console simply eluded me.
1

Problem : you can't break from if block as it is not enclosed in any loop.

Solution : You just return the status of the condition from the if block to ensure the loop breaks from the caller function .

1. if the command equals to "e" return the true so that caller breaks the loop.
2. else return the false so that loop continues.

 class Program { public void Play(){ Announcer(" \n\nProgram Name Goes Here \n\n"); while (true) { /* * Allow user to display director of books (Three) * allow user to select specific book with any comments it might have (2-4 comments) * Allow user to enter a specific comment * display book with new new added comment * Allow user to exit book * */ if(Select()) break; //break if you get return value of Select() as true Console.Read(); } } public bool Select(){ String command = GetString(" \n\n(V)eiw, (S)elect, (C)omment, (R)emove, (E)xit").ToLower(); if (command == "v") { Announcer(" \n\nEnter listing: "); //ViewDirectory();//call view directory here return false; } else if (command == "c") { Announcer(" \n\nEnter comment: "); return false;} else if (command == "s") { //we want to do a selectString method that returns length of selects here String select = GetString(" \n\n(1)st Selection, (2)nd Selection, (3)rd book, (E)xit").ToLower(); if (select == "1") { Announcer(" \n\nDisplay book info + allow for user comment entering"); } else if (select == "2") { Announcer(" \n\nDisplay book info + allow for user comment entering"); } else if (select == "3") { Announcer(" \n\nDisplay book info + allow for user comment entering"); } return false; } else if (command == "e") { return true; } else { Console.WriteLine("\n\nOopsy, I don't know that command! \n\n"); return false; } } } 

2 Comments

I attempted this, and i got a lot of errors. I see that the word is keyword 'return' is mispelled which i corrected when i tried to run it. But i still got errors when attempting to run this solution.
I think the error came because i copied over my announce and getstring methods and didn't realize it. I retested after putting those mehtods back and it works. But it has a bug in if the 'default' statment prints to console, any result gets the i don't know that command to print again and again. So if i entered V then E

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.