Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Problem with my loop in C++ (http://www.go4expert.com/forums/loop-cpp-t24026/)

pkjm17 29Nov2010 07:21

Problem with my loop in C++
 
1 Attachment(s)
My problem here is for my simulation the minutes are off. For example, on one of my runs, Person #1 arrives at minute 6, but why does it show them arriving at minute 1 when the loop just begins? It should start them at minute 6. This is the output I'm getting. Please see screen shot.

Code:

case '2' : //Run simulation
        {
            int t = 0;
            int a[q.getCount()];
            int out[q.getCount()];
            cout<<"Enter simulator time: ";
            cin>>time;
            cout<<"Enter max minutes to enter queue: ";
            cin>>minute;
            cout<<"Enter max minutes to serve: ";
            cin>>serve;
            cout<<"Queue Size: " <<q.getCount() <<endl <<endl;
            cout<<"MINUTE"<<setw(10)<<"PERSON"<<setw(5)<<"IN"<<setw(10)<<"SERVE" <<endl;
            a[0] = rand() % (minute - 1) + 1 + 1;
            for(int i=1; i<q.getCount(); i++) //get arrival minute
                a[i] = a[i - 1] + rand() % (minute - 1) + 1 +1;

            for(int s=0; s<q.getCount(); s++)//get serve minutes
                out[s] = rand()%(serve-1)+1+1;

            while(t < time){//begin loop start with minute 1
                t++;
                cout<<t;

            for(int j=0; j<q.getCount(); j++){
                if(a[j] && t<= a[j])// if( not processed && some one still needs processing
                    cout<<setw(width) <<j+1 <<setw(width) <<a[j]+1 <<setw(width) <<out[j] <<endl;

            for(int c=out[j];c>0;c--){//print out serve minutes until zero
              t++;
              --out[j];
              cout<<t<<setw(width) <<j+1 <<setw(width) <<a[j]+1 <<setw(width) <<out[j] <<endl;}//end of for loop
              a[j]=NULL;
            }//end of for loop
            cout<<endl;
            }//end of while loop
            cout<<endl;
            break;
        }


xpi0t0s 29Nov2010 13:28

Re: Problem with my loop in C++
 
No idea: tiny screenshot, partial code. Try adding some more printf/cout statements to display what the program is doing and why, and see if you can find the problem that way. That's how I debug code that doesn't appear to work properly (when I can't use a debugger).


All times are GMT +5.5. The time now is 16:20.