seeking ideas on c++ FIFO problem.

Discussion in 'Software' started by jenritz2k3, Mar 22, 2009.

  1. jenritz2k3

    jenritz2k3 Private E-2

    may i ask for a sample program for a fifo problem? in c++ is any body has?
    it is like an array? or does any body can help me figure out what is missing in my program?

    here is the program pls help me figure out what is missing? thanks in advance..

    #include <iostream.h>
    #include<conio.h>
    #include<string.h>


    int ctr, sw=0, op, res;
    char job[4], mm[10][4];

    void main()
    { clrscr(); //intialize memory
    for(ctr=0;ctr<10;ctr++)
    strcpy(mm[ctr],"---");

    while(op<4)
    {
    clrscr();
    //display memory
    for(ctr=0;ctr<10;ctr++)
    {
    gotoxy(5,ctr+4);
    cout<<mm[ctr];
    }
    //options for memory management
    gotoxy(1,15);
    cout<<"OPTIONS";
    gotoxy(1,17);
    cout<<"1. ALLOCATION";
    gotoxy(1,18);
    cout<<"2. DEALLOCATION";
    gotoxy(1,19);
    cout<<"3. COMPACTION";
    gotoxy(1,20);
    cout<<"4. EXIT";
    gotoxy(1,20);
    cout<<"CHOICE:";
    //enter choices[1-4] and place it to a variable op
    gotoxy(10,22);
    cin>>op;

    switch(op)
    {
    case 1:
    //initialize switch to 0 which signifies that memory is not yet full.
    sw=0;
    //test if the memory if already full
    for (ctr=0; ctr<10;ctr++)
    { res=strcmp(mm[ctr],"---");
    if(res==0)
    {
    gotoxy(1,24);
    cout<<"ENTER JOB NAME :";
    gotoxy(18,24);
    cin>>job;
    //how to input exlusivity here?
    //exlusivity is the term use when two similar jobs are not allowed to be
    //in the memory at the same time.
    strcpy(mm[ctr],job);
    // assign 1 as switch value after allocating the incoming job
    sw=1;
    break;

    }

    }
    if(sw==0)
    {
    cout<<"MEMORY FULL!"
    getch();
    }
    case 2: //how to input deallocation?
    //is the process of freeing a memory location
    break;

    case 3:

    //how to input compaction in c++
    //compaction is the process of all occupied areas to
    //one end or the other of the main storage.
    break;

    }
    }

    }

    plsss. any idea c++ masters..??? thanks in advance.
     
  2. smilinggeek

    smilinggeek Private E-2

    Hmm, sounds like an assignment. Engaging tutor mode and all sorts of unsolicited opinion. :)

    Suggestions:
    You're using C++ as if it was C with a couple extensions. Nothing really wrong with that, but classes are cleaner if you know how to use them.

    Use descriptive constants rather than hardcoded numbers or strings (ie: you use "---" as a constant: treat it as such and create a named constant for it). It's a good habit to get into.

    Practice modularity. Functions and procedures are good, use them. Even if they're small to start with, by making things modular you can work and expand a particular function without worrying about interference with other areas of the program. For instance, output of the menu, "memory" initialization, each of "allocation", "deallocation", etc. all should be separate functions.
    Yes, it might look a bit silly initially with such small programs, but getting into the habit of properly factoring a program is an important skill to develop.

    Beware strcpy - it's a easy way to cause buffer overflow and all sorts of subtle bugs.

    You say it's a FIFO problem, but what I'm seeing is a memory allocation/deallocation problem. Now, I guess FIFO might be used as part of that problem, but it's a small part.

    I see most of the program is missing - such as all the allocation, deallocation code. I don't even see any pseudo-code to show how far you got in the design. So there are a few problems here.

    - need to show some sort of overall design to show what it is you're trying to accomplish. Show some sort of logic flow design, anything. For instance:
    • user starts program
    • selects one of allocation, deallocation, compaction
    • if selects allocation:
      • enters job number
      • what are you expecting to have happen next?
    • etc.
    Show us what you're expecting, show us the code you've written to accomplish what you're expecting, then I for one can probably help.

    - your question needs to be more precise than "how to input XX here". Show some pseudo code or something to give a clue about what it is you're trying to accomplish.

    - this looks like a homework assignment. Is it? Honesty is your best friend here. I'm more than happy to guide someone through the learning process, and provide hints when you get stuck but can show you've actually thought about it. But I for one am not going to do someone's homework for them. People don't learn anything if someone else does the work for them.

    - If you have specific questions I'll be more than happy to help. "Write my program for me" isn't a specific question.

    Finally, I recommend reading "How to Ask Questions the Smart Way" by Eric Raymond (ESR), with specific attention to the homework section:
    http://www.catb.org/~esr/faqs/smart-questions.html#homework
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds