我刚开始使用c ++,因此我无法找到错误所在的位置。当我的代码到达通话assignGate
时,它就会停止工作。我尝试删除了一行程序,一切正常。所以这个问题最有可能出现在那个函数中,但我看不出它有什么问题。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct node {
string code;
int flight;
int time;
string gate;
node *next;
};
bool isEmpty(node *head);
void insert(node *&head, node *&last, string code, int flight, int time);
void remove(node *&head, node *&last);
void assignGate(node *&head, int gates);
void print(node *&head);
int main()
{
int gates;
bool x = false;
string code;
int flight, time, gate;
node *head = NULL;
node *last = NULL;
cout << "Welcome to Gate Scheduler.\n";
ifstream file("FlightList.txt");
if (!file) {
cout << "Unable to open text file. \n";
cout << "Make sure file is in correct location then restart program. \n";
}
while (file >> code >> flight >> time) {
insert(head, last, code, flight, time);
}
cout << "Please enter the max number of gates avaliable:";
cin >> gates;
assignGate(head, gates);
cout << "\n";
print(head);
return 0;
}
bool isEmpty(node *head) {
if (head == NULL) {
return true;
}
else {
return false;
}
}
void insert(node *&head, node *&last, string code, int flight, int time) {
node *temp = new node;
temp->flight = flight;
temp->code = code;
temp->time = time;
temp->next = NULL;
if (isEmpty(head)) {
head = temp;
last = temp;
}
else {
last->next = temp;
last = temp;
}
}
void remove(node *&head, node *&last) {
if (isEmpty(head)) {
cout << "The list is already empty \n";
}
else if (head == last) {
delete head;
head = NULL;
last = NULL;
}
else {
node *temp = head;
head = head->next;
delete temp;
}
}
void assignGate(node *&head, int gates) {
int y = 0;
int gate[6];
node* temp = head;
while (temp->next != NULL) {
if (temp->time > 2300 || temp->time < 600) {
temp->gate = "N/A: Plane lands outside of airport operating hours.";
}
else {
for (y = 0; y <= gates; ++y) {
if (gate[y] == NULL) {
temp->gate = y;
}
else if (gate[y] + 100 < temp->time) {
temp->gate = y;
}
}
if (temp->gate != "0" || "1" || "2" || "3" || "4") {
temp->gate == "All gate are full at this time";
}
}
}
}
void print(node *&head) {
node* temp = head;
while (temp->next != NULL) {
cout << "Flight " << temp->code << " " << temp->flight << " will be at gate " << temp->gate << "\n";
temp = temp->next;
}
}
答案 0 :(得分:1)
首先,您假设该数组初始化为零。它不是。您在gate []中没有零。你应该对它进行初始化以使它们为零。
int gate[6] = {};
你真的应该检查门是否小于阵列的大小。
接下来,您的问题出在这一部分:
else if (gate[y] + 100 < temp->time) {
temp->gate = y;
}
}
if (temp->gate != "0" || "1" || "2" || "3" || "4") {
temp->gate == "All gate are full at this time";
temp->gate = y
没有按你的想法行事。它将字符串分配给存储在整数中的代码。当然,if()会失败。但它也会因为你写错了操作而失败。
(temp->gate != "0" || "1" || "2" || "3" || "4")
等于... (temp->gate != true)
您应该查看运营商优先级表以了解原因。
并且存在算法错误..你永远不会继续使用temp ..它保持不变,你永远不会从while循环中退出。
可能还有其他遗漏,因为它们对当前问题不是必不可少的。我专注于assignGate函数
PS。这不是错误,但风格非常糟糕:
bool isEmpty(node *head) {
if (head == NULL) {
return true;
}
else {
return false;
}
}
'head == NULL'是一个bool表达式。你可以退货。
inline bool isEmpty(node *head) {
return head == NULL;
}