我一直在OSX Yosemite下使用Xcode6.3.2在C++中开发一个应用程序,每当我要求操作员使用下面的代码输入参数时,它都工作得很好,除非操作员在键入时出错,并使用键盘上的箭头更正了他的输入。如果他这样做了,这是他得到的输出:
Please enter the name of the object : test
You entered : test代码如下:
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
char *object_name = new char [20];
cout << "\nPlease enter the name of the object : ";
cin >> object_name;
cout << "You entered : " << object_name;
}在@awesomeyi和@NathanOliver之后,我现在使用std::string,但我仍然遇到同样的问题。
下面是新的代码:
#include <iostream>
#include <string>
using namespace std;
int main( int argc, char** argv )
{
string object_name;
cout << "\nPlease enter the name of the object : ";
cin >> object_name;
cout << "You entered : " << object_name;
}有什么想法吗?
谢谢。
发布于 2016-06-25 18:20:09
我刚刚在我的苹果机上的El Capitan和Dev C++版本5.9.2上尝试了Xcode的最新版本,显然使用Dev C++在流中插入字符没有问题,但是交换数字和插入'?‘字符在Xcode的输出流中很常见。正如@NathanOliver提到的,我也尝试过wcin。这个问题似乎只存在于Xcode编译器中。
https://stackoverflow.com/questions/31810098
复制相似问题