我最近從windows上的dev C++切換到osx dev環境,並試圖使用崇高的文本3.但是,當我運行我的程序時,出現了分段錯誤。以下是錯誤消息:sublime text C++編譯會導致seg fault
/bin/bash: line 1: 20506 Segmentation fault: 11 "/Users/jimi/Documents/University/lab0603"
[Finished in 1.2s with exit code 139]
[shell_cmd: g++ "/Users/jimi/Documents/University/lab0603.cpp" -o "/Users/jimi/Documents/University/lab0603" && "/Users/jimi/Documents/University/lab0603"]
[dir: /Users/jimi/Documents/University]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
下面是完整的代碼:
#include <iostream>
#include <cmath>
using namespace std;
string flightDirections[16] = {"ENE", "NE", "NNE", "N", "NNW", "NW", "WNW", "W", "WSW", "SW", "SWS", "S", "SSE", "SE", "ESE", "E"};
double cruisingSpeed = 0;
double windSpeed = 0;
int windDirection = 0;
double flightDistance = 0;
string numberToDirection(int direction) {
return (direction) ? "The wind is blowing from the East." : " The wind is blowing from the West.";
//this way we only have one return statement :)
}
void getInput() {
cout << "Hello! \n"
<< "Thank you for choosing to use this really great program!! \n"
<< "This program will compute the necessary heading adjustment for your flight,"
<< " and provide the estimated flight time. \n";
cout << "Enter the aircraft cruising speed in still air (in km/h): ";
cin >> cruisingSpeed;
cout << " \n \t cruising speed = " << cruisingSpeed << "\n Enter the wind speed in km/h: ";
cin >> windSpeed;
cout << " \n \t wind speed = " << windSpeed << "\n Enter 1 if the wind is blowing from the West and -1 if wind is blowing from the East:";
cin >> windDirection;
cout << "\n\t" << numberToDirection(windDirection) << "\n Enter the distance between the originating and destination cities, in km:";
cin >> flightDistance;
cout << "\n\t flight distance = " << flightDistance << "\n Enter the compass direction of the destination city, relative to the originating cities, using the following values:";
for (int i = 0; i < sizeof(flightDirections); i++) {
cout << i + 1;
cout << flightDirections[i];
}
cin.ignore();
}
int main() {
getInput();
return 0;
}
這是怎麼回事?
如果您手動運行該命令,會發生什麼情況? – Praetorian
'sizeof(flightDirections)'這是什麼值?你調試了你的代碼嗎? – PaulMcKenzie
@Praetorian我該怎麼做?謝謝 –