473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C++ Loop logic help

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
5 1982

<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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Mark Anderson | last post by:
A 'for' loop takes 3 arguments (initialize; test; increment). The 'test' must equate as true or false This doesn't work... x = 5; for (y=1; (y==5); y+=1) { alert(x * y); } ...nor does... x...
2
by: LisaB | last post by:
I am new to Visual Basic .Net and would like to build a simple application that searches a directory and returns the Path, Size and DateModified of all files that end in ".MDB". a) I would...
4
by: OpticTygre | last post by:
I need to write a loop that prints all the combination possibilities of a character array. Basically, taking a scrambled word, or a regular word, and printing out all the combinations. The...
1
by: Christopher DeMarco | last post by:
Hi all... I've written a class to provide an interface to popen; I've included the actual select() loop below. I'm finding that "sometimes" popen'd processes take "a really long time" to...
6
by: samuel.y.l.cheung | last post by:
Hi, I am trying to convert a Java iterator loop to C++ STL? the loop looks like this: public static boolean func (List aList) { int minX = 0 for (Iterator iter = aList.listIterator(1);...
7
by: Jerry | last post by:
I'm trying to execute a stored procedure in a loop while paging through database table records but the stored procedure isn't running. I get the folowing error: The component 'adodb.connection'...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
6
by: moti | last post by:
Whenever I use PrintDocument.Print() to print a page it goes to PrintDocument_PrintPage and stays there forever unless I set e.HasMorePages to False. When I set it to False it prints the page but...
10
by: wd.jonsson | last post by:
Hmm, my while loop with "or" doesn't seem to work as I want it to... How do I tell the while loop to only accept "Y" or "y" or "N" or "n" input from the str(raw_input)? Thank's in advance! ...
10
by: Jaye | last post by:
Hi. I am a relative newbie to ASP and I am working on an application that uses ASP and an Oracle 9i database. I have a form that allows the user to query the database by selecting a client name(s)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.