C++ Loop logic help

J

justbovo

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
 
J

Jim Langston

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
 
S

shadowman

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;
}
 
J

justbov

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.
 
M

Mumia W.

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top