簡體   English   中英

在C ++程序上退出狀態-1

[英]Exit Status -1 on C++ Program

執行后,我的代碼給出的退出狀態為-1。 我可以顯示輸入是否有任何區別。 有人能找到原因嗎?

INPUT:

6

N 10

2號

3號

4號

5號

8號

我已經查看了2D整數數組和代碼中的變量,尋找未初始化的變量,但是沒有發現此類錯誤。 有人可以看到我為什么要獲得退出狀態-1嗎?

#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int main() {
  ofstream fout("mowing.out");
  ifstream fin("mowing.in");
  int n; fin >> n;
  int ans = 0;
  int field[2003][2003];
  for (int i = 0; i < 2003; i++) {
    for (int j = 0; j < 2003; j++) {
      field[i][j] = 0;
    }
  }
  int xloc = 1001, yloc = 1001, time = 0;
  for (int i = 0; i < n; i++) {
    char dir; int steps;
    fin >> dir >> steps;
    if (dir == 'N') {
      for (int j = 1; j < steps; j++) {
        yloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    if (dir == 'S') {
      for (int j = 1; j < steps; j++) {
        yloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    if (dir == 'W') {
      for (int j = 1; j < steps; j++) {
        xloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else {
      for (int j = 1; j < steps; j++) {
        xloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
  }
  if (ans == 0) fout << -1 << "\n";
  else fout << ans << "\n";
  return 0;
}

fin >> dir >> steps;

您沒有得到期望值

第一個輸入是int n; fin >> n; int n; fin >> n; 如果輸入文件就像您在問題中指出的那樣, dir的第一個值將是換行符(輸入文件中的6之后),則>>將毫無問題地保持在錯誤中,因為在與N不兼容之后步驟是一個int

為了解決這個問題

  • 不必將intchar讀混合使用,無需確定格式並顯式繞過所有必需的字符
  • 或者更簡單,更安全,不要讀取dirchar而是一個字符串,因此string dir; 而不是char dir; 並且當然在XNSW之后,將測試(dir == 'X')更改為(dir == "X")

您可能錯過了添加其他內容的原因,因為您這樣做:

if (dir == 'N') {
  ...
}
if (dir == 'S') {
  ...
}
if (dir == 'W') {
  ...
}
else {
  ...
}

所以最后還有正常的情況下,“E”也做了N和S的情況下,也許你想

if (dir == 'N') { // in fact (dir == "N") see remark above
  ...
}
else if (dir == 'S') { // in fact (dir == "S") see remark above
  ...
}
else if (dir == 'W') { // in fact (dir == "W") see remark above
  ...
}
else {
  ...
}

我鼓勵您檢查是否已成功打開文件,目前假設您已打開文件,並檢查輸入文件中的閱讀情況是否良好

請注意我的樹莓派堆棧被限制在8192K( ulimit -s ),所以現場的尺寸太大,我改變了它是靜態的 ,以便能夠執行程序(我更換使用2 復雜的初始化)

mowing.out的預期內容是什么 做上面的改變我得到18


如果我使用定義:

#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int main() {
  ofstream fout("mowing.out");

  if (!fout.is_open()) {
    cerr << "cannot open mowing.out" << endl;
    return -1;
  }

  ifstream fin("mowing.in");

  if (! fin.is_open()) {
    cerr << "cannot open mowing.int" << endl;
    return -1;
  }

  int n; 

  if ((!(fin >> n)) || (n < 0)) {
    cerr << "invalid number of couples" << endl;
    return -1;
  }

  int ans = 0;
  static int field[2003][2003] = { 0};
  int xloc = 1001, yloc = 1001, time = 0;

  for (int i = 0; i < n; i++) {
    string dir; int steps;

    if (!(fin >> dir >> steps)) {
      cerr << "error while reading fin & dir" << endl;
      return -1;
    }

    if (dir == "N") {
      for (int j = 1; j < steps; j++) {
        yloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else if (dir == "S") {
      for (int j = 1; j < steps; j++) {
        yloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else if (dir == "W") {
      for (int j = 1; j < steps; j++) {
        xloc--;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
    else {
      for (int j = 1; j < steps; j++) {
        xloc++;
        time++;
        if (field[xloc][yloc] != 0) ans = max(ans, time-field[xloc][yloc]);
        field[xloc][yloc] = time;
      }
    }
  }
  if (ans == 0) fout << -1 << "\n";
  else fout << ans << "\n";
  return 0;
}

編譯執行:

pi@raspberrypi:/tmp $ g++ -g -pedantic -Wextra -Wall e.cc
pi@raspberrypi:/tmp $ cat mowing.in 
6

N 10

E 2

S 3

W 4

S 5

E 8
pi@raspberrypi:/tmp $ ./a.out
pi@raspberrypi:/tmp $ cat mowing.out 
18

除了bruno提出的優點之外,我相信您遇到的問題的根本原因是(nomen預兆!)堆棧溢出。

您的數組太大,無法放在堆棧上。 快速計算(假設sizeof(int) == 4 ):

2003 * 2003 * 4 B = 16048036 B = 15671.91015625 KiB = 15.304599761962890625 MiB

您嘗試在堆棧上分配15.3 MiB的內存,但是根據此問題 ,默認情況下Windows允許1 MiB,Linux通常允許8 MiB。

您應該自己在堆上分配內存,或者(最好)使用std::vector ,如下所示:

std::vector<std::vector<int>> field (2003, std::vector(2003));
//it is already initialized above, no need for for loops ;)
//later on it can be used like regular array in most of the cases

有人可以看到我為什么要獲得退出狀態-1嗎?

不只是任何人- 都能做到!

在此處輸入圖片說明

...通過使用調試器在程序執行期間在不同點停止程序,並檢查nans和其他變量的值。

我假設您正在使用一些IDE來編輯和編譯代碼。 IDE通常具有集成的調試器。 例子:

看來這幾天的學生真的沒被教過調試... :-(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM