Ich bin neu in der Arbeit mit C++, so habe ich Probleme zu finden, wo mein Fehler liegt. Wenn mein Code den Anruf assignGate
erreicht, funktioniert es nicht mehr. Ich habe versucht, mein Programm mit Zeile entfernt und alles hat gut funktioniert. Also ist das Problem wahrscheinlich in dieser Funktion, aber ich kann nichts falsch daran sehen.C++ Programm stoppt bei Funktionsaufruf
#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;
}
}
Es gibt viele Dinge mit 'assignGate', aber das sind alles grundlegende C++ Regeln, die über ein gutes Buch gelöst werden können. – DeiDei
Wie ich schon sagte, ich bin neu in C++ zu programmieren und mein Code bekommt keine Fehler, wenn ich es debugge. Also, wenn Sie es mir zumindest in die richtige Richtung zeigen könnten, um den Fehler zu beheben, würde ich es begrüßen. Vielen Dank! – mathmaster12
Was geben Sie als 'Gates' Argument in 'assignGate' Funktion ein? Wenn die 'Datei' eine Zeile enthält und Sie einen Wert für den Eingabe-Gates größer als 1 eingeben, wird 'assignGate' unerwartet beendet. –