Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am attempting to build a battleship game in C++, this is my first year in programming so im still learning, but Ive already learned some basic stuff... If I could get someones email address, maybe I could email you my code so I could better explain the problem, its really complicated, but to be brief, I have 2 for loops that get stuck in a never ending cycle, Ive tested where the loops are at using cout statements, I can see where it gets stuck, but according to my logic, it should eventually get out of it. when you see the code maybe you will understand what im talking about.... Ive been completely stumped for days, dont know what else to do..... my email address is only_leviathan@hotmail.com

Hey! This is Berdon from http://www.subduck.com. Try posting your question there! You'll get amazingly quick and speedy answers! Its the best place for C++ questions because we have a lot of experienced people over there, and some great tutorials and sources which make it wonderful for newcomers to C++ and even those just with troubles!

Check the conditions in your for loops. For example, a for loop with the following would produce an infinite loop:
for (int i = 0; i >= 0; i++)
{
// some kind of code in here.
}This would produce an infinite loop because you're not telling it when to stop. The loop's condition is set at i >= 0, which is basically telling the program to run as long as the number i is greater than or equal to zero. And we know that this loop will go on forever because i will always be incrementing, or adding '1' to itself after every iteration. Now take this following code for example:
for (int i = 0; i (less than equal to) 10; i++)
{
// some kind of code in here
}Because we have the condition i is less than or equal to 10, the loop will terminate when it hits the 10th iteration thus ending the loop.
Hope this helps.
* Note this text box doesn't allow me to use the boolean operator 'less than'.

Steve,
Please post the offending code (at least, the loop control statements). As suggested above, it's certainly possible that your for-loop test is backwards, or that you're incrementing the loop counter when you should be decrementing (or vice-versa).
regards, Phil

void carrier_placement2(char carrier_loc_y2, int carrier_loc_x2, char grid[][10])
{
int xcoordinate=1;
char ycoordinate=65;
for (int row=0; row> carrier_loc_y2; cin >> carrier_loc_x2;
carrier_placement1(carrier_loc_y2, carrier_loc_x2, grid);
break;
}
}
}
}
}
according to this you would think that it wouldnt work because, column is only updated if the following statement is true (grid[column][row]=='.') however this statement is true 100 percent of the time unless you have already entered another value.. it is a self error checking method i used. but every time, column is updated.sorry about the sloppy format, trying to squeeze it into this box....

Your code looks very confusing. The first thing I noticed in your syntax is that you've missed a closing parenthesis in your for loop condition. We cannot help you if we do not know where your condition ends.
Secondly, why are you comparing the variable 'row' which is of type int with 'carrier_loc_y2' which is of type char in your for loop condition? What is your intent with comparing these values and why are they of the types stated when it is apparent that you are handling only integers in your code?
Thirdly, why are there so many closing brackets at the end of your code and is this intended that your function is being called again inside of your statement? Usually if you call a function within itself you'd produce a recursion of some sort. Where does that break in your code belong (in main or in your function definition)?Is this entire code your function definition or is it your function definition AND part of your code in main()? We need your variables explained. Please provide a more thorough definition of your code with some commenting. Sorry if I offend you, but it needs to be more readable and more commented if we are to help you.
-Rolos

![]() |
Screen resolution
|
Printing in VB with IE Co...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |