C++ bool输出为205,而不是true或false

C++ bool输出为205,而不是true或false,c++,boolean,cout,direction,C++,Boolean,Cout,Direction,我目前正在尝试使用布尔值输出方向 1是真的。0是错误的 1表示电梯正在上升。0表示电梯正在下降 我希望输出为: 节点:时间戳。当前用户楼层。目的地用户层。DirectionUser正在前进 Node 0 : 1 3 7 true Node 1 : 1 2 9 true Node 2 : 1 7 9 true Node 3 : 2 4 6 true Node 4 : 2 4 8 true Node 5 : 2 1 17 true Node 6 : 5 1 15 true Node 7 : 5 5

我目前正在尝试使用布尔值输出方向

1是真的。0是错误的

1表示电梯正在上升。0表示电梯正在下降

我希望输出为:

节点:时间戳。当前用户楼层。目的地用户层。DirectionUser正在前进

Node 0 : 1 3 7 true
Node 1 : 1 2 9 true
Node 2 : 1 7 9 true
Node 3 : 2 4 6 true
Node 4 : 2 4 8 true
Node 5 : 2 1 17 true 
Node 6 : 5 1 15 true
Node 7 : 5 5 1 false
Node 8 : 6 17 4 false
Node 9 : 6 4 17 true
相反,我得到的是输出:

Node 0 : 1 3 7 205
Node 1 : 1 2 9 205
Node 2 : 1 7 9 205
Node 3 : 2 4 6 205
Node 4 : 2 4 8 205
Node 5 : 2 1 17 205 
Node 6 : 5 1 15 205
Node 7 : 5 5 1 205
Node 8 : 6 17 4 205
Node 9 : 6 4 17 205
代码本身编译时没有错误。我不知道我的逻辑哪里出错了。 当我试图找出一种方法来分配用户请求电梯服务的方向时,用户发出的请求

reqNode *temp = new reqNode;

if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);
下面是我的文件,完整地介绍了代码

binaryHeap.h文件:

#include <vector>
using namespace std;


template <class Comparable>
class BinaryHeap
{

public:
BinaryHeap( ): Array( 11 ), theSize( 0 ){}


bool isEmpty( ) const
{
    return theSize == 0;
}

const Comparable & findMin( ) const
{
    if( isEmpty( ) )
    {
    cout << "heap empty" << endl; //throw UnderflowException( );
    //break;
    }
return Array[ 1 ];
}

void insert( const Comparable & x)
{
    Array[ 0 ] = x;   // initialize sentinel

    if( theSize + 1 == Array.size( ) )
      Array.resize( Array.size( ) * 2 + 1 );

    // Percolate up
    int hole = ++theSize;
        for( ; x < Array[ hole / 2 ]; hole /= 2 )
           Array[ hole ] = Array[ hole / 2 ];
           Array[ hole ] = x;
}

void deleteMin( )
{
    if( isEmpty( ) )
    {
    cout << "heap empty" << endl; //throw UnderflowException( );
    break;
    }
 Array[ 1 ] = Array[ theSize-- ];
 percolateDown( 1 );
}

void deleteMin( Comparable & minItem )
{
  minItem = findMin( );
  Array[ 1 ] = Array[ theSize-- ];
  percolateDown( 1 );
}

void makeEmpty( )
{
    theSize = 0;
}

private:

int theSize;  // Number of elements in heap
vector<Comparable> Array;    // The heap Array

void buildHeap( )
{
    for( int i = theSize / 2; i > 0; i-- )
    percolateDown( i );
}

void percolateDown( int hole )
{
  int child;
  Comparable tmp = Array[ hole ];

  for( ; hole * 2 <= theSize; hole = child )
  {
    child = hole * 2;
    if( child != theSize && Array[ child + 1 ] < Array[ child ] )
        child++;
    if( Array[ child ] < tmp )
        Array[ hole ] = Array[ child ];
    else
        break;
  }
 Array[ hole ] = tmp;
}
};
好了,所有的文件都到了。哇,我想我的空格键是从所有的4x间距断开的!哎呀

最后是driver2.cpp:所有魔法都发生在哪里

#include <iostream>
#include <fstream>
#include <string>
#include "lazy.h"
#include <algorithm>
using namespace std;



void setNode(reqNode nizzode, int priority)
{
nizzode.priority = priority;
}


int main()
{
Lazy lazy(1);
}
就这样

