You are using a version without Ads of this website. Please, consider donating:

### Problem with Boolean and While loop.

Im writing a Battleships games, and everything is working (of which I know anyway) as intended, EXCEPT, my ship placement function... which sort of works?
The code works fine up to line 9, however once it gets to the while loop, the program closes with no errors or anything...

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 bool valid; int shipplacement(int &Xcoordinate, int &Ycoordinate); int shipplacement(int &Xshipcoord, int &Yshipcoord){ int a=3; valid=false; char direction; Getshipcoord(Xcoord, Ycoord); while(valid=false){ cout << "Which direction would you like your ship to point?" << endl; cin >> direction; switch (direction){ case 'L': shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ --Xcoord; shipboard[Xcoord][Ycoord]='s'; } return true; break; case 'R': shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ ++Xcoord; shipboard[Xcoord][Ycoord]='s'; } return true; break; case 'U': shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ ++Ycoord; shipboard[Xcoord][Ycoord]='s'; } return true; break; case 'D': shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ --Xcoord; shipboard[Xcoord][Ycoord]='s'; } return true; break; default: cout << "No valid direction was given, the choice is either: up (U), down (D), left (L), right (R)." << endl; return false; break; } } return true; }

Thank you for reading and any help is appreciated! :)
valid = false

gives variable valid a false state.

valid == false

compares valid with false.

= is not the same as ==, the first case is an assignment, second one is comparrison (however itÂ´s spelled!)

You could also use:

while (!valid) {

since ! means NOT, the opposite of
Your while loop is looking for the specific boolean variable "valid", but you never actually change that variable to anything other than false. Your cases may return true, but that's not setting "valid" to true.

Also, from the way you've posted the code it's almost impossible to tell how you have things organized in the rest of the program, which makes it quite difficult to say why it's ending all of a sudden. You declare a function prototype and then a definition right below it, with what looks like a global variable of "valid" right above it. But, why are you returning a true or false after each case? If you want the loop statement to end at these points, you should be using valid = true; and not return true;. Return true; will just tell the function that it's supposed to end, and the final result was a value of "true" or since this is an int function, 1.

Hope that helps.
Tha nk you very much marcos!
@kazaken, the main program literally just has, cin.getline, and outputs some text, then calls the Shipplacement function, and it never made it past the shipplacement, so i figured it was unnecessary. I could copy it in, but well, its 315 lines long and i assumed that the most of it was not needed as the issue iI knew was directly in the shipplacement func...
But thank you for the explanation of the whole Return true/false, because in all honesty I didn't actually know that haha.
Last edited on
Sorry, just realised that it was a blatenly obvious error and just took me a while to see it!

** edited the wrong comment, should have been the bottom ones...
Last edited on
I have to say, your usage of brackets and tabs makes this very hard to read. Unless you're paying very close attention to the number of brackets, you can't even tell when the while loop ends. It would be extremely helpful if you used tabs to create logical breaks in your code and to organize your code within each set of brackets. Having code that's further to the left than it should be creates unnecessary confusion.
Sorry about that, it was at one point, but I emailed it from one computer to another and well, it got weird, and i never sorted it out... ill do it now...
I've cleaned it up as well, and moved something that should stop your program from ending. Namely, the very last return true; statement which is trying to run at the end of each iteration of the loop. Putting this outside the loop will ensure it only returns true once the while loop has finished going through all 5 ships.

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 int shipplacement(int &Xshipcoord, int &Yshipcoord) { int a=3; ShipsToPlace=5; valid=false; char direction; while(ShipsToPlace !=0) { --ShipsToPlace; cout << "You have " << ShipsToPlace << " Ships to place." << endl; Getshipcoord(Xcoord, Ycoord); while(valid == false) { cout << "Which direction would you like your ship to point?" << endl; cin >> direction; switch (direction) { case 'L': { shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ --Xcoord; shipboard[Xcoord][Ycoord]='s'; } valid = true; break; } case 'R': { shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ ++Xcoord; shipboard[Xcoord][Ycoord]='s'; } valid = true; break; } case 'U': { shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ ++Ycoord; shipboard[Xcoord][Ycoord]='s'; } valid = true; break; } case 'D': { shipboard[Xcoord][Ycoord]='s'; for(a=3; a!=0; a--){ --Xcoord; shipboard[Xcoord][Ycoord]='s'; } valid = true; break; } default: { cout << "No valid direction was given, the choice is either: up (U), down (D), left (L), right (R)." << endl; valid = false; break; } } } shipplacement2(Xcoord, Ycoord); } return true; }

I of course haven't really tested this, but it should be fine. :) Hope that helps!
Thank you very much, works wonders!
Ok, so. I have done quiet a few additions, the program now will not allow for covering other ships, and placing ships off edge (0 to 9) It also output some values (so i cant see what its doing when its running(I.e Undo)) and for some reason, when it goes and clears a ship placement if it covers another ship... it goes up to like 11... when it should only ever get to 4? Im sorry if its messy.

Looking at likes 31 and 58 for example (as code is applied to every case statement and is the same (other than things such as the Coordinate values...)