Problem with my loop in C++

Discussion in 'C++' started by pkjm17, Nov 29, 2010.

  1. pkjm17

    pkjm17 New Member

    Joined:
    Nov 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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;
            }
    
     

    Attached Files:

  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice