<分区>
所以这是一个非常基本的问题,而且非常琐碎,但我只是通过 C++ 中的编程原则和实践,我的读取字符串和 int 的程序的行为与 Bjarne Stroustrup 写的书不同,所以我是如果他犯了错误,他会感到惊讶。无论如何,这是代码:
#include "..\std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age\n";
string first_name = "???"; // string variable
// ("???” means “don’t know the name”)
int age = -1; // integer variable (1 means “don’t know the age”)
cin >> first_name >> age; // read a string followed by an integer
cout << "Hello, " << first_name << " (age " << age << ")\n";
}
当我在提示符下向终端输入“22 Carlos”时,它输出“Hello, 22 (age 0)”基本上使我用于错误检查的初始化值变得无用。这是 C++ 的新特性还是什么,这就是为什么这本书是错误的?
Edit1:顺便说一句,我在 Windows 7 和 -std=c++11 触发器上使用 GCC for cygwin。