下面是一個簡單的家庭作業問題,我有一個介紹CS類:C++分段故障
#include <iostream>
#include <string>
int main() {
using namespace std;
int num1; int num2; int num3; int ans;
char oper;
/*
cout << "Enter an arithmetic expression:" << endl;
cin >> oper >> num1 >> num2 >> num3;
DEBUGGING
*/
cout << "enter an operator" << endl;
cin >> oper; /* Segmentation error occurs here... */
cout << "enter number 1" << endl;
cin >> num1;
cout << "enter number 2" << endl;
cin >> num2;
cout << "enter number 3" << endl;
cin >> num3;
cout << "okay" << endl;
if (oper = "+") {
if (num1 + num2 != num3) {
cout << "These numbers don't add up!" << endl;
}
else {
ans = num1 + num2;
cout << num1 << "+" << num2 << "==" << ans << endl;
}
}
else if (oper = "-") {}
else if (oper = "*") {}
else if (oper = "/") {}
else if (oper = "%") {}
else {
cout << "You're an idiot. This operator clearly does not exist... Try again " << endl;
}
return 0;
}
我不是很熟悉分段錯誤,所以如果有人可以解釋,如果我做錯了它」 d太棒了。
你輸入什麼? – chris
http://en.wikipedia.org/wiki/Segmentation_fault –
編譯你的程序時啓用調試信息和警告(例如Linux上的'g ++ -Wall -g'),然後學習使用調試器(例如Linux上的'gdb' )。 –