|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Passing Boolean error?
Hey, im a c++ dabbler, and i tried to write a simple program where a '*' bounced off of the boundaries (0 and 70) of a single dimensional array. Im not sure where, or what the error is. If anyone can help me out id really appreciate it, the code is in the attached txt file.
|
|
#2
|
|||
|
|||
|
Are you getting any compile/runtime errors, or are you just not receiving the results you're after?
__________________
____________________________________________ Developer Shed Weekly Writer | DevArticles Forum Moderator Build Your Own KlipFolio Klip With PHP FrankManno.com - Under Construction Design Interactive Group - Under Construction |
|
#3
|
|||
|
|||
|
No Errors and no Warnings. All that happens is the object that is suppose to ricochet of the boundaries simply hits the boundary wall then dissappears.
|
|
#4
|
|||
|
|||
|
not sure if you got this fixed yet, but try this code.
Code:
#include<iostream.h>
void main(void)
{
char array[70];
int x = 0;
int filler;
bool move = true;
bool movement(int x, bool move);
void showarray(char array[70]);
int rotations = 0;
/* fill array with 70 #'s */
do
{
array[x] = '#';
x++;
}while(x<=70);
/* put '*' into arrays first position */
x = 0;
array[x] = '*';
/* print the array as it is now */
showarray(array);
/* for 500 rotations move the '*' in the array */
do
{
/* check direction - first time in, x = 0, so forward motion */
movement(x, move);
/* while moving forwards alter the array */
while(move == true)
{
filler = x;
x++;
array[x] = '*';
array[filler] = '#';
/* show the new array */
showarray(array);
/* check direction again - if false,
jumps out of this loop into the next */
movement(x, move);
}
while(move == false)
{
filler = x;
x--;
array[x] = '*';
array[filler] = '#';
showarray(array);
movement(x, move);
}
rotations++;
}while(rotations<500);
}
/* control the movement (direction) of the '*' in the array */
bool movement(int x, bool move)
{
if(x==0)
move = true;
if(x==70)
move = false;
return(move);
}
/* print the current array */
void showarray(char array[])
{
int x = 0;
do
{
cout<<array[x];
x++;
}while(x<=70);
cout<<endl;
}
basically it removes the if-else statement you had and replaces it with 2 while loops that continually check the movement function for the direction. This hasnt been tested, but it looks as though it should work.By the way, its always a good idea to comment your code so others know straight away what piece of code does what instead of trying to work it out. if it doesnt work when compiled try using the statement Code:
move = movement(x, move) to change the value of move, as the movement function may not be altering the move variable properly Hope this helped |
|
#5
|
|||
|
|||
|
It Worked!
Hey, thanks a lot man! When I used your code as displayed it did the same exact thing that it did before. However when I replaced the "movement(x, move)" with "move = movement(x, move)" it worked perfectly. Thanks for your help and i'll comment my programs from now on.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Passing Boolean error? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|