Xcode不允许我在C ++中初始化值

时间:2015-08-30 22:39:00

标签: c++ xcode

我目前正在使用Stroustrup的书“Programming - Principles and Practice Using C ++”来获取C ++。其中一个主题涉及初始化变量。请考虑本书中的以下示例:

#include <iostream>
#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";
}

在代码中,变量age应该初始化为-1。但是,在Xcode中,当我尝试构建并运行它时,当我输入不是年龄整数的东西时,age设置为0。例如,如果我输入“22 Carlos”,则脚本会返回0表示年龄。根据这本书,我应该看到-1。

我正在使用Xcode作为我的IDE。它与IDE有关吗?

0 个答案:

没有答案