example code:
fin >> numOfRecords;
printf("%i", numOfRecords); /*prints the correct number of records*/
for (int i = 1; i < numOfRecords; i++)
printf("%i", i); /*never stops incrimenting*/
[edited by - prod on April 29, 2003 11:16:44 PM]
loop values and compile time
I was wondering if the values for loops are calculated at compile time. In my database program, the first thing it does is load the number of records and then loop through that number to load the information.For the loop to work correctly i have to "hard code" in the value for the number of records. For example (of my problem) :
If something is ''hard coded'' the value can''t change. Your value can change.
I can''t see how your code is incorrect. It also works fine on my machine (if that''s anything to go by!). I just changed fin to cin.
I can''t see how your code is incorrect. It also works fine on my machine (if that''s anything to go by!). I just changed fin to cin.
numOfRecords
might be a bad value due to either the file being loaded incorrectly or corrupt. In that way it would appear to loop forever, when in reality it's going to a really high limit. Output is extremely slow, take away the printf
and it should run to the limit rather quickly, assuming the loop isn't optimized away and my theory was correct [edited by - Zipster on April 30, 2003 4:02:26 AM]
I think the OP is confusing macro loop limits with variable loop limits.
#define MAX_STRING_LENGTH 100
for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char
is different from
unsigned int MAX_STRING_LENGTH = 100;
for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char
The first one is compile time (before compile time, actually). The second is at runtime.
#define MAX_STRING_LENGTH 100
for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char
is different from
unsigned int MAX_STRING_LENGTH = 100;
for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char
The first one is compile time (before compile time, actually). The second is at runtime.
quote:Original post by flangazor
for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char
with that semi-colon you won''t be doing anything for each char
I put it in to make it syntactically complete.
[edited by - flangazor on April 30, 2003 7:39:35 AM]
[edited by - flangazor on April 30, 2003 7:39:35 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement