【发布时间】:2018-05-11 01:28:06
【问题描述】:
我是一名业余程序员,正在学习如何在 xcode 中使用 c++,并且我一直在尝试创建一个程序,您可以在其中回答所提出的问题,并且问题会根据您的答案而有所不同。问题是,我不断收到错误消息:线程 1:EXC_BAD_ACCESS (code=1, address=0x0),我不知道是什么原因造成的。这是我目前的代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "what is your name ";
getline (std::cin, name);
string yes;
cout << "ok, " << name << ", do you want to play a game? ";
getline (std::cin, yes);
cout << "\nno " << std::endl;
string input =0;
cin >> input;
string Yes = "yes";
string No = "no";
if (input == No)
{
cout << "ok, sorry" << endl;
}
else if (input == Yes)
{
cout << " a question" << endl;
}
}
【问题讨论】:
-
string input =0;Rubber ducky 想知道你想在这里发生什么。 -
另外,如果你是一个业余程序员,我建议你阅读好的 C++ 材料。没有任何 C++ 书籍在任何代码示例中都有 user4581301 指出的错误,除非它是您的拼写错误。
-
将“输入”声明为其余代码的标识符。其中大部分是随机教程拼凑在一起的,所以我不完全确定。对不起。
-
@j_lo2004 -- 为什么将字符串设置为“0”?将设置删除为 0 个东西。
-
string是一个字符序列。通常,您会在引号中指定某些内容。string input = "Nothing to see here. Move along.";给它分配一个数字是不寻常的。在这种情况下,这是致命的,因为幕后发生了一些非常巧妙的事情。我想我知道发生了什么,我正在做一些实验,看看我是否正确。