this progran will simulate a process queue, giving time slices based on the priority. The process entries will be in a file, one per line.the data is the start time, priority level, total time to run and process name.
when I run the program I got few problem
1. how can I move to the next line of the file, it seems the program keeps reading from the same line.
2. some of the data on the file either give garbge or nothing such as procname which is processname.
I suspect there is a problem with my getline.

the following is a small portion of the total program but the problem is inside the while loop.
Please I need your help, I need to turn in my homework tomorrow.
thanks a lot.

while (timing < timeU) // A loop for each time unit.

{
    if (myfile.is_open())
    {

        cout << "time =  " << timing << endl;
        getline(myfile, proname);
        myfile>> str_time;
        if (str_time != timing)
     //     myfile.putback(str_time);
        else //if (timing == str_time)// If the start time has arrived
        {
            myfile>> priority >> timerun; //data in the in the input.txt
            proc.set(priority, timerun, proname);    // To place in the process.
            dh.insert(proc);  // Insert the process.
            effpro = priority + timing; // To get the effective prority.
            effpro++; // to refelect the time.
            cout << " inserting  " << proname << endl;
        }

        //Read in and insert the next process.
   /*     if (timing == str_time)
        {
            nextOfproc.set(priority, timerun, proname);
            dh.insert(nextOfproc);
            effpro = priority + timing;
            effpro++;
            cout << "  inserting " << proname << endl;
        } */

    while (!dh.IsEmpty())    // If the heap is not empty.
    {
        dh.deleteMin();      // To get the process from the heap.
        //proc.run();
        cout << "  " << "running" << proname << endl; // To output the name of process.
        if (!proc.done())
        {
            dh.insert(proc);
            // heap.deleteMin();
                                                                              }  // If the process is not done place it in the back of the heap.
        // To updating the effective priority level.
    }

    //timing++;

    if (proc.done())
        cout << proname << "finished" <<endl;

    }

    timing++;

   }

Recommended Answers

All 7 Replies

by the way timing is initilized to zero, and timeu is the total time unit that the user provide, say 100. the while will end when we reach 99.

As you can see, it's very hard to follow your code without the indenting.
Please use code tags, like

[code]

your code goes here

[/code]

And it would be clearer if you don't give us big blocks that are commented out - just show what's in your code.

I have put tags around my code as requested
this progran will simulate a process queue, giving time slices based on the priority. The process entries will be in a file, one per line.the data is the start time, priority level, total time to run and process name.
when I run the program I got few problem
1. how can I move to the next line of the file, it seems the program keeps reading from the same line.
2. some of the data on the file either give garbge or nothing such as procname which is processname.
I suspect there is a problem with my getline.

the following is a small portion of the total program but the problem is inside the while loop.

This is my input

10 9 15 program1
11 1 30 important
12 11 10 junk program
14 15 5 other program
17 6 20 another program

Please I need your help, I need to turn in my homework tomorrow.
thanks a lot.

#include <iostream>
#include <fstream>
#include<string>
#include <stdio.h>
#include <stdlib.h>
//using namespace std;
#include "process.h"
#include "dheap.h"
using namespace std;

int main(int argc, char *argv[])
{
    // argv is the array in which contains those arguments in a string format.
    if (argc != 4) // The number of arguments.
    {
        //TODO-reallocate
        cerr << "You Forget To Enter An Argument!"<<endl;
        exit(1);
    }

    char *TheFile = argv[1];  // is the input.txt at the first argument.
    int timeU = atoi(argv[2]); // The 2nd argument (number of time units to run).
    int dvalue = atoi(argv[3]); // The 3rd argument (the D-value).
    Process proc;  // An object created for the process entires.
    // Process nextOfproc;
    // If I have entered the first process and do what needed, this will be
    // the next object of the process.
    Dheap<Process> dh(dvalue);
    fstream myfile;
    myfile.open(TheFile);
    int priority;
    int timerun;
    int effpro;
    int timing = 0;
    int str_time;
    string proname; // The process name.

while (timing < timeU) // A loop for each time unit.

{
if (myfile.is_open())
{

cout << "time = " << timing << endl;
getline(myfile, proname);
myfile>> str_time;
if (str_time != timing)
// myfile.putback(str_time);
else //if (timing == str_time)// If the start time has arrived
{
myfile>> priority >> timerun; //data in the in the input.txt
proc.set(priority, timerun, proname); // To place in the process.
dh.insert(proc); // Insert the process.
effpro = priority + timing; // To get the effective prority.
effpro++; // to refelect the time.
cout << " inserting " << proname << endl;
}

//Read in and insert the next process.
/* if (timing == str_time)
{
nextOfproc.set(priority, timerun, proname);
dh.insert(nextOfproc);
effpro = priority + timing;
effpro++;
cout << " inserting " << proname << endl;
} */

while (!dh.IsEmpty()) // If the heap is not empty.
{
dh.deleteMin(); // To get the process from the heap.
//proc.run();
cout << " " << "running" << proname << endl; // To output the name of process.
if (!proc.done())
{
dh.insert(proc);
// heap.deleteMin();
} // If the process is not done place it in the back of the heap.
// To updating the effective priority level.
}

//timing++;

if (proc.done())
cout << proname << "finished" <<endl;

}

timing++;

}

lines 45 and 46: getline() reads the entire line, then on line 46 you are trying to read just a single word from that line. If you need to split up the line into individual words then either delete line 45 and not use getline() at all, or delete line 46 and use stringstream to split the line.

would the followind lines do the job

#include <sstream>
  stringstream  processline;
  getline (myfile, processline);
  processline >> str_time;
  processline >> priority  >> timerun  >> procname;

If my input
10 9 30 first process
.
.
and output the four variable I should get
str_time 10
priority 9
timerun 30
procname first process

when I tried to implement this

#include <sstream>  stringstream  processline;  
getline (myfile, processline);  
processline >> str_time;

I got a compilation error

c:\CBProjects\test\main.cpp|16|error: no matching function for call to `getline(std::istream&, std::stringstream&)'|
c:\CBProjects\test\main.cpp|17|error: no match for 'operator>>' in 'process >> processline'|

any suggestion why?

Thank you

Ancient Dragon

it works, some one suggest to use Vector as another option, do you think it is a good idea?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.