By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,571 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,571 IT Pros & Developers. It's quick & easy.

C++ Loop logic help

P: n/a
Hello, My name is Justin.

I am working on a part of a Find Memory module for a program. Here's
how it works.. I enter '13' at the main menu which branches out to my
Find Memory module.
I ask the user how much Memory is needed and saved it as
'Needed_Memory_Amount '.
Then I run thru a couple of loops to see what I can do. Either I find
a spot for the memory on the 'FixedBlockTable', or I must give them a
spot on the 'MemoryWaitTable'.

I thought my code looked pretty good, but after I compiled I realized
there is something wrong with my code. Here's what happens..

I enter 13, and then It asks how much. If i put 1800 it returns like
this ..

Code:
Please enter the amount of memory needed :
1800
The job is TOO BIG !!
It will NEVER run !!

Please press ENTER to continue...
....So that all works fine. but Here's the output I get after I enter a
number that surely fits.

Code:
Please enter the amount of memory needed :
152
**DEBUG** First if loop nma <= memlimit
.....and then it returns back to the main menu. i see it prits out my
debug statement

Can someone enligghten me to why so it stops right there, and won't
continue with the rest of my loops ??

Here's the code, I'm not sure of the problem since I know it stops
after the first loop.
const int MEMLIMIT = 1000;
int Needed_Memory_Amount,
FBT_Slot,
MWT_Slot;

char Junk [30];

cout << "Please enter the amount of memory needed :"
<< '\n';

cin >Needed_Memory_Amount;

if (Needed_Memory_Amount <= MEMLIMIT)
{
cout << "First if loop nma <= memlimit";

for (FBT_Slot = 1; FBT_Slot <= 3; FBT_Slot++)
if (FixedBlockTable [FBT_Slot].Status = 0)
{

cout << "2nd if slot = status =0";

if (FixedBlockTable [FBT_Slot].Size >=
Needed_Memory_Amount)
{

FixedBlockTable [FBT_Slot].Status = 1;
cout << "You can have partition @ loc : "
<< FixedBlockTable [FBT_Slot].Location
<< endl
<< endl
<< "Please press enter to continue."
<< endl;
cin.getline (Junk, 20, '\n');
cin.getline (Junk, 20, '\n');
return;
}
}

for (MWT_Slot = 1; MWT_Slot <= 3; MWT_Slot++)

if (MemoryWaitTable [MWT_Slot].Status = 0)
{

MemoryWaitTable [MWT_Slot].Status = 1;
MemoryWaitTable [MWT_Slot].Size =
Needed_Memory_Amount;
MemoryWaitTable [MWT_Slot].Address = Running;
cout << "Sorry, Your job must wait."
<< "\nPlease check the Memory Wait Table.";

P (0, Running);
TrafficController ();
}
}

else

cout << endl
<< endl
<< "The job is TOO BIG !!"
<< endl
<< "It will NEVER run !!"
<< endl
<< endl
<< "Please press ENTER to continue..."
<< endl;
cin.getline (Junk, 20, '\n');
cin.getline (Junk, 20, '\n');
return;
}
Please and thanks - j

Apr 19 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

<ju******@sbcglobal.netwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
Hello, My name is Justin.

I am working on a part of a Find Memory module for a program. Here's
how it works.. I enter '13' at the main menu which branches out to my
Find Memory module.
I ask the user how much Memory is needed and saved it as
'Needed_Memory_Amount '.
Then I run thru a couple of loops to see what I can do. Either I find
a spot for the memory on the 'FixedBlockTable', or I must give them a
spot on the 'MemoryWaitTable'.

I thought my code looked pretty good, but after I compiled I realized
there is something wrong with my code. Here's what happens..

I enter 13, and then It asks how much. If i put 1800 it returns like
this ..

Code:
Please enter the amount of memory needed :
1800
The job is TOO BIG !!
It will NEVER run !!

Please press ENTER to continue...
...So that all works fine. but Here's the output I get after I enter a
number that surely fits.

Code:
Please enter the amount of memory needed :
152
**DEBUG** First if loop nma <= memlimit
....and then it returns back to the main menu. i see it prits out my
debug statement

Can someone enligghten me to why so it stops right there, and won't
continue with the rest of my loops ??

Here's the code, I'm not sure of the problem since I know it stops
after the first loop.
const int MEMLIMIT = 1000;
int Needed_Memory_Amount,
FBT_Slot,
MWT_Slot;

char Junk [30];

cout << "Please enter the amount of memory needed :"
<< '\n';

cin >Needed_Memory_Amount;

if (Needed_Memory_Amount <= MEMLIMIT)
{
cout << "First if loop nma <= memlimit";

for (FBT_Slot = 1; FBT_Slot <= 3; FBT_Slot++)
if (FixedBlockTable [FBT_Slot].Status = 0)
Did you mean == 0 here? An assignment in an if statement checks the value.
The value of something = 0 is 0, which evaluates to false.
{

cout << "2nd if slot = status =0";

if (FixedBlockTable [FBT_Slot].Size >=
Needed_Memory_Amount)
{

FixedBlockTable [FBT_Slot].Status = 1;
cout << "You can have partition @ loc : "
<< FixedBlockTable [FBT_Slot].Location
<< endl
<< endl
<< "Please press enter to continue."
<< endl;
cin.getline (Junk, 20, '\n');
cin.getline (Junk, 20, '\n');
return;
}
}

for (MWT_Slot = 1; MWT_Slot <= 3; MWT_Slot++)

if (MemoryWaitTable [MWT_Slot].Status = 0)
{

MemoryWaitTable [MWT_Slot].Status = 1;
MemoryWaitTable [MWT_Slot].Size =
Needed_Memory_Amount;
MemoryWaitTable [MWT_Slot].Address = Running;
cout << "Sorry, Your job must wait."
<< "\nPlease check the Memory Wait Table.";

P (0, Running);
TrafficController ();
}
}

else

cout << endl
<< endl
<< "The job is TOO BIG !!"
<< endl
<< "It will NEVER run !!"
<< endl
<< endl
<< "Please press ENTER to continue..."
<< endl;
cin.getline (Junk, 20, '\n');
cin.getline (Junk, 20, '\n');
return;
}
Please and thanks - j

Apr 19 '07 #2

P: n/a
ju******@sbcglobal.net wrote:
<SNIP>
>
const int MEMLIMIT = 1000;
int Needed_Memory_Amount,
FBT_Slot,
MWT_Slot;

char Junk [30];

cout << "Please enter the amount of memory needed :"
<< '\n';

cin >Needed_Memory_Amount;

if (Needed_Memory_Amount <= MEMLIMIT)
{
cout << "First if loop nma <= memlimit";

for (FBT_Slot = 1; FBT_Slot <= 3; FBT_Slot++)
if (FixedBlockTable [FBT_Slot].Status = 0)
This condition will always be false, since you are setting
FixedBlockTable [FBT_Slot].Status to zero here.

Did you mean:
if (FixedBlockTable [FBT_Slot].Status == 0)
??
{

cout << "2nd if slot = status =0";

if (FixedBlockTable [FBT_Slot].Size >=
Needed_Memory_Amount)
{

FixedBlockTable [FBT_Slot].Status = 1;
cout << "You can have partition @ loc : "
<< FixedBlockTable [FBT_Slot].Location
<< endl
<< endl
<< "Please press enter to continue."
<< endl;
cin.getline (Junk, 20, '\n');
cin.getline (Junk, 20, '\n');
return;
}
}

for (MWT_Slot = 1; MWT_Slot <= 3; MWT_Slot++)

if (MemoryWaitTable [MWT_Slot].Status = 0)
Again, same as above.
{

MemoryWaitTable [MWT_Slot].Status = 1;
MemoryWaitTable [MWT_Slot].Size =
Needed_Memory_Amount;
MemoryWaitTable [MWT_Slot].Address = Running;
cout << "Sorry, Your job must wait."
<< "\nPlease check the Memory Wait Table.";

P (0, Running);
TrafficController ();
}
}

else

cout << endl
<< endl
<< "The job is TOO BIG !!"
<< endl
<< "It will NEVER run !!"
<< endl
<< endl
<< "Please press ENTER to continue..."
<< endl;
cin.getline (Junk, 20, '\n');
cin.getline (Junk, 20, '\n');
return;
}
Apr 19 '07 #3

P: n/a
Hey thank you all, that is exactly what was wrong. I can't believe i
over looked that ! Thanks again so much, you've been so helpful.

Apr 19 '07 #4

P: n/a
justbov wrote:
Hey thank you all, that is exactly what was wrong. I can't believe i
over looked that ! Thanks again so much, you've been so helpful.
Here is how to prevent such bugs
http://www.gmonline.demon.co.uk/csce....html#constant
Apr 20 '07 #5

P: n/a
On 04/19/2007 12:30 PM, justbov wrote:
Hey thank you all, that is exactly what was wrong. I can't believe i
over looked that ! Thanks again so much, you've been so helpful.
Some people prefer to place the constant before the variable in
conditional expressions:

if (0 == FixedBlockTable [FBT_Slot].Status) { ... }

Just a couple of days ago, doing this helped me catch a bug before it
became a bug. :-)

--
Count the YOYOs:
http://home.earthlink.net/~mumia.w.18.spam/games_fever/
Apr 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.