dimanche 10 novembre 2019

Print statements work inside a loop, but not outside - C++ 11

I am looping through a vector of objects, and am performing some logic steps inside that loop sequence.

If I place any cout << " test " << endl; or any print statements inside the loop, they print out just fine.

However, if I try and print immediately outside the loop, the print statements fail and do not show when running the program (even though it compiles).

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cmath>
#include "Passenger.h"
using namespace std;

int time = 0;

void changeTime(int curr, int future){
    time += abs((curr - future) * 10);
}

int main(){

    // OTHER CODE THAT WAS DELETED TO BE COMPLETE, MINIMAL, VERIFIABLE
    // OTHER CODE THAT WAS DELETED TO BE COMPLETE, MINIMAL, VERIFIABLE

    // Pickup passengers
    // This is the loop where the code executes properly

    for(unsigned int i = 0; i < passengers.size(); i++){
        // Loop through elevators
        for (int j = 0; j < elevators.size(); j++){
            // Increment time by 1 second
            time += 1;

            // If the elevator hasn't moved
            if(elevators[j].getStatus().compare("STOPPED") == 0 && elevators[j].getStops() == 0 && elevators[j].checkCount() == true){                
                // Add the passengers stop to the list of stops for the elevator
                elevators[j].addStop(passengers[i].getEndFloor());
                // Change the current passenger count
                elevators[j].changePassengerCount(1);

                // If the elevator's current destination is higher than its current location
                if(elevators[j].getRoute() > elevators[j].getLocation()){
                    // Move up
                    elevators[j].setStatus("MOVING UP");
                // If the elevator's current destination is lowerr than its current location
                }else if (elevators[j].getRoute() < elevators[j].getLocation()){
                    // Move down
                    elevators[j].setStatus("MOVING DOWN");
                }else{
                    // Otherwise, its there, so set it to stopping
                    elevators[j].setStatus("STOPPING");
                } //end else

                break;                     
            } //end if

            // If the elevator is stopped
            else if (elevators[j].getStatus() == "STOPPED"){
                // Add the passengers stop to the list of stops for the elevator
                elevators[j].addStop(passengers[i].getEndFloor());
                // Change the current passenger count
                elevators[j].changePassengerCount(1);

                // If the elevator's current destination is higher than its current location
                if(elevators[j].getRoute() > elevators[j].getLocation()){
                    // Move up
                    elevators[j].setStatus("MOVING UP");
                // If the elevator's current destination is lowerr than its current location
                }else if (elevators[j].getRoute() < elevators[j].getLocation()){
                    // Move down
                    elevators[j].setStatus("MOVING DOWN");
                }else{
                    // Otherwise, its there, so set it to stopping
                    elevators[j].setStatus("STOPPING");
                } //end else

                break;                     
            } //end if

            // If the current elevator is going to pass this passengers start floor
            else if (elevators[j].getRoute() > passengers[i].startFloor){
                // Add the passengers stop to the list of stops for the elevator
                elevators[j].addStop(passengers[i].getEndFloor());
                // Change the current passenger count
                elevators[j].changePassengerCount(1);

                // If the elevator's current destination is higher than its current location
                if(elevators[j].getRoute() > elevators[j].getLocation()){
                    // Move up
                    elevators[j].setStatus("MOVING UP");
                // If the elevator's current destination is lowerr than its current location
                }else if (elevators[j].getRoute() < elevators[j].getLocation()){
                    // Move down
                    elevators[j].setStatus("MOVING DOWN");
                }else{
                    // Otherwise, its there, so set it to stopping
                    elevators[j].setStatus("STOPPING");
                } //end else
                break;                     
            } //end if

            else{
                // If none of these conditions match, move on to the next elevator
                continue;
            } //end else
        } //end looping through elevators for pickup


        // Now, decide when to drop off passengers
        for(int k = 0; k < elevators.size(); k++){
            // Update the time
            time += 1;
            if(elevators[k].getLocation() != elevators[k].getRoute()){
                 //cout << " In this Block! " << endl;
                // Calculate the time to move floors
                changeTime(elevators[k].getLocation(), elevators[k].getRoute());
                // Set location to where you are going
                elevators[k].setLocation(elevators[k].getRoute());
                // Update status
                elevators[k].setStatus("STOPPING");                
            }else{
                //cout << " Otherwise, I'm here " << endl;
                time += 2;
                elevators[k].setStatus("STOPPED");
                elevators[k].dropoffPassenger();
            } //end else
        } //end dropoff sequence

        // This works, and prints out 500 times (one time for each item in the vector)
        cout << "Got here, end of dropoff" << endl;

    } //end looping through passengers

    // This fails here, why?
    cout << " Got here" << endl;

    // for(int x = 0; x < elevators.size(); x++){
    //     cout << " Evaluating Elevator: " << elevators[x].getName() << endl;
    //     elevators[x].printQueue();
    // }

    cout << " TIME: " << time << endl;

} //end main

I would expect Got here to show in the console, but it does not.

Why do the print statements inside the loop work, but not outside?

I did not find other posts on SO that mimicked this error.

N.B. - if more code is needed, please comment and let me know. I am trying to keep this in accordance with this SO rule.

Aucun commentaire:

Enregistrer un commentaire