现在,我的代码从T1.txt文件中接收10个用户请求,并生成一个请求节点,该节点具有以下属性:请求所在楼层的时间戳、当前楼层、发出请求的用户的起始楼层,以及最终的目标楼层。我还认为我有逻辑根据当前楼层和目标楼层确定用户的方向。我错了。非常感谢您的帮助

205是0xCD的十六进制,它是Visual Studio中处理调试生成时未初始化数据的可能填充符之一。 您需要调试代码,并追溯这些未初始化数据的来源。
这里没有人会为您调试它。

任何非零值都被认为是真的。现在添加Makefile并配置脚本。您可以添加一些自述文件,我敢打赌您可以将这段代码缩减到十几行,用于编译、运行和说明问题。在这一点上,要么答案是显而易见的,要么你会发现有人愿意阅读并找出问题所在。@NeelBasu-请不要!越少越好!这里99%的代码与此问题无关。您的内存泄漏!myStream.eof循环。您可以调用new,但不能调用delete。解决方案:替换reqNode*temp=新的reqNode;具有节点温度;并对语法进行相应的修改。我知道这已经过时了。然而,我同意shoosh的观点。最有可能的情况是bool变量尚未初始化为true或false。我见过类似的事情。
class reqNode//create a node that takes in several properties.
{
public:

reqNode()
:direction(true)
{   
//default constructor
//initialize the memeber variables of this class.
priority = start = destination
= timestamp = start_time
= finish_time = -1;
set_dir(true);
}

reqNode(const reqNode &copy){       //copy constructor
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;
}

reqNode & operator=(const reqNode & copy){ // operation overload
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;

return *this;
}

bool operator<(const reqNode &rhs) const// operation overload
{
if(this->priority < rhs.priority)
return true;
else
return false;
}
void setPriority(int x){//assigning the priority to of whatever I declare "x" to be.
priority = x;
}

void calculate_priority()
{
if( start < destination )
priority = destination;  // Requests for higher floors will have lower priorities due to min heap structure
else
priority = 20 - destination;  // Requests for higher floors will have higher priorities
}

void set_dir(bool dir)
{
direction = dir;
}

//declare the member variables.
int priority, start, destination, 
timestamp, start_time, finish_time;
bool direction;
};
#include <iostream>
#include <fstream>
#include <stdio.h> //allows for the usage of getchar
#include "Elevator2.h"

void Elevator::displayMessage()
{
cout <<"Welcome to University Towers " << endl << endl << endl<<        "----------------------------" << endl;
}

double Elevator::get_total_cost()
{
return total_cost;
}

double Elevator::get_total_wait_time()
{
return total_wait_time;
}

bool Elevator::get_direction()
{
return direction;
}

void Elevator::change_direction()
{
direction = !direction;
}

void Elevator::set_direction(bool new_direction)
{
direction = new_direction;
}

int Elevator::get_elevator_floor()
{
return elevator_floor;
}

void Elevator::increment_elevator_floor()
{
if(direction) //if direction is TRUE (elevator moving upward) increment
elevator_floor++;
else
elevator_floor--;
}

void Elevator::set_elevator_floor(int new_floor)
{
elevator_floor = new_floor;
}

void Elevator::readRequests()
{
ifstream myStream("T1.txt");

while(!myStream.eof())
{
int timestamp ,currentFloor, destinationFloor;


myStream >> timestamp >> currentFloor >> destinationFloor;
//cout<< endl <<"The current timestamp is "<< timestamp << "The current floor is " <<  currentFloor 
//  << " and the destination floor is " << destinationFloor << endl << endl;
//cout<< endl;

reqNode *temp = new reqNode;

//initialize request node object
temp->timestamp = timestamp;
temp->start = currentFloor;
temp->destination = destinationFloor;
temp->start_time = -1;
temp->finish_time = -1;

//temp->direction = ( (currentFloor < destinationFloor) ? 1 : 0 );
temp->calculate_priority();

if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);

request.push(*temp);//push nodes into the request bank
}
int i = 0;
while( !request.empty() )
{

 cout << "Node " << i << " : " << request.front().timestamp << " " <<  request.front().start << " " << request.front().destination
<< " " <<  request.front().direction << endl;

//printf_s("%d\n", request.front().direction);

request.pop();//popping the request in order to test
i++;
}

//bool test = false;

//cout << test;

}
#include <iostream>
#include "binaryHeap.h"
#include "reqnode.h"
#include <queue>
using namespace std;

class Elevator
{
public:

//Elevator();//Default constructor
Elevator(int initial_floor)//Each object starts with a initial floor aka the current  floor.
:total_wait_time(0), total_cost(0), elevator_floor(initial_floor),   direction(true)//initialize each object accordingly.
{
readRequests();//for each elevator object...created(i.e - request read, there is a  node created).
}

void displayMessage();//display greeting text.
double get_total_cost();//returns the number of floors the elevator travels.
double get_total_wait_time();//returns the number of floors the elevator travels to  get to user.

bool get_direction();  // get the current direction of the elevator.
void set_direction(bool);  // sets direction to desired direction of the user.

void change_direction();  // reverses direction of the elevator.

int get_elevator_floor();// return the elevator floor(currently on).
void set_elevator_floor(int); // sets according to the user's request.

void increment_elevator_floor();  // moves elevator one floor determined by direction(dependent on the set direction bool).

protected://only calls from inherited/friendship classes are aloud. Never calls from the Driver.

queue<reqNode> waiting_queue;//queue stores reqNode objects, we name this queue - waiting queue.
queue<reqNode> request;// ""                  ""                                - request.
BinaryHeap<reqNode> service_queue;// ""                        ""               - service queue.
queue<reqNode>  finished;//""                      ""                           - finished.

private:

double total_wait_time;//number of floors the elevator travels to get to user.
double total_cost;//number of floors the elevator travels while servicing.
int elevator_floor;//initial floor value.
bool direction;//iterating through the floors for the sabbath algorithm
void readRequests();//read the in the requests per user and store them(by way of the request queue/bank).  
};
#include "lazy.h"

void Lazy::setAttributes(reqNode Nodey) 
{

int costToserve = abs(Nodey.start - Nodey.destination);//the floors between user's start_floor and user's destination_floor.
int directionOf = (Nodey.start - Nodey.destination) * -1;//the direction the elevator is heading.
int priority = abs(Nodey.destination - get_elevator_floor());//already in the servicing queue.

}

void Lazy::simulation()
{
time = 0;

while( !request.empty() )  //  while request remain, do:
{
//  check for and read new requests into incoming/waiting queue
while(request.front().timestamp == time)  //  schedule new requests
{
waiting_queue.push(request.front());  // places next request into waiting queue
request.pop();
}

scheduler();  // inputs waiting requests into servicing queue based on elevator's current state

if( !service_queue.isEmpty() )  //  if( !service_queue.isEmpty() )
{
if( service_queue.findMin().direction != get_direction() )  //  if(top node of service queue's direction == current direction of the elevator)
           {                                    
change_direction();//then we change the direction.
}//  else set direction to top node's direction

increment_elevator_floor();  // increments/decrements elevator floor by one.


while( service_queue.findMin().destination == get_elevator_floor() )//check top node to see if it is done;  current floor is destination
{
reqNode * temp;

service_queue.deleteMin(*temp); // min item is serviced; pop from heap
temp->finish_time = time; 
finished.push(*temp);  //  place serviced node to finished bank
}

}
increment_elevator_floor();
time++;
}
}

void Lazy::scheduler()
{
// UPDATE:  PRIORITY TO BE CALCULATED BY REFERENCE POINT (DETERMINED BY DIRECTION) IN DRIVER BY CALL TO REQNODE METHOD

int size = waiting_queue.size(); //set to wait queue's size

if( !service_queue.isEmpty() )
{
for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor 
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() );  //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}
}
else
{

if( !waiting_queue.empty() )//check waiting queue (if not empty)
{
/*  Elevator should take the direction of next request waiting and begin moving in that direction.
Elevator should pick up requests that are on the way and in same direction as next waiting request
*/
set_direction(waiting_queue.front().direction);

for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor 
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() );  //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}   
}

}

}
#include "Elevator2.h"

class Lazy : public Elevator
{   
public:
Lazy(int initial_floor)//initialize the same way you did your base class. same parameters.
:Elevator(initial_floor){}

void simulation();//carries out the normal elevator simulation

private:

void scheduler();//scheduler to handle. 
void setAttributes(reqNode);//set attributes of each request node.
int time;
};   
#include <iostream>
#include <fstream>
#include <string>
#include "lazy.h"
#include <algorithm>
using namespace std;



void setNode(reqNode nizzode, int priority)
{
nizzode.priority = priority;
}


int main()
{
Lazy lazy(1);
}