Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 some really weird stuff is happening
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Fonzie




PostPosted: Sun Jan 15, 2006 10:07 pm   Post subject: some really weird stuff is happening

I'm using 4.5 of the borland compiler.

So I had just finished my final project and was about to relax. I had tested it and everything worked great. Then I noticed a small area of the code where there was room to improve it by adding random numbers instead of predetermined. So I finished that and now my whole program isn't working. And it's not working in the most illogical ways. I had this bit of code completely seperate from the code I changed (do I still need to put this in the tag? It's 3 lines).

while (count<13){
taken1[count]=taken[count];
count+=1;}

for some reason it now goes into an infintie loop. and what's even stranger is what happened next. I made this piece of code and replaced the other one to investigate the problem

while (count<13){
taken1[count]=taken[count];
cout<<count;
count+=1;}

exactly the same except it shows what count equals. that piece of code works perfectly. No infinite loops. How is that possible!? Some other weird stuff happens later in the program with the code I changed to, like entire groups of code skipped for no reason. Can someone please explain why this is happening?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Jan 15, 2006 10:31 pm   Post subject: (No subject)

Use a modern compiler, like a decent version of GCC.
Fonzie




PostPosted: Sun Jan 15, 2006 10:43 pm   Post subject: (No subject)

sorry that's what the school gives me, some crap that was made in 95. I'm not sure if it's relevent but I was using these libraries when everything was working fine

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

and I added

#include <stdlib.h>

for random numbers when everything went to hell. So can anyone help? or failing help, a possible explanation would be good too. I'd really like to know what's causing this erratic behaviour.
[Gandalf]




PostPosted: Sun Jan 15, 2006 10:51 pm   Post subject: (No subject)

How are you generating random numbers?
Fonzie




PostPosted: Sun Jan 15, 2006 11:05 pm   Post subject: (No subject)

well near the top of the page I use the function, randomize();
then when I need to generate the number I use this line of code

randy = random(3);

(where randy is a variable and 3 is the range (0-2))

It seems when I got frustrated I also put the function, randomize();
right above the line where I get a randomized number. I believe this was after my program went crazy but it may be significant.
md




PostPosted: Sun Jan 15, 2006 11:07 pm   Post subject: (No subject)

if it's not too big you could post your entire code. Also; I really recomend using a better compiler, just because the school gave you crap doesn't mean you ahve to use it.
Fonzie




PostPosted: Sun Jan 15, 2006 11:12 pm   Post subject: (No subject)

you guys REALLY don't want to look through it. It's my final project and I'm very messy. 850 lines of uncommented (I do that later (I know stupid)) unindented (I never do that, I miss when turing did it auto from just pressing f2 (I know stupid)) mess.
md




PostPosted: Sun Jan 15, 2006 11:15 pm   Post subject: (No subject)

Fonzie wrote:
you guys REALLY don't want to look through it. It's my final project and I'm very messy. 850 lines of uncommented (I do that later (I know stupid)) unindented (I never do that, I miss when turing did it auto from just pressing f2 (I know stupid)) mess.


Well, if it's that messy perhaps going through it all and commenting and making it properly indented will help you find errors. Doing things afterwards is always a bad idea though, as you never know if you'll remember what you were thinking when you wrote a particular peice of code.
Sponsor
Sponsor
Sponsor
sponsor
Fonzie




PostPosted: Sun Jan 15, 2006 11:22 pm   Post subject: (No subject)

if I understood the error, I would, but the stuff I'm seeing is just boggling my mind. I really can't see any possible reason why adding output would prevent a loop from being infinite.

I just changed my program back to the way it was before I made the "improvements". I'm presently commenting that. I'd just really like to know why these insane actions are happening so I can avoid running into them again, or perhaps make those improvements to my present program.
Andy




PostPosted: Mon Jan 16, 2006 9:34 am   Post subject: (No subject)

try to debug the program by setting a break point at the incrementing line, and then trace through it to see what happens.

dont use outputs to debug code, use debuggers
Fonzie




PostPosted: Mon Jan 16, 2006 9:16 pm   Post subject: (No subject)

well after some experimentation I found that generating a random number in a loop totally destroys c++. All bets are off and things just go crazy when I do that. I imagine it has something to do with the crappy compiler.

Anywho I've wormed my way around that problem and added the random numbers in, though not as I had hoped. I'll be sure to post my final here when I finish adding the last pieces.
md




PostPosted: Mon Jan 16, 2006 10:40 pm   Post subject: (No subject)

Fonzie wrote:
well after some experimentation I found that generating a random number in a loop totally destroys c++. All bets are off and things just go crazy when I do that. I imagine it has something to do with the crappy compiler.

Anywho I've wormed my way around that problem and added the random numbers in, though not as I had hoped. I'll be sure to post my final here when I finish adding the last pieces.


Though you are using an old compiler I don't think that the compiler or it's libraries are at fault. More likely it was a bug in your code. If you were using arrays were you writing beyond the end? Borland's compiler may not be the best, but it's definitely not so bad as to screw up when you use random numbers in a loop.
Andy




PostPosted: Tue Jan 17, 2006 10:00 am   Post subject: (No subject)

how are u generating the numbers? by seeding the time?
wtd




PostPosted: Tue Jan 17, 2006 12:09 pm   Post subject: (No subject)

Seeing all of your code would help.
[Gandalf]




PostPosted: Wed Jan 18, 2006 4:23 pm   Post subject: (No subject)

First, seed the random numbers with the time:
code:
srand ((unsigned)time(NULL));

Then, use the rand() function to get a random number between 0 and 1. You will likely have to manipulate it to get the numbers in the range you want.

If you want to avoid problems with the code others help you with, you should really get a newer compiler. I believe everything in conio.h is not commonly used outside of those outdated compilers, and not cross-platform compatible.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: