a lil help.. prog's done but wrong output

This is a discussion on a lil help.. prog's done but wrong output within the C++ Programming forums, part of the General Programming Boards category; I have a hw prob here that im done coding w/.. i just get the wrong output about halfway and ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    a lil help.. prog's done but wrong output

    I have a hw prob here that im done coding w/.. i just get the wrong output about halfway and i know kind of wat it is but i cant fix it. my thought is im makin my function to complex.
    http://cis.poly.edu/cs1124/Homework/hw01/hw01.html
    thats the site that explains the hw prob and heres my code..

    Code:
    #include <vector>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    struct Process //Structure will hold all process information from the procces file
    {	
    	//CHECK THESE TYPES!!!
    	int procId; // process ID ... 1000.. 1001 etc
    	string name;  // holds the name of the process
    	int cpuTime; // Total CPU time needed for process
        int timeBurst; // Burst for each process
    	
    };
    
    //will gather contents from stream and enter them into a vector of structs
    void fillVector(ifstream&, vector<Process>&); 
    	
    //Processes the schedule and outputs as it creates the schedules.
    void procSched(vector<Process>&);
    
    
    //----------------------------------------------
    int main()
    {
    	vector<Process> proc; //Vector of process structs
    	ifstream ifs; // filestream for process text
    
    	ifs.open("process.txt"); // open file and check for error while opening
    		if (!ifs)
    		{
    			cerr << "Error opening 'process.txt' \n ";
    			exit(1); //exit if error is found
    		}
    	
    	fillVector(ifs, proc); //fill the vector with information from the stream
    	procSched(proc); //output the scheduling as it is calculated
    
    	return 0;
    }
    //-----------------------------------------------
    
    
    void fillVector(ifstream& inputFile, vector<Process>& procVec)
    {
    	Process tempProc; //temporary variable to fill process vector
    
    	//Loop through the file checking for each struct member
    	while (inputFile >> tempProc.procId >> tempProc.name 
    					 >> tempProc.cpuTime >> tempProc.timeBurst)
    	{
    		procVec.push_back(tempProc); // fill the vector with each temp member
    		
    	}
    	
    }
    
    void procSched(vector<Process>& procVector)
    {	
    	// totProc is the total number of Proccess that need to run
    	//size_t totProc = procVector.size(); 
    	
    	int time(0); //will print the time each process began
    
    	while (!procVector.empty()) // Continue to process until vector is empty
    	{
    		
    		for (size_t j = 0; j < procVector.size(); j++) // Loop through the vecotr 
    		{
    			
    			//The time is decreased by each processes burst
    			procVector[j].cpuTime -= procVector[j].timeBurst;
    
    			if (procVector[j].cpuTime <= 0) // If time is below 0, the process is exhausted
    				{
    					//output the process information with 0 being the final time left
    					cout << time << ' ' << procVector[j].procId << ' '
    						 << procVector[j].name <<  ' ' 
    						 << "0" << endl;
    					
    					//The time the next process begins is incremented by the burst 
    					//of the process just completed
    					time += procVector[j].timeBurst;
    
    					//delete the element of the vector 
    					//that no longer runs as a process
    					procVector.erase(procVector.begin() + j); 
    					
    				}
    			else // if its not 0 the process is ongoing
    				{
    					//output the process information
    					cout << time  << ' ' << procVector[j].procId << ' ' 
    					<< procVector[j].name << ' '
    					<< procVector[j].cpuTime  << endl;
    
    					//The time the next process begins is incremented by the burst 
    					//of the process just completed
    					time += procVector[j].timeBurst;
    				}
    
    		}	
    	}
    	
    
    }

    I know the prob is in my last function.. i think im either deleting something 2 quick or displaying 2 late. something to that effect... if u think i can do this more effectively plz tell me.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Ok, you have to think about what happens when a process dies.
    Code:
    if (procVector[j].cpuTime <= 0)
    You're not going to give it another time slice
    Code:
    time += procVector[j].timeBurst;
    you're just going to increment by one cycle
    Code:
    time++;
    Presumably process kill time is one cycle
    Code:
    procVector.erase(procVector.begin() + j);
    Now your vector is smaller by one process. You need to adjust index 'j' to compensate for this
    Code:
    j--;
    Well that's my take on it anyway. Check it out and see if you agree for the same reasons. Just because I think the output is correct doesn't mean my logic is!

    Edit: I wanted to say something before but forgot. Time for that now
    Code:
    if (ifs.is_open())
    {
        fillVector(ifs, proc); //fill the vector with information from the stream
        procSched(proc); //output the scheduling as it is calculated
    }
    else
        cerr << "Error opening 'process.txt' \n ";
    ifs.is_open() is the proper way to check that the input file is indeed open. You can rid of that exit(1), that's how I'd do it anyway

    p.s. You forgot to doing something before the program ends...
    Last edited by Br5an; 09-12-2005 at 04:08 AM. Reason: additional comment

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    i think u mean i forgot to close the file...

    as for wat u wrote ill check that out later tonight when i get home.

    for the ifs.open .... we have 2 do it that way or lose some points .


    =======
    I just tried it and it works. thx for da help
    Last edited by gamer4life687; 09-12-2005 at 06:33 AM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    I wasn't talking about
    Code:
    ifs.open(process.txt)
    I was talking about
    Code:
    if (!ifs)
    Change the -external- input file name to something else, for example rename process.txt to process.bin, and you'll soon see why
    Code:
    if (!ifs)
    is not the correct way to check and see if a file is open

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I know what u meant. i was referring to the whole open process including checking for failure. its my schools standard and we basically have no choice but 2 do it that way including closing and checking.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Ok, I looked it up and
    Code:
    !ifs
    has defined behavior. From there it's a small step to do something like
    Code:
    if(ifs)
    and I'm not sure that behavior is defined. Maybe someone "in the know" will comment on that. BTW, your welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 08:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 11:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 12:56 PM
  5. Help! I am getting wrong output
    By mastamoo in forum C Programming
    Replies: 0
    Last Post: 10-08-2001, 11:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21