C++ Loop logic help

Discussion in 'C++' started by justbovo@sbcglobal.net, Apr 19, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > 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
    >
    Jim Langston, Apr 19, 2007
    #2
    1. Advertising

  3. shadowman Guest

    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;
    > }
    >
    shadowman, Apr 19, 2007
    #3
  4. justbov Guest

    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.
    justbov, Apr 19, 2007
    #4
  5. anon Guest

    anon, Apr 20, 2007
    #5
  6. Mumia W. Guest

    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/
    Mumia W., Apr 20, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spike
    Replies:
    8
    Views:
    1,460
    Steve Holden
    Feb 9, 2010
  2. addi
    Replies:
    0
    Views:
    267
  3. James Rasmussen

    Infinite Loop in Code -- Logic Error

    James Rasmussen, Jul 17, 2010, in forum: Ruby
    Replies:
    8
    Views:
    186
    James Rasmussen
    Jul 19, 2010
  4. Chip

    Loop Logic Question

    Chip, Dec 10, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    157
    Anno Siegel
    Dec 13, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page