2017-02-13 5 views
1

C++で作業するのが初めてのので、エラーがどこにあるのかを見つけるのが難しいです。私のコードがassignGateという呼出しに達すると、それは動作を停止します。私は行を削除して私のプログラムを試してみました。だから問題は、その機能の可能性が高いですが、私はそれに間違って何かを見ることはできません。関数呼び出し時にC++プログラムが停止する

#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

「assignGate」には多くの問題がありますが、それらはすべて、良い本を使って解決できる基本的なC++ルールです。 – DeiDei

+0

私はC++でプログラミングするのが初めてだと言いましたが、私のコードはデバッグする際にエラーが出ません。だからもしあなたが少なくとも私がそれを感謝するだろうエラーを修正する正しい方向に私を指すことができる。ありがとう! – mathmaster12

+0

'assignGate'関数で 'gates'引数として入力するものは何ですか? 'ファイル'に1行があり、ゲート値が1より大きい場合、 'assignGate'は予期せず終了します –

答えて

1

まず、配列がゼロに初期化されていると仮定します。そうではない。あなたはゲート[0]にゼロを持っていません。値を0にするために値を初期化する必要があります。

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)理由を理解するには、オペレータの優先度の表を参照する必要があります。

そして、アルゴリズム上の間違いがあります。一時的に進むことはありません。同じままですが、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; 
} 
関連する問題